Want more features on Pastebin? Sign Up, it's FREE!
Guest

my aboutHome.js

By: a guest on Aug 12th, 2012  |  syntax: JavaScript  |  size: 13.28 KB  |  views: 227  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.  
  5. // If a definition requires additional params, check that the final search url
  6. // is handled correctly by the engine.
  7. const SEARCH_ENGINES = {
  8.   "Google": {
  9.     image: "data:image/png;base64," +
  10.            "iVBORw0KGgoAAAANSUhEUgAAAEYAAAAcCAYAAADcO8kVAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ" +
  11.            "bWFnZVJlYWR5ccllPAAADHdJREFUeNrsWQl0VNUZvve9NzNJJpnsIkuEJMoqAVJAodCKoFUsAUFQ" +
  12.            "qhig0npaRUE8Viv1FFtQWxSwLXVhEawbhOWobOICFCGiEIIQRGIgCSFjMslsb9567+1/Z+7gmIYK" +
  13.            "Vivt6Ztzz5y5b+7yf//3f/9/38PoW7gYY+i7uDDG39heJfT/q91LGTiTIcWJkCxzxDmCCBGCkBEO" +
  14.            "FDCm5CPs+CGWYvcliRxEzDwgu9I/IzZClonQgT/jC9Eu3GFTz6sdKc57kIzHWKaFjIA2wz++Zhkn" +
  15.            "yblMIDkAFIcDDFcQ+vtjGJuaOlKPkB2G4V4U9kcu8zfWlPtPVX/g9zZ7QwE03jDTqzWVndBUc57a" +
  16.            "Up91gToce0cf3R05El5u6gYyNQ0BKK/x/nNmjKwwxBmx8/eSNHiWsVLXlBJ/7UdTazcN3gn3bYEw" +
  17.            "FmG3pvOobRuScoc+ibEyF6GsUugrgEYuMGD4nqltmJjqFBkt+gcJ/ed0SZIA5crZ+gumrpQ0H319" +
  18.            "ogBFh6aJFoGmQguf2n7tu62HnvgJ1cPBcN3m6dAnX4CM4QAQigmxdQthm9EEJ58bY3bOl/CQ2YE5" +
  19.            "pu24LdBwZE7De+M+4gBAs/IntETphOHD4FOzNoNPbjuzBkn+48/9qKXywWPcM99Edvh2siPfHeyc" +
  20.            "nH8mU/pM2pJLsfshI0KCNRv7viiYYXW7sRnmxTFQhCp3G9/CTqzLsht3jtkrmGJdgGF0xmYpQx5G" +
  21.            "KBEInWdWSs4pnm6bLD3i95WJsDG7jmtiXFYwlmF2WXATmCPROE05IGa3G33sxPrsL014tGRMVo5D" +
  22.            "uVdirD/8zJBluQgC9qSF2JKcV9cuPwudsbq1YLqCydjYGOkSngYtKq36vJUs6jqhuqXtgCvursty" +
  23.            "uHOnSZIMWROnc/dR2J5pYAZO3tF0rOwvAXI/jvKZ/vN6zVNuHQGWjYNx/SWGiohtH9R1Y17HDRvf" +
  24.            "4XtUCEoaQwyGbEOr5QZ3HeeLbRwrosnRNB5lHNwpuBn+HK2KWFsLcd34scWpGJd5g6Ener61faoQ" +
  25.            "bOXk6OsWpycnP98yYdzMrLINxYks+3h1fvZlHfE6M6LXu0oa4mPko8s7TL70kuSnOmVIMxvW5n2v" +
  26.            "00111fF1htzXWiwpnrJAw8FbD60qXtHn9o9LUrJ6r2CUBoOnDpQeKxu0ncPhntgRwKLRcErUVd9t" +
  27.            "k1falinlvLLmLr7WHfndsh/t0WOdg9Dt1cOHTyrctWutRGzH5ZbNjcQ0FpEce+lMQwCnpMRqnSQ3" +
  28.            "Qu50hFIzMXJnSsjt+aI+fG/kiOwUStcFQuG9AMor0GUI0da6btoyKxIKnWKaXlR/zajFCYWlXNBB" +
  29.            "WslMKz+tpOEezkIxJtJzuvfl5ia1DCiQnuki6+MiXzRlR47s9Lwdaa1bCKAc4uscXnX5mwFvzdO6" +
  30.            "JnlQSv8lgiOUERZ1QYLG4PqJE+ZItl2y4MDB3wjma8/XnGiuavSuUMNhKNOshdyZkmViD7EAGBrX" +
  31.            "K9gzA1CYqPZEfEoAEK91eN3jTELIlRT7jnuhm9M5mxrmJZVNvjUio0VEC3Exr2ryLTbVCJI0/ZfL" +
  32.            "e/TI5ZusfbXbKAcjP2706msTQRHiH3pxa2ghgIlkU+9b91zqRA6OK6MIQh+nG8HP6wT4PPzD3n3z" +
  33.            "lxoRiohl5eVd/1G/qC2Ug8LBOcMYh5PYd6mqemTRJ8d88axb3r//NTkYT2tQ1e27W3yzo+aamh0k" +
  34.            "NoWIcfeJ1Ss8A2EU0xgqflEkYQBGBuYAe3hByAHiNVBcqyRdLzEjYLhpEGFk/CaHXFtZX79RD4WR" +
  35.            "Bl4plOWR3MhkbI0DMOHfFhNjaEK6Neas1D9Rg3qVHQFwLHIV9DkN01miaxD6LNUjQpKPMQLHl522" +
  36.            "jWAVtQxELTM7agBN+AdcGwYNvJREtDwjrOL5hQWpVf36TTtcVFRhGMaAlxsbpw+prCwt/fRTHoZE" +
  37.            "MVS1Sna5r5CUpKExisc0RVFix4BoKEFHlDES78dIcYjdf0FRhapqH5tQxAyTtiOwZHVTk3dWdnaV" +
  38.            "zFgv27a5RzfKlt6PAiOZFQWmrUTy2Y3WFntPdgruhXVWxIFRA2ZIBq9QqeP18PvlBPAtRq0gHGNQ" +
  39.            "uHbN4ej+qJDDmMZIaaZZYASC/MzTe1RScmmdqlZce/z4CLFfW7RoppWsSP1Wy7R5NeTpfMNnU+s2" +
  40.            "pGIZ2KC4oEGoOOCb/7aNpkKbWKsswhhoUrQZBmPdp/hXcWDUQCjIGZFByLB2Su9ogaUaRhAa8hsG" +
  41.            "DxXFCmlB8CBKleyhZynXiWkwv6VRpEVYkBtnBGq28bMPZcmjC0rKCxPLFqy4GDWbVwSOPemLGhvP" +
  42.            "SMJNlc2+es0fQGYo5HnH59sCoMQLWVU0LV4ISqHjf/obtbQQxCbMnPngRcM25MbCB5giDo+Hl6Xg" +
  43.            "qtVd6yqWeu7e91RyR++Rd28OthAUaLZRa+0Rrg+SNxQqD0dDyRx9lmqY6brOVDi7HFHV9/mWvV5z" +
  44.            "r63aSCF0yDOlcla7NZrFmA3AeH2E1052/ebi1ZZ6ej3oh8eZ2fe1vtPqOTi495SaHygOOc1/dOFj" +
  45.            "QnsYhdMw44lFaMysU6dOBCBvRcCB35fl+0X4am3COCaakdoVjVaoZgW1dESJnSd5hiz/7NU02Qbd" +
  46.            "4dpDYdLL7wizOLW5OGoRTAM+G0VCBrg0yDOMXRGJPB8GNpim2efF7Ozi9hgA4Hfxm0b53NbW/Zyy" +
  47.            "i7bQlyJBFjIjDF1ViKe29xhEJizP0Flw6S76klhfrX+j8C7dt/8BPRxpsGnGyqKfGRQ7O20OVr80" +
  48.            "NVT9bIMIBwhrygMsLr7RcKvT9bUq1zXLumVtdvaAs56V+GK+3UMXEK15HzU1jvANHa47/YIGJ2cT" +
  49.            "DmAWSIZtUdT9tiDpNjEQpZ1pJpumqiKih0AfSHTB2X7/2w2GsT4CNM8k5NlnPJ7Eyg+vT0+faVqW" +
  50.            "Z2tEu1cYaC3fQxsPnaS/swAYN2K/qnhQHpgAKC6/Xx6Qgtmkilo2Z9WHrFHQnO/Bf/rtoctPlOVM" +
  51.            "az35/pKIyhCAh6SUQre4H/M+L7lAqJl+RvKsVeHw0pBlntJME2VQunVzRsaERCfuyMzMfyszMzN+" +
  52.            "ak52XTQ2333prxdJzuyRXGSw7KjFEnlUwYF1zrROLbxO4umwcVOWkjV0z51YyXqaEQsR9djYQMX4" +
  53.            "TTwVQst8NiVlPqS+Upj0EAyZB9+tcB4ZByJ71V5C7ntcj550Q4KBTl7pvjFVmtbnYvSQ7ACcEZoD" +
  54.            "fTUwbgDE490fN6B5o5fRjdAXiDNBGKLwNVMLZnTJLPrDh1hypAFHAkTzXnNqc+GHfG75oYxVYN0k" +
  55.            "YEwQXPEAcuF9ZIH/01ku1/ChivJHkNCeMk8sCNXChCdhQr7+6uvC4RU4d8RJ1PRuV64JKdDSU3su" +
  56.            "HuHMuKJUcuWMhMU4QHwflWBHgFEb4tXuSs3gEaLV7bdDlXvU6rm7hKH8SobmmawohUNkeSDUghdD" +
  57.            "0vfXMrbnYdOoSij6Eg108TFje6EOMwbjwZ0zUHeXA5GGANoz6jm2VwCotikBcN7YpvHEtvrDnoqh" +
  58.            "t58kuzpDJcoPhQDO6YGn3+pTK/007QYUoClgOUHpWAUuldPV4VYYn8rXfMDpHN4NS4McOBpsJ7fZ" +
  59.            "9utrbNvLWYdzrq5H3PO+Hfmy8GCKaI7U7o/3wq6ObklOIkhykcD+sbuFMeKAcKYos8RvSczhEgLM" +
  60.            "EioJknDoTEznWLDNJb5RO2POPBfqf2frdFN3LAz6Im+agU9e+Xzn8HLod+dcueXnDk/vX2DZlQaK" +
  61.            "/ebpLV0miPmcCXs1xZySWC9JMA/Fz3/CeXZbgcTCIEVMqiSAkFguxQ0mX06IX9KueIuPpV/xPCS+" +
  62.            "ttQGnDMs6Tej8SaseF4LN9c9cnxNj6VxI8Q+3em9Hx+c3PmW1UDztMZtXVLEfdymbGAJ60kJGZQm" +
  63.            "tH99bE8YGN/wd/mgxdG7NFDb8/ZohryYA5HguHhI5uYO27vyoqtrmAiXr31JX/V48CuY8R8FJhxE" +
  64.            "eeEAQWk9HnYlFmMJoRKG03QLtUJ7/93FvpXXJ7wM/6Za4l71UEu5pWkoucv0Be0tm95vmUdy5t5k" +
  65.            "tpbPbe8B2vmsi7+rl2Nf4yVaUlLHSQXu7r8tw1JyT+ivhQBaAhZUxBSC5EPpPtMKVDzi3z/+HZHJ" +
  66.            "7K/7IvC/CRhZ6Ep6evGGyXJS3kAsp3SGcgLKc7uSktBhrW7ZFq32r/HHCVbb0P9fBSYOTpIoJ5SE" +
  67.            "7GUnpHbrbG8EzsfWfwgwAEfC/ToQIhkhAAAAAElFTkSuQmCC"
  68.   , params: "source=hp&channel=np"
  69.   }
  70.  
  71. , "Яндекс":
  72.   {
  73.     image: "data:image/png;base64," +
  74.            "iVBORw0KGgoAAAANSUhEUgAAAEYAAAAcCAYAAADcO8kVAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ" +
  75.            "bWFnZVJlYWR5ccllPAAABWFJREFUeNrsWWtsVEUUnltLH7tbaeuDbfCBojUoBTGmooLE+Igx+gON" +
  76.            "RvEJhEQNUdEYA0Ji4xNf2Bg1iBJJrGBC+CEBNYoxxmh94CMKRE2MitBqoZRi6bbdZT3TfhM/TmZ2" +
  77.            "u5jGsOEkX8/0ztzp3HPP4zu3UTabNUfEI9YwgzAxjUbBGkG7IAv0CwYE53rWC+KChFloRh329igN" +
  78.            "zD8keJR+P2DvEbgnrjp4eWT65GerSZuU6FWii9Fj5pGHvC6ow/WpdP1C7SV3Bm18eNpDG2a0oA0P" +
  79.            "v0qFSn3IMPOKxChsmBJ1/TpBEuNn1NxRB8XOoJSYRabfrCiG0FGiDXMZ9HeC73PfGpkOST0vmYGi" +
  80.            "LEraMCdB/5jP46xhnhaj7C3Sal2qjFSDcU8eb4m2m4xpHvKWYwSTBd2Cr1HBrIwVnCXYIdiiNrDh" +
  81.            "Wi8YQLVzZ+mDt/ar9acK5gqOE6wTvKvmE4JzsN83ghSu1+AMMcGngr/pnnHYM4nzrRX8EapKm5Fc" +
  82.            "3/bwlAn/Jt/EtJdNmdvidjxcpyrjT+D6Fx7LPoA5jf3ktU5metY9rtZcRHNn0vV3cO0rtf6GwN9v" +
  83.            "DCXfX6AbVLL1hJJOxIM6UtwnJG7ORuIaMl5W7W297g2MmwR3YLxQcDmty3jOdongCrrXyRTBaoyf" +
  84.            "x5qdgjZ4qzfHbCQ3mzXcChcYH8hhIGf0zwQ3Ch6k8/Ae9yEM3hc8LFguWIm5uwIvwYXhPdA2RNbT" +
  85.            "/BLoFsECwXsw1gUIZa9h7NvZivGLgkk010eHjv5jbitXD1HiWVMhuB7jDXR9E/R0Qa3nPvvmTxZc" +
  86.            "7fGWyQhNK6/R9b8Ev4aSr0HyunWQ3Q/li8/hdh8JTiOD+DpPa7jegHtriUN35zDMRMEJGH9J17dB" +
  87.            "18KzO9V9NvndjbH1sB9objp0u+CT4VYlJ5txKLvpDMFsIJ/EwYOs9bsEp+RYeyz0nx7y6ORsGu8K" +
  88.            "EM2kx1ts7rkXL+YxNd8I/TOcoCDDOB5jY/Fj/P4cEmVTjr0SlKNCOcjJ8fQgodAcQ/d/i/BLK8Oo" +
  89.            "ZtYcLVgGD1wq2K7mx0LvKITHaFlCbny/oI4M43uQDJJkL3KH5RWnB/auh96ax9AGnKQdoZNAyO4T" +
  90.            "VHv4VobC+XzPntWUMgpivtwzufbgWbVpSHYh4V0DnrA6YETrCWdgvGUYIboX9KEahqlFcq0GT2HZ" +
  91.            "jwrXBW4zJ/C8FYdqmEWUb94aZniUUbXJVbmm0N6/5zjbPnohcfKePiDlSfBJeO0r9Bx8pi7oEw/F" +
  92.            "MPMp8S0roARHar+QYS6FXp9nv230dicVcA7LaZoxHo/ncfIbEdi6Qgxje4vFRL5aRqA/uxn6Vc9c" +
  93.            "muK/lXqeuQXsPwZMdi0RPedxH1AFva0QwyygavDkCBjlFuy/HJWhksLQgOVyxWqh3mYx7RND2Pi8" +
  94.            "0n1+baawmU9e2o6x/XR7raIQVb4mskGQQaO4ydNENlATeTE1kXOQc/agXDpZqhq42dQL2US9G1Wl" +
  95.            "G5XEzaWJbyTBddzcTuSmAYTMOKybQWsmeppIbk5nqcbxJ1RHO37B10TeRL3KU543kUKF0J8leqgq" +
  96.            "8ae8PdAd6ltPL954LXQV/m4HEbgaYqjT6KNZHWhAKd5+mzpDN4WflUdw5koweitv4lldX2QpxQSc" +
  97.            "/UOfx9jvvTHBKP+/RmKRoHwIiYg8pgQJsszTKFYSV2qC0VcShyqnqlEKRpolqsAyFfnpKmLOnOgr" +
  98.            "VAVirhYnYzsZLbgSe57nwtL375N8H+Oy3H2qKpAKEL5eVc65E04rD2NW66uWrUDobKnAnPs7PR5+" +
  99.            "tLFQHjMS0knhEZLdim/8bxId+RetX/4RYACXlwEEPBQycwAAAABJRU5ErkJggg=="
  100.   }
  101. };
  102.  
  103. // The process of adding a new default snippet involves:
  104. //   * add a new entity to aboutHome.dtd
  105. //   * add a <span/> for it in aboutHome.xhtml
  106. //   * add an entry here in the proper ordering (based on spans)
  107. // The <a/> part of the snippet will be linked to the corresponding url.
  108. const DEFAULT_SNIPPETS_URLS = [
  109.   "http://www.mozilla.com/firefox/features/?WT.mc_ID=default1"
  110. , "https://addons.mozilla.org/firefox/?src=snippet&WT.mc_ID=default2"
  111. ];
  112.  
  113. const SNIPPETS_UPDATE_INTERVAL_MS = 86400000; // 1 Day.
  114.  
  115. let gSearchEngine;
  116.  
  117. document.addEventListener("DOMContentLoaded", function init() {
  118.   setupSearchEngine();
  119.   loadSnippets();
  120. });
  121. window.addEventListener("load", fitToWidth);
  122. window.addEventListener("resize", fitToWidth);
  123.  
  124.  
  125. function onSearchSubmit(aEvent)
  126. {
  127.   let searchTerms = document.getElementById("searchText").value;
  128.   if (gSearchEngine && searchTerms.length > 0) {
  129.     const SEARCH_TOKENS = {
  130.       "_searchTerms_": encodeURIComponent(searchTerms)
  131.     }
  132.     let url = gSearchEngine.searchUrl;
  133.     for (let key in SEARCH_TOKENS) {
  134.       url = url.replace(key, SEARCH_TOKENS[key]);
  135.     }
  136.     window.location.href = url;
  137.   }
  138.  
  139.   aEvent.preventDefault();
  140. }
  141.  
  142.  
  143. function setupSearchEngine()
  144. {
  145.   gSearchEngine = JSON.parse(localStorage["search-engine"]);
  146.  
  147.   if (!gSearchEngine)
  148.     return;
  149.  
  150.   // Look for extended information, like logo and links.
  151.   let searchEngineInfo = SEARCH_ENGINES[gSearchEngine.name];
  152.   if (searchEngineInfo) {
  153.     for (let prop in searchEngineInfo)
  154.       gSearchEngine[prop] = searchEngineInfo[prop];
  155.   }
  156.  
  157.   // Enqueue additional params if required by the engine definition.
  158.   if (gSearchEngine.params)
  159.     gSearchEngine.searchUrl += "&" + gSearchEngine.params;
  160.  
  161.   // Add search engine logo.
  162.   if (gSearchEngine.image) {
  163.     let logoElt = document.getElementById("searchEngineLogo");
  164.     logoElt.src = gSearchEngine.image;
  165.     logoElt.alt = gSearchEngine.name;
  166.   }
  167.  
  168.   // The "autofocus" attribute doesn't focus the form element
  169.   // immediately when the element is first drawn, so the
  170.   // attribute is also used for styling when the page first loads.
  171.   let searchText = document.getElementById("searchText");
  172.   searchText.addEventListener("blur", function searchText_onBlur() {
  173.     searchText.removeEventListener("blur", searchText_onBlur);
  174.     searchText.removeAttribute("autofocus");
  175.   });
  176.  
  177. }
  178.  
  179. function loadSnippets()
  180. {
  181.   // Check last snippets update.
  182.   let lastUpdate = localStorage["snippets-last-update"];
  183.   let updateURL = localStorage["snippets-update-url"];
  184.   if (updateURL && (!lastUpdate ||
  185.                     Date.now() - lastUpdate > SNIPPETS_UPDATE_INTERVAL_MS)) {
  186.     // Try to update from network.
  187.     let xhr = new XMLHttpRequest();
  188.     try {
  189.       xhr.open("GET", updateURL, true);
  190.     } catch (ex) {
  191.       showSnippets();
  192.       return;
  193.     }
  194.     // Even if fetching should fail we don't want to spam the server, thus
  195.     // set the last update time regardless its results.  Will retry tomorrow.
  196.     localStorage["snippets-last-update"] = Date.now();
  197.     xhr.onerror = function (event) {
  198.       showSnippets();
  199.     };
  200.     xhr.onload = function (event)
  201.     {
  202.       if (xhr.status == 200) {
  203.         localStorage["snippets"] = xhr.responseText;
  204.       }
  205.       showSnippets();
  206.     };
  207.     xhr.send(null);
  208.   } else {
  209.     showSnippets();
  210.   }
  211. }
  212.  
  213. function showSnippets()
  214. {
  215.   let snippetsElt = document.getElementById("snippets");
  216.   let snippets = localStorage["snippets"];
  217.   // If there are remotely fetched snippets, try to to show them.
  218.   if (snippets) {
  219.     // Injecting snippets can throw if they're invalid XML.
  220.     try {
  221.       snippetsElt.innerHTML = snippets;
  222.       // Scripts injected by innerHTML are inactive, so we have to relocate them
  223.       // through DOM manipulation to activate their contents.
  224.       Array.forEach(snippetsElt.getElementsByTagName("script"), function(elt) {
  225.         let relocatedScript = document.createElement("script");
  226.         relocatedScript.type = "text/javascript;version=1.8";
  227.         relocatedScript.text = elt.text;
  228.         elt.parentNode.replaceChild(relocatedScript, elt);
  229.       });
  230.       return;
  231.     } catch (ex) {
  232.       // Bad content, continue to show default snippets.
  233.     }
  234.   }
  235.  
  236.   // Show default snippets otherwise.
  237.   let defaultSnippetsElt = document.getElementById("defaultSnippets");
  238.   let entries = defaultSnippetsElt.querySelectorAll("span");
  239.   // Choose a random snippet.  Assume there is always at least one.
  240.   let randIndex = Math.floor(Math.random() * entries.length);
  241.   let entry = entries[randIndex];
  242.   // Inject url in the eventual link.
  243.   if (DEFAULT_SNIPPETS_URLS[randIndex]) {
  244.     let links = entry.getElementsByTagName("a");
  245.     // Default snippets can have only one link, otherwise something is messed
  246.     // up in the translation.
  247.     if (links.length == 1) {
  248.       links[0].href = DEFAULT_SNIPPETS_URLS[randIndex];
  249.     }
  250.   }
  251.   // Move the default snippet to the snippets element.
  252.   snippetsElt.appendChild(entry);
  253. }
  254.  
  255. function fitToWidth() {
  256.   if (window.scrollMaxX) {
  257.     document.body.setAttribute("narrow", "true");
  258.   } else if (document.body.hasAttribute("narrow")) {
  259.     document.body.removeAttribute("narrow");
  260.     fitToWidth();
  261.   }
  262. }
clone this paste RAW Paste Data