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/. */
6 * This module exports a provider that offers bookmarks with keywords.
12 } from "resource:///modules/UrlbarUtils.sys.mjs";
16 ChromeUtils.defineESModuleGetters(lazy, {
17 KeywordUtils: "resource://gre/modules/KeywordUtils.sys.mjs",
18 UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs",
22 * Class used to create the provider.
24 class ProviderBookmarkKeywords extends UrlbarProvider {
26 * Returns the name of this provider.
28 * @returns {string} the name of this provider.
31 return "BookmarkKeywords";
35 * Returns the type of this provider.
37 * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.*
40 return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
44 * Whether this provider should be invoked for the given context.
45 * If this method returns false, the providers manager won't start a query
46 * with this provider, to save on resources.
48 * @param {UrlbarQueryContext} queryContext The query context object
49 * @returns {boolean} Whether this provider should be invoked for the search.
51 isActive(queryContext) {
53 (!queryContext.restrictSource ||
54 queryContext.restrictSource == UrlbarUtils.RESULT_SOURCE.BOOKMARKS) &&
55 !queryContext.searchMode &&
56 queryContext.tokens.length
63 * @param {object} queryContext The query context object
64 * @param {Function} addCallback Callback invoked by the provider to add a new
67 async startQuery(queryContext, addCallback) {
68 let keyword = queryContext.tokens[0]?.value;
70 let searchString = UrlbarUtils.substringAfter(
71 queryContext.searchString,
74 let { entry, url, postData } = await lazy.KeywordUtils.getBindableKeyword(
83 if (entry.url.host && searchString) {
84 // If we have a search string, the result has the title
85 // "host: searchString".
86 title = UrlbarUtils.strings.formatStringFromName(
87 "bookmarkKeywordSearch",
97 title = UrlbarUtils.prepareUrlForDisplay(url);
100 let result = new lazy.UrlbarResult(
101 UrlbarUtils.RESULT_TYPE.KEYWORD,
102 UrlbarUtils.RESULT_SOURCE.BOOKMARKS,
103 ...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, {
104 title: [title, UrlbarUtils.HIGHLIGHT.TYPED],
105 url: [url, UrlbarUtils.HIGHLIGHT.TYPED],
106 keyword: [keyword, UrlbarUtils.HIGHLIGHT.TYPED],
107 input: queryContext.searchString,
109 icon: UrlbarUtils.getIconForUrl(entry.url),
112 result.heuristic = true;
113 addCallback(this, result);
117 export var UrlbarProviderBookmarkKeywords = new ProviderBookmarkKeywords();