Skip to content

Commit

Permalink
Fix spurious error messages when updating contextual menu
Browse files Browse the repository at this point in the history
There were spurious error messages in the dev console of
uBO in Firefox, because Firefox does not silently ignore
duplicate contextual menu entries, which could occur
transiently when the contextual menu entries were updated.

The fix simplifies contextual menu code, and actually
fulfill the original goal of avoiding to call extensions
framework API as much as possible.
  • Loading branch information
gorhill committed Jul 15, 2021
1 parent bbdb68a commit d17e22a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
35 changes: 10 additions & 25 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,35 +1415,20 @@ vAPI.Net = class {

vAPI.contextMenu = webext.menus && {
_callback: null,
_entries: [],
_createEntry: function(entry) {
webext.menus.create(JSON.parse(JSON.stringify(entry)));
},
_hash: '',
onMustUpdate: function() {},
setEntries: function(entries, callback) {
entries = entries || [];
let n = Math.max(this._entries.length, entries.length);
for ( let i = 0; i < n; i++ ) {
const oldEntryId = this._entries[i];
const newEntry = entries[i];
if ( oldEntryId && newEntry ) {
if ( newEntry.id !== oldEntryId ) {
webext.menus.remove(oldEntryId);
this._createEntry(newEntry);
this._entries[i] = newEntry.id;
}
} else if ( oldEntryId && !newEntry ) {
webext.menus.remove(oldEntryId);
} else if ( !oldEntryId && newEntry ) {
this._createEntry(newEntry);
this._entries[i] = newEntry.id;
}
}
n = this._entries.length = entries.length;
const hash = entries.map(v => v.id).join();
if ( hash === this._hash ) { return; }
this._hash = hash;
webext.menus.removeAll();
for ( const entry of entries ) {
webext.menus.create(JSON.parse(JSON.stringify(entry)));
}
const n = entries.length;
callback = callback || null;
if ( callback === this._callback ) {
return;
}
if ( callback === this._callback ) { return; }
if ( n !== 0 && callback !== null ) {
webext.menus.onClicked.addListener(callback);
this._callback = callback;
Expand Down
1 change: 1 addition & 0 deletions platform/chromium/webext.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const webext = {
},
onClicked: chrome.contextMenus.onClicked,
remove: promisifyNoFail(chrome.contextMenus, 'remove'),
removeAll: promisifyNoFail(chrome.contextMenus, 'removeAll'),
},
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy
privacy: {
Expand Down

0 comments on commit d17e22a

Please sign in to comment.