Some WebExtension APIs perform functions that are generally performed as a result of a user action. For example:
browserAction.openPopup
API enabling an extension to open the popup programmatically.
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:
例如:
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);
最后修改: , 由 MDN 贡献者