diff --git a/platform/mv3/extension/js/background.js b/platform/mv3/extension/js/background.js index 1036e633b..d83abf48e 100644 --- a/platform/mv3/extension/js/background.js +++ b/platform/mv3/extension/js/background.js @@ -154,6 +154,17 @@ async function onPermissionsAdded(permissions) { } } +async function onPermissionsChanged(op, permissions) { + await isFullyInitialized; + const { pending } = onPermissionsChanged; + await Promise.all(pending); + const promise = op === 'removed' + ? onPermissionsRemoved() + : onPermissionsAdded(permissions); + pending.push(promise); +} +onPermissionsChanged.pending = []; + /******************************************************************************/ function setDeveloperMode(state) { @@ -698,13 +709,13 @@ runtime.onMessage.addListener((request, sender, callback) => { browser.permissions.onRemoved.addListener((...args) => { isFullyInitialized.then(( ) => { - onPermissionsRemoved(...args); + onPermissionsChanged('removed', ...args); }); }); browser.permissions.onAdded.addListener((...args) => { isFullyInitialized.then(( ) => { - onPermissionsAdded(...args); + onPermissionsChanged('added', ...args); }); }); diff --git a/platform/mv3/extension/js/mode-manager.js b/platform/mv3/extension/js/mode-manager.js index 1190cd216..4367ff6c2 100644 --- a/platform/mv3/extension/js/mode-manager.js +++ b/platform/mv3/extension/js/mode-manager.js @@ -365,17 +365,15 @@ export async function syncWithBrowserPermissions() { if ( afterMode > MODE_BASIC ) { return afterMode !== beforeMode; } const filteringModes = await getFilteringModeDetails(); if ( allowedHostnames.has('all-urls') === false ) { - const { optimal, complete } = filteringModes; - for ( const hn of optimal ) { - if ( allowedHostnames.has(hn) ) { continue; } - if ( isDescendantHostnameOfIter(hn, allowedHostnames) ) { continue; } - optimal.delete(hn); + const { none, basic, optimal, complete } = filteringModes; + for ( const hn of new Set([ ...optimal, ...complete ]) ) { + applyFilteringMode(filteringModes, hn, afterMode); modified = true; } - for ( const hn of complete ) { - if ( allowedHostnames.has(hn) ) { continue; } - if ( isDescendantHostnameOfIter(hn, allowedHostnames) ) { continue; } - complete.delete(hn); + for ( const hn of allowedHostnames ) { + if ( optimal.has(hn) || complete.has(hn) ) { continue; } + if ( basic.has(hn) || none.has(hn) ) { continue; } + applyFilteringMode(filteringModes, hn, MODE_OPTIMAL); modified = true; } if ( modified ) { diff --git a/platform/mv3/extension/js/utils.js b/platform/mv3/extension/js/utils.js index 730325e1f..cab0b972a 100644 --- a/platform/mv3/extension/js/utils.js +++ b/platform/mv3/extension/js/utils.js @@ -105,10 +105,10 @@ const subtractHostnameIters = (itera, iterb) => { /******************************************************************************/ -const matchFromHostname = hn => +export const matchFromHostname = hn => hn === '*' || hn === 'all-urls' ? '' : `*://*.${hn}/*`; -const matchesFromHostnames = hostnames => { +export const matchesFromHostnames = hostnames => { const out = []; for ( const hn of hostnames ) { out.push(matchFromHostname(hn)); @@ -116,20 +116,25 @@ const matchesFromHostnames = hostnames => { return out; }; -const hostnamesFromMatches = origins => { +export const hostnameFromMatch = origin => { + if ( origin === '' || origin === '*://*/*' ) { return 'all-urls'; } + const match = reOriginToHostname.exec(origin); + if ( match === null ) { return ''; } + return match[1]; +}; + +export const hostnamesFromMatches = origins => { const out = []; for ( const origin of origins ) { - if ( origin === '' || origin === '*://*/*' ) { - out.push('all-urls'); - continue; - } - const match = /^\*:\/\/(?:\*\.)?([^/]+)\/\*/.exec(origin); - if ( match === null ) { continue; } - out.push(match[1]); + const hn = hostnameFromMatch(origin); + if ( hn === '' ) { continue; } + out.push(hn); } return out; }; +const reOriginToHostname = /^\*:\/\/(?:\*\.)?([^/]+)\/\*/; + /******************************************************************************/ const broadcastMessage = message => { @@ -217,9 +222,6 @@ export { isDescendantHostnameOfIter, intersectHostnameIters, subtractHostnameIters, - matchFromHostname, - matchesFromHostnames, - hostnamesFromMatches, hasBroadHostPermissions, gotoURL, strArrayEq,