MDNSuggestions.sys.mjs (4336B)
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 import { SuggestProvider } from "moz-src:///browser/components/urlbar/private/SuggestFeature.sys.mjs"; 6 7 const lazy = {}; 8 9 ChromeUtils.defineESModuleGetters(lazy, { 10 QuickSuggest: "moz-src:///browser/components/urlbar/QuickSuggest.sys.mjs", 11 UrlbarPrefs: "moz-src:///browser/components/urlbar/UrlbarPrefs.sys.mjs", 12 UrlbarResult: "moz-src:///browser/components/urlbar/UrlbarResult.sys.mjs", 13 UrlbarUtils: "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs", 14 }); 15 16 const RESULT_MENU_COMMAND = { 17 MANAGE: "manage", 18 NOT_INTERESTED: "not_interested", 19 NOT_RELEVANT: "not_relevant", 20 }; 21 22 /** 23 * A feature that supports MDN suggestions. 24 */ 25 export class MDNSuggestions extends SuggestProvider { 26 get enablingPreferences() { 27 return ["mdn.featureGate", "suggest.mdn", "suggest.quicksuggest.all"]; 28 } 29 30 get primaryUserControlledPreferences() { 31 return ["suggest.mdn"]; 32 } 33 34 get merinoProvider() { 35 return "mdn"; 36 } 37 38 get rustSuggestionType() { 39 return "Mdn"; 40 } 41 42 async makeResult(queryContext, suggestion) { 43 if (!this.isEnabled) { 44 // The feature is disabled on the client, but Merino may still return 45 // mdn suggestions anyway, and we filter them out here. 46 return null; 47 } 48 49 const url = new URL(suggestion.url); 50 url.searchParams.set("utm_medium", "firefox-desktop"); 51 url.searchParams.set("utm_source", "firefox-suggest"); 52 url.searchParams.set( 53 "utm_campaign", 54 "firefox-mdn-web-docs-suggestion-experiment" 55 ); 56 url.searchParams.set("utm_content", "treatment"); 57 58 return new lazy.UrlbarResult({ 59 type: lazy.UrlbarUtils.RESULT_TYPE.URL, 60 source: lazy.UrlbarUtils.RESULT_SOURCE.OTHER_NETWORK, 61 isBestMatch: true, 62 showFeedbackMenu: true, 63 payload: { 64 icon: "chrome://global/skin/icons/mdn.svg", 65 url: url.href, 66 originalUrl: suggestion.url, 67 title: suggestion.title, 68 description: suggestion.description, 69 shouldShowUrl: true, 70 bottomTextL10n: { 71 id: "firefox-suggest-mdn-bottom-text", 72 }, 73 }, 74 highlights: { 75 title: lazy.UrlbarUtils.HIGHLIGHT.TYPED, 76 }, 77 }); 78 } 79 80 /** 81 * Gets the list of commands that should be shown in the result menu for a 82 * given result from the provider. All commands returned by this method should 83 * be handled by implementing `onEngagement()` with the possible exception of 84 * commands automatically handled by the urlbar, like "help". 85 */ 86 getResultCommands() { 87 return /** @type {UrlbarResultCommand[]} */ ([ 88 { 89 l10n: { 90 id: "firefox-suggest-command-dont-show-mdn", 91 }, 92 children: [ 93 { 94 name: RESULT_MENU_COMMAND.NOT_RELEVANT, 95 l10n: { 96 id: "firefox-suggest-command-not-relevant", 97 }, 98 }, 99 { 100 name: RESULT_MENU_COMMAND.NOT_INTERESTED, 101 l10n: { 102 id: "firefox-suggest-command-not-interested", 103 }, 104 }, 105 ], 106 }, 107 { name: "separator" }, 108 { 109 name: RESULT_MENU_COMMAND.MANAGE, 110 l10n: { 111 id: "urlbar-result-menu-manage-firefox-suggest", 112 }, 113 }, 114 ]); 115 } 116 117 onEngagement(queryContext, controller, details, _searchString) { 118 let { result } = details; 119 switch (details.selType) { 120 case RESULT_MENU_COMMAND.MANAGE: 121 // "manage" is handled by UrlbarInput, no need to do anything here. 122 break; 123 // selType == "dismiss" when the user presses the dismiss key shortcut. 124 case "dismiss": 125 case RESULT_MENU_COMMAND.NOT_RELEVANT: 126 lazy.QuickSuggest.dismissResult(result); 127 result.acknowledgeDismissalL10n = { 128 id: "firefox-suggest-dismissal-acknowledgment-one-mdn", 129 }; 130 controller.removeResult(result); 131 break; 132 case RESULT_MENU_COMMAND.NOT_INTERESTED: 133 lazy.UrlbarPrefs.set("suggest.mdn", false); 134 result.acknowledgeDismissalL10n = { 135 id: "firefox-suggest-dismissal-acknowledgment-all-mdn", 136 }; 137 controller.removeResult(result); 138 break; 139 } 140 } 141 }