[mv3] Automatically select optimal for newly allowed hosts

Related issue:
https://github.com/uBlockOrigin/uBOL-home/issues/456
This commit is contained in:
Raymond Hill 2025-10-18 10:03:10 -04:00
parent 5e2b85d53d
commit 511255f7c7
No known key found for this signature in database
GPG key ID: F5630CAE62A14316
3 changed files with 35 additions and 24 deletions

View file

@ -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);
});
});

View file

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

View file

@ -105,10 +105,10 @@ const subtractHostnameIters = (itera, iterb) => {
/******************************************************************************/
const matchFromHostname = hn =>
export const matchFromHostname = hn =>
hn === '*' || hn === 'all-urls' ? '<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 === '<all_urls>' || 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 === '<all_urls>' || 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,