User actions 编辑

Some WebExtension APIs perform functions that are generally performed as a result of a user action. For example:

  • a browser action with a popup will display the popup when the user clicks it, but there's also a browserAction.openPopup API enabling an extension to open the popup programmatically.
  • if an extension adds a sidebar, it is typically opened by the user via some part of the browser's built-in user interface, such as the View/Sidebar menu. But there's also a sidebarAction.open API enabling an extension to open their sidebar programmatically.

To follow the principle of "no surprises", APIs like this can only be called from inside the handler for a user action. User actions include the following:

  • Clicking the extension's browser action or page action.
  • Selecting a context menu item defined by the extension.
  • Activating a keyboard shortcut defined by the extension (this only treated as a user action from Firefox 63 onwards).
  • Clicking a button in a page bundled with the extension.

For example:

function handleClick() {
  browser.sidebarAction.open();
}

browser.browserAction.onClicked.addListener(handleClick);

Note that user actions in normal web pages are not treated as user actions for this purpose. For example, if a user clicks a button in a normal web page, and a content script has added a click handler for that button and in that handler sends a message to the extension's background page, then the background page message handler is not considered to be handling a user action.

Also, if a user input handler waits on a promise, then its status as a user input handler is lost. For example:

async function handleClick() {
  let result = await someAsyncFunction();

  // this will fail, because the handler lost its "user action handler" status
  browser.sidebarAction.open();
}

browser.browserAction.onClicked.addListener(handleClick);

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

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

发布评论

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

词条统计

浏览:53 次

字数:2418

最后编辑:7 年前

编辑次数:0 次

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