search.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifdef 0
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #endif
  6. #include ../shared/searchenginelogos.js
  7. // This global tracks if the page has been set up before, to prevent double inits
  8. var gInitialized = false;
  9. var gObserver = new MutationObserver(function (mutations) {
  10. for (let mutation of mutations) {
  11. if (mutation.attributeName == "searchEngineURL") {
  12. setupSearchEngine();
  13. if (!gInitialized) {
  14. gInitialized = true;
  15. }
  16. return;
  17. }
  18. }
  19. });
  20. window.addEventListener("pageshow", function () {
  21. window.gObserver.observe(document.documentElement, { attributes: true });
  22. });
  23. window.addEventListener("pagehide", function() {
  24. window.gObserver.disconnect();
  25. });
  26. function onSearchSubmit(aEvent) {
  27. let searchTerms = document.getElementById("searchText").value;
  28. let searchURL = document.documentElement.getAttribute("searchEngineURL");
  29. if (searchURL && searchTerms.length > 0) {
  30. const SEARCH_TOKEN = "_searchTerms_";
  31. let searchPostData = document.documentElement.getAttribute("searchEnginePostData");
  32. if (searchPostData) {
  33. // Check if a post form already exists. If so, remove it.
  34. const POST_FORM_NAME = "searchFormPost";
  35. let form = document.forms[POST_FORM_NAME];
  36. if (form) {
  37. form.parentNode.removeChild(form);
  38. }
  39. // Create a new post form.
  40. form = document.body.appendChild(document.createElement("form"));
  41. form.setAttribute("name", POST_FORM_NAME);
  42. // Set the URL to submit the form to.
  43. form.setAttribute("action", searchURL.replace(SEARCH_TOKEN, searchTerms));
  44. form.setAttribute("method", "post");
  45. // Create new <input type=hidden> elements for search param.
  46. searchPostData = searchPostData.split("&");
  47. for (let postVar of searchPostData) {
  48. let [name, value] = postVar.split("=");
  49. if (value == SEARCH_TOKEN) {
  50. value = searchTerms;
  51. }
  52. let input = document.createElement("input");
  53. input.setAttribute("type", "hidden");
  54. input.setAttribute("name", name);
  55. input.setAttribute("value", value);
  56. form.appendChild(input);
  57. }
  58. // Submit the form.
  59. form.submit();
  60. } else {
  61. searchURL = searchURL.replace(SEARCH_TOKEN, encodeURIComponent(searchTerms));
  62. window.location.href = searchURL;
  63. }
  64. }
  65. aEvent.preventDefault();
  66. }
  67. function setupSearchEngine() {
  68. let searchText = document.getElementById("searchText");
  69. let searchEngineName = document.documentElement.getAttribute("searchEngineName");
  70. let searchEngineInfo = SEARCH_ENGINES[searchEngineName];
  71. let logoElt = document.getElementById("searchEngineLogo");
  72. // Add search engine logo.
  73. if (searchEngineInfo && searchEngineInfo.image) {
  74. logoElt.parentNode.hidden = false;
  75. logoElt.src = searchEngineInfo.image;
  76. logoElt.alt = searchEngineName;
  77. searchText.placeholder = "";
  78. } else {
  79. logoElt.parentNode.hidden = false;
  80. logoElt.src = SEARCH_ENGINES['generic'].image;
  81. searchText.placeholder = searchEngineName;
  82. }
  83. }