1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

[mv3] Fix improper usage of scripting.unregisterContentScripts()

THis was causing all registered css/scripts to be removed when revoking
permission for a single site.
This commit is contained in:
Raymond Hill 2022-09-17 11:22:25 -04:00
parent 3e1160db69
commit 9058c3524e
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -83,6 +83,16 @@ const hostnamesFromMatches = origins => {
return out; return out;
}; };
const arrayEq = (a, b) => {
if ( a === undefined ) { return b === undefined; }
if ( b === undefined ) { return false; }
if ( a.length !== b.length ) { return false; }
for ( const i of a ) {
if ( b.includes(i) === false ) { return false; }
}
return true;
};
/******************************************************************************/ /******************************************************************************/
const toRegisterable = (fname, entry) => { const toRegisterable = (fname, entry) => {
@ -113,6 +123,17 @@ const toRegisterable = (fname, entry) => {
return directive; return directive;
}; };
const toMaybeUpdatable = (registered, candidate) => {
const matches = candidate.y && matchesFromHostnames(candidate.y);
if ( arrayEq(registered.matches, matches) === false ) {
return toRegisterable(candidate);
}
const excludeMatches = candidate.n && matchesFromHostnames(candidate.n);
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
return toRegisterable(candidate);
}
};
/******************************************************************************/ /******************************************************************************/
const shouldRegister = (origins, matches) => { const shouldRegister = (origins, matches) => {
@ -243,26 +264,39 @@ async function registerInjectable() {
} }
} }
const before = new Set(registered.map(entry => entry.id)); const before = new Map(registered.map(entry => [ entry.id, entry ]));
const toAdd = []; const toAdd = [];
const toUpdate = [];
for ( const [ fname, entry ] of toRegister ) { for ( const [ fname, entry ] of toRegister ) {
if ( before.has(fname) ) { continue; } if ( before.has(fname) === false ) {
toAdd.push(toRegisterable(fname, entry)); toAdd.push(toRegisterable(fname, entry));
continue;
}
const updated = toMaybeUpdatable(before.get(fname), entry);
if ( updated !== undefined ) {
toUpdate.push(updated);
}
} }
const toRemove = []; const toRemove = [];
for ( const fname of before ) { for ( const fname of before.keys() ) {
if ( toRegister.has(fname) ) { continue; } if ( toRegister.has(fname) ) { continue; }
toRemove.push(fname); toRemove.push(fname);
} }
const todo = []; const todo = [];
if ( toRemove.length !== 0 ) { if ( toRemove.length !== 0 ) {
todo.push(browser.scripting.unregisterContentScripts(toRemove)); todo.push(browser.scripting.unregisterContentScripts({ ids: toRemove }));
console.info(`Unregistered ${toRemove.length} content (css/js)`); console.info(`Unregistered ${toRemove} content (css/js)`);
} }
if ( toAdd.length !== 0 ) { if ( toAdd.length !== 0 ) {
todo.push(browser.scripting.registerContentScripts(toAdd)); todo.push(browser.scripting.registerContentScripts(toAdd));
console.info(`Registered ${toAdd.length} content (css/js)`); console.info(`Registered ${toAdd.map(v => v.id)} content (css/js)`);
}
if ( toUpdate.length !== 0 ) {
todo.push(browser.scripting.updateContentScripts(toUpdate));
console.info(`Updated ${toUpdate.map(v => v.id)} content (css/js)`);
} }
if ( todo.length === 0 ) { return; } if ( todo.length === 0 ) { return; }