SearchShortcuts.jsm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. "use strict";
  5. const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
  6. // List of sites we match against Topsites in order to identify sites
  7. // that should be converted to search Topsites
  8. const SEARCH_SHORTCUTS = [
  9. {keyword: "@\u767E\u5EA6", shortURL: "baidu", url: "https://baidu.com"},
  10. {keyword: "@duckduckgo", shortURL: "duckduckgo", url: "https://duckduckgo.com"},
  11. {keyword: "@\u044F\u043D\u0434\u0435\u043A\u0441", shortURL: "yandex", url: "https://yandex.com"},
  12. ];
  13. this.SEARCH_SHORTCUTS = SEARCH_SHORTCUTS;
  14. // These can be added via the editor but will not be added organically
  15. this.CUSTOM_SEARCH_SHORTCUTS = [
  16. ...SEARCH_SHORTCUTS,
  17. {keyword: "@duckduckgo", shortURL: "duckduckgo", url: "https://duckduckgo.com"},
  18. {keyword: "@wikipedia", shortURL: "wikipedia", url: "https://wikipedia.org"},
  19. ];
  20. // Note: you must add the activity stream branch to the beginning of this if using outside activity stream
  21. this.SEARCH_SHORTCUTS_EXPERIMENT = "improvesearch.topSiteSearchShortcuts";
  22. this.SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF = "improvesearch.topSiteSearchShortcuts.searchEngines";
  23. this.SEARCH_SHORTCUTS_HAVE_PINNED_PREF = "improvesearch.topSiteSearchShortcuts.havePinned";
  24. function getSearchProvider(candidateShortURL) {
  25. return SEARCH_SHORTCUTS.filter(match => candidateShortURL === match.shortURL)[0] || null;
  26. }
  27. this.getSearchProvider = getSearchProvider;
  28. // Check topsite against predefined list of valid search engines
  29. // https://searchfox.org/mozilla-central/rev/ca869724246f4230b272ed1c8b9944596e80d920/toolkit/components/search/nsSearchService.js#939
  30. async function checkHasSearchEngine(keyword) {
  31. return (await Services.search.getDefaultEngines())
  32. .find(e => e.wrappedJSObject._internalAliases.includes(keyword));
  33. }
  34. this.checkHasSearchEngine = checkHasSearchEngine;
  35. const EXPORTED_SYMBOLS = ["checkHasSearchEngine", "getSearchProvider", "SEARCH_SHORTCUTS", "CUSTOM_SEARCH_SHORTCUTS", "SEARCH_SHORTCUTS_EXPERIMENT",
  36. "SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF", "SEARCH_SHORTCUTS_HAVE_PINNED_PREF"];