aboutHome.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include ../shared/searchenginelogos.js
  5. // This global tracks if the page has been set up before, to prevent double inits
  6. var gInitialized = false;
  7. var gObserver = new MutationObserver(function (mutations) {
  8. for (let mutation of mutations) {
  9. if (mutation.attributeName == "searchEngineURL") {
  10. setupSearchEngine();
  11. if (!gInitialized) {
  12. gInitialized = true;
  13. }
  14. return;
  15. }
  16. }
  17. });
  18. window.addEventListener("pageshow", function () {
  19. // Delay search engine setup, cause browser.js::BrowserOnAboutPageLoad runs
  20. // later and may use asynchronous getters.
  21. window.gObserver.observe(document.documentElement, { attributes: true });
  22. fitToWidth();
  23. window.addEventListener("resize", fitToWidth);
  24. });
  25. window.addEventListener("pagehide", function() {
  26. window.gObserver.disconnect();
  27. window.removeEventListener("resize", fitToWidth);
  28. });
  29. function onSearchSubmit(aEvent)
  30. {
  31. let searchTerms = document.getElementById("searchText").value;
  32. let searchURL = document.documentElement.getAttribute("searchEngineURL");
  33. if (searchURL && searchTerms.length > 0) {
  34. // Send an event that a search was performed. This was originally
  35. // added so Firefox Health Report could record that a search from
  36. // about:home had occurred.
  37. let engineName = document.documentElement.getAttribute("searchEngineName");
  38. let event = new CustomEvent("AboutHomeSearchEvent", {detail: engineName});
  39. document.dispatchEvent(event);
  40. const SEARCH_TOKEN = "_searchTerms_";
  41. let searchPostData = document.documentElement.getAttribute("searchEnginePostData");
  42. if (searchPostData) {
  43. // Check if a post form already exists. If so, remove it.
  44. const POST_FORM_NAME = "searchFormPost";
  45. let form = document.forms[POST_FORM_NAME];
  46. if (form) {
  47. form.parentNode.removeChild(form);
  48. }
  49. // Create a new post form.
  50. form = document.body.appendChild(document.createElement("form"));
  51. form.setAttribute("name", POST_FORM_NAME);
  52. // Set the URL to submit the form to.
  53. form.setAttribute("action", searchURL.replace(SEARCH_TOKEN, searchTerms));
  54. form.setAttribute("method", "post");
  55. // Create new <input type=hidden> elements for search param.
  56. searchPostData = searchPostData.split("&");
  57. for (let postVar of searchPostData) {
  58. let [name, value] = postVar.split("=");
  59. if (value == SEARCH_TOKEN) {
  60. value = searchTerms;
  61. }
  62. let input = document.createElement("input");
  63. input.setAttribute("type", "hidden");
  64. input.setAttribute("name", name);
  65. input.setAttribute("value", value);
  66. form.appendChild(input);
  67. }
  68. // Submit the form.
  69. form.submit();
  70. } else {
  71. searchURL = searchURL.replace(SEARCH_TOKEN, encodeURIComponent(searchTerms));
  72. window.location.href = searchURL;
  73. }
  74. }
  75. aEvent.preventDefault();
  76. }
  77. function setupSearchEngine()
  78. {
  79. // The "autofocus" attribute doesn't focus the form element
  80. // immediately when the element is first drawn, so the
  81. // attribute is also used for styling when the page first loads.
  82. let searchText = document.getElementById("searchText");
  83. searchText.addEventListener("blur", function searchText_onBlur() {
  84. searchText.removeEventListener("blur", searchText_onBlur);
  85. searchText.removeAttribute("autofocus");
  86. });
  87. let searchEngineName = document.documentElement.getAttribute("searchEngineName");
  88. let searchEngineInfo = SEARCH_ENGINES[searchEngineName];
  89. let logoElt = document.getElementById("searchEngineLogo");
  90. // Add search engine logo.
  91. if (searchEngineInfo && searchEngineInfo.image) {
  92. logoElt.parentNode.hidden = false;
  93. logoElt.src = searchEngineInfo.image;
  94. logoElt.alt = searchEngineName;
  95. searchText.placeholder = "";
  96. } else {
  97. logoElt.parentNode.hidden = false;
  98. logoElt.src = SEARCH_ENGINES['generic'].image;
  99. searchText.placeholder = searchEngineName;
  100. }
  101. }
  102. function fitToWidth() {
  103. if (window.scrollMaxX) {
  104. document.body.setAttribute("narrow", "true");
  105. } else if (document.body.hasAttribute("narrow")) {
  106. document.body.removeAttribute("narrow");
  107. fitToWidth();
  108. }
  109. }