Bug 1963616 - Don't try adding action context menus without permission r=willdurand

When an extension does not have the contextMenus or menus permission,
it is not possible to have extension-defined context menus on an
extension's action button. The actionContextMenu() call would never
add menu items in this case.

As the bug report shows, trying to call actionContextMenu() can throw
an error when the ext-menus.js module has not been loaded. To avoid
this error, verify that the extension has the permission to add menus.

Because ext-menus.js is declared with `"events": ["startup"]`, the
module is guaranteed to be loaded if an extension has the permissions.

Differential Revision: https://phabricator.services.mozilla.com/D248154
This commit is contained in:
Rob Wu
2025-05-07 18:07:55 +00:00
committed by rob@robwu.nl
parent c748163e88
commit 38b9ddf35c
2 changed files with 13 additions and 6 deletions

View File

@@ -726,12 +726,17 @@ this.browserAction = class extends ExtensionAPIPersistent {
const action =
this.extension.manifestVersion < 3 ? "onBrowserAction" : "onAction";
if (
this.extension.hasPermission("contextMenus") ||
this.extension.hasPermission("menus")
) {
global.actionContextMenu({
extension: this.extension,
[action]: true,
menu,
});
}
}
/**
* Returns a potentially pre-loaded popup for the given URL in the given

View File

@@ -263,7 +263,9 @@ this.pageAction = class extends ExtensionAPIPersistent {
menu.id === "pageActionContextMenu" &&
trigger &&
getActionId() === this.browserPageAction.id &&
!this.browserPageAction.getDisabled(trigger.ownerGlobal)
!this.browserPageAction.getDisabled(trigger.ownerGlobal) &&
(this.extension.hasPermission("contextMenus") ||
this.extension.hasPermission("menus"))
) {
global.actionContextMenu({
extension: this.extension,