tor-browser

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

UrlbarProviderBookmarkKeywords.sys.mjs (3007B)


      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 /**
      6 * This module exports a provider that offers bookmarks with keywords.
      7 */
      8 
      9 import {
     10  UrlbarProvider,
     11  UrlbarUtils,
     12 } from "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs";
     13 
     14 const lazy = {};
     15 
     16 ChromeUtils.defineESModuleGetters(lazy, {
     17  KeywordUtils: "resource://gre/modules/KeywordUtils.sys.mjs",
     18  UrlbarResult: "moz-src:///browser/components/urlbar/UrlbarResult.sys.mjs",
     19 });
     20 
     21 /**
     22 * Class used to create the provider.
     23 */
     24 export class UrlbarProviderBookmarkKeywords extends UrlbarProvider {
     25  /**
     26   * @returns {Values<typeof UrlbarUtils.PROVIDER_TYPE>}
     27   */
     28  get type() {
     29    return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
     30  }
     31 
     32  /**
     33   * Whether this provider should be invoked for the given context.
     34   * If this method returns false, the providers manager won't start a query
     35   * with this provider, to save on resources.
     36   *
     37   * @param {UrlbarQueryContext} queryContext The query context object
     38   */
     39  async isActive(queryContext) {
     40    return (
     41      (!queryContext.restrictSource ||
     42        queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.BOOKMARKS) &&
     43      !queryContext.searchMode &&
     44      !!queryContext.tokens.length
     45    );
     46  }
     47 
     48  /**
     49   * Starts querying.
     50   *
     51   * @param {UrlbarQueryContext} queryContext
     52   * @param {(provider: UrlbarProvider, result: UrlbarResult) => void} addCallback
     53   *   Callback invoked by the provider to add a new result.
     54   */
     55  async startQuery(queryContext, addCallback) {
     56    let keyword = queryContext.tokens[0]?.value;
     57 
     58    let searchString = UrlbarUtils.substringAfter(
     59      queryContext.searchString,
     60      keyword
     61    ).trim();
     62    let { entry, url, postData } = await lazy.KeywordUtils.getBindableKeyword(
     63      keyword,
     64      searchString
     65    );
     66    if (!entry || !url) {
     67      return;
     68    }
     69 
     70    let title;
     71    if (entry.url.host && searchString) {
     72      // If we have a search string, the result has the title
     73      // "host: searchString".
     74      title = UrlbarUtils.strings.formatStringFromName(
     75        "bookmarkKeywordSearch",
     76        [
     77          entry.url.host,
     78          queryContext.tokens
     79            .slice(1)
     80            .map(t => t.value)
     81            .join(" "),
     82        ]
     83      );
     84    } else {
     85      title = UrlbarUtils.prepareUrlForDisplay(url);
     86    }
     87 
     88    let result = new lazy.UrlbarResult({
     89      type: UrlbarUtils.RESULT_TYPE.KEYWORD,
     90      source: UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
     91      heuristic: true,
     92      payload: {
     93        title,
     94        url,
     95        keyword,
     96        input: queryContext.searchString,
     97        postData,
     98        icon: UrlbarUtils.getIconForUrl(entry.url),
     99      },
    100      highlights: {
    101        title: UrlbarUtils.HIGHLIGHT.TYPED,
    102        url: UrlbarUtils.HIGHLIGHT.TYPED,
    103        keyword: UrlbarUtils.HIGHLIGHT.TYPED,
    104      },
    105    });
    106    addCallback(this, result);
    107  }
    108 }