1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-23 02:42:41 +01: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 && { vAPI.contextMenu = webext.menus && {
_callback: null, _callback: null,
_entries: [], _hash: '',
_createEntry: function(entry) {
webext.menus.create(JSON.parse(JSON.stringify(entry)));
},
onMustUpdate: function() {}, onMustUpdate: function() {},
setEntries: function(entries, callback) { setEntries: function(entries, callback) {
entries = entries || []; entries = entries || [];
let n = Math.max(this._entries.length, entries.length); const hash = entries.map(v => v.id).join();
for ( let i = 0; i < n; i++ ) { if ( hash === this._hash ) { return; }
const oldEntryId = this._entries[i]; this._hash = hash;
const newEntry = entries[i]; webext.menus.removeAll();
if ( oldEntryId && newEntry ) { for ( const entry of entries ) {
if ( newEntry.id !== oldEntryId ) { webext.menus.create(JSON.parse(JSON.stringify(entry)));
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 n = entries.length;
callback = callback || null; callback = callback || null;
if ( callback === this._callback ) { if ( callback === this._callback ) { return; }
return;
}
if ( n !== 0 && callback !== null ) { if ( n !== 0 && callback !== null ) {
webext.menus.onClicked.addListener(callback); webext.menus.onClicked.addListener(callback);
this._callback = callback; this._callback = callback;

View File

@ -73,6 +73,7 @@ const webext = {
}, },
onClicked: chrome.contextMenus.onClicked, onClicked: chrome.contextMenus.onClicked,
remove: promisifyNoFail(chrome.contextMenus, 'remove'), remove: promisifyNoFail(chrome.contextMenus, 'remove'),
removeAll: promisifyNoFail(chrome.contextMenus, 'removeAll'),
}, },
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy
privacy: { privacy: {