1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Fix spurious error messages when updating contextual menu

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.
This commit is contained in:
Raymond Hill 2021-07-15 13:14:37 -04:00
parent bbdb68a2b6
commit d17e22a505
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 10 additions and 24 deletions

View File

@ -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;
}
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)));
}
n = this._entries.length = entries.length;
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;

View File

@ -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: {