From bb48df9ad120b307f2e36feebbf9a35872948b20 Mon Sep 17 00:00:00 2001 From: Mike de Boer Date: Thu, 25 Jul 2013 02:43:19 +0200 Subject: [PATCH] Bug 890690 - Support POST searches in about:home. r=gavin --HG-- extra : transplant_source : %A5%23%F7%EC%87%DC%ED%D3V%00%C1%D7K%90%D4%009Pp%24 --- browser/base/content/abouthome/aboutHome.js | 44 +++++++++++++++++---- browser/base/content/browser.js | 3 ++ browser/modules/AboutHomeUtils.jsm | 6 +-- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/browser/base/content/abouthome/aboutHome.js b/browser/base/content/abouthome/aboutHome.js index f385cd2fc4dd..e0f3fbd7efd0 100644 --- a/browser/base/content/abouthome/aboutHome.js +++ b/browser/base/content/abouthome/aboutHome.js @@ -294,13 +294,6 @@ function onSearchSubmit(aEvent) let searchURL = document.documentElement.getAttribute("searchEngineURL"); if (searchURL && searchTerms.length > 0) { - const SEARCH_TOKENS = { - "_searchTerms_": encodeURIComponent(searchTerms) - } - for (let key in SEARCH_TOKENS) { - searchURL = searchURL.replace(key, SEARCH_TOKENS[key]); - } - // Send an event that a search was performed. This was originally // added so Firefox Health Report could record that a search from // about:home had occurred. @@ -308,7 +301,42 @@ function onSearchSubmit(aEvent) let event = new CustomEvent("AboutHomeSearchEvent", {detail: engineName}); document.dispatchEvent(event); - window.location.href = searchURL; + const SEARCH_TOKEN = "_searchTerms_"; + let searchPostData = document.documentElement.getAttribute("searchEnginePostData"); + if (searchPostData) { + // Check if a post form already exists. If so, remove it. + const POST_FORM_NAME = "searchFormPost"; + let form = document.forms[POST_FORM_NAME]; + if (form) { + form.parentNode.removeChild(form); + } + + // Create a new post form. + form = document.body.appendChild(document.createElement("form")); + form.setAttribute("name", POST_FORM_NAME); + // Set the URL to submit the form to. + form.setAttribute("action", searchURL.replace(SEARCH_TOKEN, searchTerms)); + form.setAttribute("method", "post"); + + // Create new elements for search param. + searchPostData = searchPostData.split("&"); + for (let postVar of searchPostData) { + let [name, value] = postVar.split("="); + if (value == SEARCH_TOKEN) { + value = searchTerms; + } + let input = document.createElement("input"); + input.setAttribute("type", "hidden"); + input.setAttribute("name", name); + input.setAttribute("value", value); + form.appendChild(input); + } + // Submit the form. + form.submit(); + } else { + searchURL = searchURL.replace(SEARCH_TOKEN, encodeURIComponent(searchTerms)); + window.location.href = searchURL; + } } aEvent.preventDefault(); diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index b75e498449fb..d386360d9198 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -2314,6 +2314,9 @@ function BrowserOnAboutPageLoad(doc) { let updateSearchEngine = function() { let engine = AboutHomeUtils.defaultSearchEngine; docElt.setAttribute("searchEngineName", engine.name); + docElt.setAttribute("searchEnginePostData", engine.postDataString || ""); + // Again, keep the searchEngineURL as the last attribute, because the + // mutation observer in aboutHome.js is counting on that. docElt.setAttribute("searchEngineURL", engine.searchURL); }; updateSearchEngine(); diff --git a/browser/modules/AboutHomeUtils.jsm b/browser/modules/AboutHomeUtils.jsm index 09d395fee082..847948a008da 100644 --- a/browser/modules/AboutHomeUtils.jsm +++ b/browser/modules/AboutHomeUtils.jsm @@ -25,13 +25,11 @@ this.AboutHomeUtils = { get defaultSearchEngine() { let defaultEngine = Services.search.defaultEngine; let submission = defaultEngine.getSubmission("_searchTerms_", null, "homepage"); - if (submission.postData) { - throw new Error("Home page does not support POST search engines."); - } return Object.freeze({ name: defaultEngine.name, - searchURL: submission.uri.spec + searchURL: submission.uri.spec, + postDataString: submission.postDataString }); },