tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WikipediaSuggestions.sys.mjs (2631B)


      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  UrlbarResult: "moz-src:///browser/components/urlbar/UrlbarResult.sys.mjs",
     12  UrlbarUtils: "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs",
     13 });
     14 
     15 /**
     16 * A feature that manages Wikipedia suggestions, both offline (Rust) and online
     17 * (Merino).
     18 */
     19 export class WikipediaSuggestions extends SuggestProvider {
     20  get enablingPreferences() {
     21    return [
     22      "wikipediaFeatureGate",
     23      "suggest.wikipedia",
     24      "suggest.quicksuggest.all",
     25    ];
     26  }
     27 
     28  get primaryUserControlledPreferences() {
     29    return ["suggest.wikipedia"];
     30  }
     31 
     32  get merinoProvider() {
     33    return "wikipedia";
     34  }
     35 
     36  get rustSuggestionType() {
     37    return "Wikipedia";
     38  }
     39 
     40  isSuggestionSponsored() {
     41    return false;
     42  }
     43 
     44  getSuggestionTelemetryType(suggestion) {
     45    // Previously online Wikipedia suggestions were not managed by this feature
     46    // and they had a separate telemetry type, so we carry that forward here.
     47    return suggestion.source == "merino" ? "wikipedia" : "adm_nonsponsored";
     48  }
     49 
     50  makeResult(queryContext, suggestion) {
     51    let { value: title, highlights: titleHighlights } =
     52      lazy.QuickSuggest.getFullKeywordTitleAndHighlights({
     53        tokens: queryContext.tokens,
     54        highlightType: lazy.UrlbarUtils.HIGHLIGHT.SUGGESTED,
     55        // Merino uses snake_case, so this will be `full_keyword` for it.
     56        fullKeyword: suggestion.fullKeyword ?? suggestion.full_keyword,
     57        title: suggestion.title,
     58      });
     59 
     60    return new lazy.UrlbarResult({
     61      type: lazy.UrlbarUtils.RESULT_TYPE.URL,
     62      source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH,
     63      payload: {
     64        url: suggestion.url,
     65        title,
     66        isBlockable: true,
     67        isManageable: true,
     68      },
     69      highlights: {
     70        title: titleHighlights,
     71      },
     72    });
     73  }
     74 
     75  onEngagement(queryContext, controller, details, _searchString) {
     76    let { result } = details;
     77 
     78    // Handle commands. These suggestions support the Dismissal and Manage
     79    // commands. Dismissal is the only one we need to handle here. `UrlbarInput`
     80    // handles Manage.
     81    if (details.selType == "dismiss") {
     82      lazy.QuickSuggest.dismissResult(result);
     83      controller.removeResult(result);
     84    }
     85  }
     86 }