Skip to content

Commit d82341c

Browse files
authored
Merge pull request #2760 from Rob--W/logspam-contextMenus.remove
Avoid logspam: "Cannot find menu item with id ..."
2 parents 0372abd + 60a6666 commit d82341c

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/js/background/assignManager.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,7 @@ window.assignManager = {
483483
},
484484

485485
contextualIdentityRemoved(changeInfo) {
486-
browser.contextMenus.remove(
487-
changeInfo.contextualIdentity.cookieStoreId
488-
);
486+
this.removeMenuItem(changeInfo.contextualIdentity.cookieStoreId);
489487
},
490488

491489
async _onClickedHandler(info, tab) {
@@ -672,11 +670,11 @@ window.assignManager = {
672670
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1215376#c16
673671
// We also can't change for always private mode
674672
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=1352102
675-
browser.contextMenus.remove(this.MENU_ASSIGN_ID);
676-
browser.contextMenus.remove(this.MENU_REMOVE_ID);
677-
browser.contextMenus.remove(this.MENU_SEPARATOR_ID);
678-
browser.contextMenus.remove(this.MENU_HIDE_ID);
679-
browser.contextMenus.remove(this.MENU_MOVE_ID);
673+
this.removeMenuItem(this.MENU_ASSIGN_ID);
674+
this.removeMenuItem(this.MENU_REMOVE_ID);
675+
this.removeMenuItem(this.MENU_SEPARATOR_ID);
676+
this.removeMenuItem(this.MENU_HIDE_ID);
677+
this.removeMenuItem(this.MENU_MOVE_ID);
680678
},
681679

682680
async calculateContextMenu(tab) {
@@ -850,12 +848,19 @@ window.assignManager = {
850848
},
851849

852850
async removeBookmarksMenu() {
853-
browser.contextMenus.remove(this.OPEN_IN_CONTAINER);
851+
this.removeMenuItem(this.OPEN_IN_CONTAINER);
854852
const identities = await browser.contextualIdentities.query({});
855853
for (const identity of identities) {
856-
browser.contextMenus.remove(identity.cookieStoreId);
854+
this.removeMenuItem(identity.cookieStoreId);
857855
}
858856
},
857+
858+
removeMenuItem(menuItemId) {
859+
// Callers do not check whether the menu exists before attempting to remove
860+
// it. contextMenus.remove rejects when the menu does not exist, so we need
861+
// to catch and swallow the error to avoid logspam.
862+
browser.contextMenus.remove(menuItemId).catch(() => {});
863+
}
859864
};
860865

861866
assignManager.init();

test/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const buildDom = async ({background = {}, popup = {}}) => {
3232
window.crypto = {
3333
getRandomValues: arr => crypto.randomBytes(arr.length),
3434
};
35+
// By default, the mock contextMenus.remove() returns undefined;
36+
// Let it return a Promise instead, so that .then() calls chained to
37+
// it (in src/js/background/assignManager.js) do not fail.
38+
window.browser.contextMenus.remove.resolves();
3539
}
3640
}
3741
};

0 commit comments

Comments
 (0)