2022-09-16 21:56:35 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2022-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* jshint esversion:11 */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
import { browser, dnr } from './ext.js';
|
|
|
|
import { fetchJSON } from './fetch.js';
|
2022-09-17 14:26:41 +02:00
|
|
|
import { parsedURLromOrigin } from './utils.js';
|
2022-09-16 21:56:35 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
const CSS_TYPE = '0';
|
|
|
|
const JS_TYPE = '1';
|
2022-09-16 21:56:35 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
let scriptingDetailsPromise;
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
function getScriptingDetails() {
|
|
|
|
if ( scriptingDetailsPromise !== undefined ) {
|
|
|
|
return scriptingDetailsPromise;
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2022-09-18 15:31:44 +02:00
|
|
|
scriptingDetailsPromise = fetchJSON('/rulesets/scripting-details').then(entries => {
|
|
|
|
const out = new Map(entries);
|
|
|
|
for ( const details of out.values() ) {
|
|
|
|
details.matches = new Map(details.matches);
|
|
|
|
details.excludeMatches = new Map(details.excludeMatches);
|
|
|
|
}
|
|
|
|
return out;
|
2022-09-16 21:56:35 +02:00
|
|
|
});
|
2022-09-18 15:31:44 +02:00
|
|
|
return scriptingDetailsPromise;
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const matchesFromHostnames = hostnames => {
|
|
|
|
const out = [];
|
|
|
|
for ( const hn of hostnames ) {
|
|
|
|
if ( hn === '*' ) {
|
|
|
|
out.push('*://*/*');
|
|
|
|
} else {
|
|
|
|
out.push(`*://*.${hn}/*`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
const hostnamesFromMatches = origins => {
|
|
|
|
const out = [];
|
|
|
|
for ( const origin of origins ) {
|
|
|
|
const match = /^\*:\/\/([^\/]+)\/\*/.exec(origin);
|
|
|
|
if ( match === null ) { continue; }
|
|
|
|
out.push(match[1]);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
const toBroaderHostname = hn => {
|
|
|
|
if ( hn === '*' ) { return ''; }
|
|
|
|
const pos = hn.indexOf('.');
|
|
|
|
return pos !== -1 ? hn.slice(pos+1) : '*';
|
|
|
|
};
|
|
|
|
|
2022-09-17 17:22:25 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const toRegisterable = (fname, entry) => {
|
|
|
|
const directive = {
|
|
|
|
id: fname,
|
|
|
|
allFrames: true,
|
|
|
|
};
|
2022-09-18 15:31:44 +02:00
|
|
|
if ( entry.matches ) {
|
|
|
|
directive.matches = matchesFromHostnames(entry.matches);
|
2022-09-16 21:56:35 +02:00
|
|
|
} else {
|
|
|
|
directive.matches = [ '*://*/*' ];
|
|
|
|
}
|
2022-09-18 15:31:44 +02:00
|
|
|
if ( entry.excludeMatches ) {
|
|
|
|
directive.excludeMatches = matchesFromHostnames(entry.excludeMatches);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2022-09-18 15:31:44 +02:00
|
|
|
if ( fname.at(-1) === CSS_TYPE ) {
|
2022-09-16 21:56:35 +02:00
|
|
|
directive.css = [
|
2022-09-18 15:31:44 +02:00
|
|
|
`/rulesets/css/${fname.slice(0,1)}/${fname.slice(1,2)}/${fname.slice(2)}.css`
|
2022-09-16 21:56:35 +02:00
|
|
|
];
|
2022-09-18 15:31:44 +02:00
|
|
|
} else if ( fname.at(-1) === JS_TYPE ) {
|
2022-09-16 21:56:35 +02:00
|
|
|
directive.js = [
|
2022-09-18 15:31:44 +02:00
|
|
|
`/rulesets/js/${fname.slice(0,1)}/${fname.slice(1)}.js`
|
2022-09-16 21:56:35 +02:00
|
|
|
];
|
|
|
|
directive.runAt = 'document_start';
|
|
|
|
directive.world = 'MAIN';
|
|
|
|
}
|
|
|
|
|
|
|
|
return directive;
|
|
|
|
};
|
|
|
|
|
2022-09-17 17:22:25 +02:00
|
|
|
const toMaybeUpdatable = (registered, candidate) => {
|
2022-09-18 15:31:44 +02:00
|
|
|
const matches = candidate.matches &&
|
|
|
|
matchesFromHostnames(candidate.matches);
|
2022-09-17 17:22:25 +02:00
|
|
|
if ( arrayEq(registered.matches, matches) === false ) {
|
|
|
|
return toRegisterable(candidate);
|
|
|
|
}
|
2022-09-18 15:31:44 +02:00
|
|
|
const excludeMatches = candidate.excludeMatches &&
|
|
|
|
matchesFromHostnames(candidate.excludeMatches);
|
2022-09-17 17:22:25 +02:00
|
|
|
if ( arrayEq(registered.excludeMatches, excludeMatches) === false ) {
|
|
|
|
return toRegisterable(candidate);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-17 14:26:41 +02:00
|
|
|
async function getInjectableCount(origin) {
|
|
|
|
const url = parsedURLromOrigin(origin);
|
|
|
|
if ( url === undefined ) { return 0; }
|
2022-09-16 21:56:35 +02:00
|
|
|
|
|
|
|
const [
|
|
|
|
rulesetIds,
|
2022-09-18 15:31:44 +02:00
|
|
|
scriptingDetails,
|
2022-09-16 21:56:35 +02:00
|
|
|
] = await Promise.all([
|
|
|
|
dnr.getEnabledRulesets(),
|
2022-09-18 15:31:44 +02:00
|
|
|
getScriptingDetails(),
|
2022-09-16 21:56:35 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
|
|
|
|
for ( const rulesetId of rulesetIds ) {
|
2022-09-18 15:31:44 +02:00
|
|
|
if ( scriptingDetails.has(rulesetId) === false ) { continue; }
|
|
|
|
const details = scriptingDetails.get(rulesetId);
|
|
|
|
let hn = url.hostname;
|
|
|
|
while ( hn !== '' ) {
|
|
|
|
const fnames = details.matches.get(hn);
|
|
|
|
total += fnames && fnames.length || 0;
|
|
|
|
hn = toBroaderHostname(hn);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
// TODO: Mind trusted-site directives.
|
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
async function registerInjectable() {
|
|
|
|
|
|
|
|
const [
|
2022-09-18 15:31:44 +02:00
|
|
|
hostnames,
|
2022-09-16 21:56:35 +02:00
|
|
|
rulesetIds,
|
|
|
|
registered,
|
2022-09-18 15:31:44 +02:00
|
|
|
scriptingDetails,
|
2022-09-16 21:56:35 +02:00
|
|
|
] = await Promise.all([
|
|
|
|
browser.permissions.getAll(),
|
|
|
|
dnr.getEnabledRulesets(),
|
|
|
|
browser.scripting.getRegisteredContentScripts(),
|
2022-09-18 15:31:44 +02:00
|
|
|
getScriptingDetails(),
|
2022-09-16 21:56:35 +02:00
|
|
|
]).then(results => {
|
|
|
|
results[0] = new Set(hostnamesFromMatches(results[0].origins));
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
if ( hostnames.has('*') && hostnames.size > 1 ) {
|
|
|
|
hostnames.clear();
|
|
|
|
hostnames.add('*');
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
const toRegister = new Map();
|
|
|
|
|
|
|
|
const checkRealm = (details, prop, hn) => {
|
|
|
|
const fnames = details[prop].get(hn);
|
|
|
|
if ( fnames === undefined ) { return; }
|
|
|
|
for ( const fname of fnames ) {
|
|
|
|
const existing = toRegister.get(fname);
|
|
|
|
if ( existing ) {
|
|
|
|
existing[prop].push(hn);
|
2022-09-17 14:26:41 +02:00
|
|
|
} else {
|
2022-09-18 15:31:44 +02:00
|
|
|
toRegister.set(fname, { [prop]: [ hn ] });
|
2022-09-17 14:26:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
for ( const rulesetId of rulesetIds ) {
|
2022-09-18 15:31:44 +02:00
|
|
|
const details = scriptingDetails.get(rulesetId);
|
|
|
|
if ( details === undefined ) { continue; }
|
|
|
|
for ( let hn of hostnames ) {
|
|
|
|
while ( hn !== '' ) {
|
|
|
|
checkRealm(details, 'matches', hn);
|
|
|
|
checkRealm(details, 'excludeMatches', hn);
|
|
|
|
hn = toBroaderHostname(hn);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 17:22:25 +02:00
|
|
|
const before = new Map(registered.map(entry => [ entry.id, entry ]));
|
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
const toAdd = [];
|
2022-09-17 17:22:25 +02:00
|
|
|
const toUpdate = [];
|
2022-09-16 21:56:35 +02:00
|
|
|
for ( const [ fname, entry ] of toRegister ) {
|
2022-09-17 17:22:25 +02:00
|
|
|
if ( before.has(fname) === false ) {
|
|
|
|
toAdd.push(toRegisterable(fname, entry));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const updated = toMaybeUpdatable(before.get(fname), entry);
|
|
|
|
if ( updated !== undefined ) {
|
|
|
|
toUpdate.push(updated);
|
|
|
|
}
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2022-09-17 17:22:25 +02:00
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
const toRemove = [];
|
2022-09-17 17:22:25 +02:00
|
|
|
for ( const fname of before.keys() ) {
|
2022-09-16 21:56:35 +02:00
|
|
|
if ( toRegister.has(fname) ) { continue; }
|
|
|
|
toRemove.push(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
const todo = [];
|
|
|
|
if ( toRemove.length !== 0 ) {
|
2022-09-17 17:22:25 +02:00
|
|
|
todo.push(browser.scripting.unregisterContentScripts({ ids: toRemove }));
|
|
|
|
console.info(`Unregistered ${toRemove} content (css/js)`);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
if ( toAdd.length !== 0 ) {
|
|
|
|
todo.push(browser.scripting.registerContentScripts(toAdd));
|
2022-09-17 17:22:25 +02:00
|
|
|
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)`);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
if ( todo.length === 0 ) { return; }
|
|
|
|
|
|
|
|
return Promise.all(todo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
export {
|
|
|
|
getInjectableCount,
|
|
|
|
registerInjectable
|
|
|
|
};
|