omnibox.onInputEntered 编辑

Fired when the user has selected one of the suggestions your extension has added to the address bar's drop-down list.

Use this event to handle the user's selection, generally by opening the corresponding page. The event listener is passed:

  • the user's selection
  • a omnibox.OnInputEnteredDisposition: use this to determine whether to open the new page in the current tab, in a new foreground tab, or in a new background tab.

Syntax

browser.omnibox.onInputEntered.addListener(listener)
browser.omnibox.onInputEntered.removeListener(listener)
browser.omnibox.onInputEntered.hasListener(listener)

Events have three functions:

addListener(listener)
Adds a listener to this event.
removeListener(listener)
Stop listening to this event. The listener argument is the listener to remove.
hasListener(listener)
Check whether listener is registered for this event. Returns true if it is listening, false otherwise.

addListener syntax

The listener function will be passed two parameters: a string text, and an omnibox.OnInputEnteredDisposition.

Parameters

text

String. This is the value of the content property of the omnibox.SuggestResult object that the user selected.

disposition

OnInputEnteredDisposition. A omnibox.OnInputEnteredDisposition enumeration, indicating whether the extension should open the page in the current tab, in a new foreground tab, or in a new background tab.

Browser compatibility

BCD tables only load in the browser

Examples

This example interprets the user's input as a CSS property name and populates the drop-down list with one omnibox.SuggestResult object for each CSS property matching the input. The SuggestResult description is the full name of the property, and the content is the MDN page for that property.

The example also listens to omnibox.onInputEntered, and opens the MDN page corresponding to the selection, according to the  omnibox.OnInputEnteredDisposition argument.

browser.omnibox.setDefaultSuggestion({
  description: "Type the name of a CSS property"
});

/*
Very short list of a few CSS properties.
*/
const props = [
  "animation",
  "background",
  "border",
  "box-shadow",
  "color",
  "display",
  "flex",
  "flex",
  "float",
  "font",
  "grid",
  "margin",
  "opacity",
  "overflow",
  "padding",
  "position",
  "transform",
  "transition"
];

const baseURL = "/wiki/en-US/docs/Web/CSS/";

/*
Return an array of SuggestResult objects,
one for each CSS property that matches the user's input.
*/
function getMatchingProperties(input) {
  var result = [];
  for (prop of props) {
    if (prop.indexOf(input) === 0) {
      console.log(prop);
      let suggestion = {
        content: baseURL + prop,
        description: prop
      }
      result.push(suggestion);
    } else {
      if (result.length != 0) {
        return result;
      }
    }
  }
  return result;
}

browser.omnibox.onInputChanged.addListener((input, suggest) => {
  suggest(getMatchingProperties(input));
});

browser.omnibox.onInputEntered.addListener((url, disposition) => {
  switch (disposition) {
    case "currentTab":
      browser.tabs.update({url});
      break;
    case "newForegroundTab":
      browser.tabs.create({url});
      break;
    case "newBackgroundTab":
      browser.tabs.create({url, active: false});
      break;
  }
});

Example extensions

Acknowledgements

This API is based on Chromium's chrome.omnibox API.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:59 次

字数:6258

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文