2022-09-16 21:56:35 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 18:10:34 +01:00
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
2022-09-16 21:56:35 +02:00
|
|
|
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';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
import { browser } from './ext.js';
|
2022-09-16 21:56:35 +02:00
|
|
|
import { fetchJSON } from './fetch.js';
|
2022-10-10 18:28:24 +02:00
|
|
|
import { getFilteringModeDetails } from './mode-manager.js';
|
|
|
|
import { getEnabledRulesetsDetails } from './ruleset-manager.js';
|
2022-09-20 14:24:01 +02:00
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
import * as ut from './utils.js';
|
2022-09-16 21:56:35 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-04-07 19:00:01 +02:00
|
|
|
const isGecko = browser.runtime.getURL('').startsWith('moz-extension://');
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
const resourceDetailPromises = new Map();
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
function getScriptletDetails() {
|
|
|
|
let promise = resourceDetailPromises.get('scriptlet');
|
|
|
|
if ( promise !== undefined ) { return promise; }
|
|
|
|
promise = fetchJSON('/rulesets/scriptlet-details').then(
|
|
|
|
entries => new Map(entries)
|
|
|
|
);
|
|
|
|
resourceDetailPromises.set('scriptlet', promise);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGenericDetails() {
|
|
|
|
let promise = resourceDetailPromises.get('generic');
|
|
|
|
if ( promise !== undefined ) { return promise; }
|
|
|
|
promise = fetchJSON('/rulesets/generic-details').then(
|
|
|
|
entries => new Map(entries)
|
|
|
|
);
|
|
|
|
resourceDetailPromises.set('generic', promise);
|
|
|
|
return promise;
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
// Important: We need to sort the arrays for fast comparison
|
2022-10-15 19:05:20 +02:00
|
|
|
const arrayEq = (a = [], b = [], sort = true) => {
|
2022-10-10 18:28:24 +02:00
|
|
|
const alen = a.length;
|
|
|
|
if ( alen !== b.length ) { return false; }
|
2022-10-15 19:05:20 +02:00
|
|
|
if ( sort ) { a.sort(); b.sort(); }
|
2022-10-10 18:28:24 +02:00
|
|
|
for ( let i = 0; i < alen; i++ ) {
|
|
|
|
if ( a[i] !== b[i] ) { return false; }
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
// The extensions API does not always return exactly what we fed it, so we
|
|
|
|
// need to normalize some entries to be sure we properly detect changes when
|
|
|
|
// comparing registered entries vs. entries to register.
|
|
|
|
|
|
|
|
const normalizeRegisteredContentScripts = registered => {
|
|
|
|
for ( const entry of registered ) {
|
2023-07-06 21:45:45 +02:00
|
|
|
const { css = [], js = [] } = entry;
|
|
|
|
for ( let i = 0; i < css.length; i++ ) {
|
|
|
|
const path = css[i];
|
|
|
|
if ( path.startsWith('/') ) { continue; }
|
|
|
|
css[i] = `/${path}`;
|
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
for ( let i = 0; i < js.length; i++ ) {
|
|
|
|
const path = js[i];
|
|
|
|
if ( path.startsWith('/') ) { continue; }
|
|
|
|
js[i] = `/${path}`;
|
|
|
|
}
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
return registered;
|
2022-10-10 18:28:24 +02:00
|
|
|
};
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-07-06 21:45:45 +02:00
|
|
|
function registerHighGeneric(context, genericDetails) {
|
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
|
|
|
|
|
|
|
const excludeHostnames = [];
|
|
|
|
const css = [];
|
|
|
|
for ( const details of rulesetsDetails ) {
|
|
|
|
const hostnames = genericDetails.get(details.id);
|
|
|
|
if ( hostnames !== undefined ) {
|
|
|
|
excludeHostnames.push(...hostnames);
|
|
|
|
}
|
|
|
|
const count = details.css?.generichigh || 0;
|
|
|
|
if ( count === 0 ) { continue; }
|
|
|
|
css.push(`/rulesets/scripting/generichigh/${details.id}.css`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( css.length === 0 ) { return; }
|
|
|
|
|
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
|
|
|
const matches = [];
|
|
|
|
const excludeMatches = [];
|
|
|
|
if ( complete.has('all-urls') ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(optimal));
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(excludeHostnames));
|
|
|
|
matches.push('<all_urls>');
|
|
|
|
} else {
|
|
|
|
matches.push(
|
|
|
|
...ut.matchesFromHostnames(
|
|
|
|
ut.subtractHostnameIters(
|
|
|
|
Array.from(complete),
|
|
|
|
excludeHostnames
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( matches.length === 0 ) { return; }
|
|
|
|
|
|
|
|
const registered = before.get('css-generichigh');
|
|
|
|
before.delete('css-generichigh'); // Important!
|
|
|
|
|
|
|
|
// https://github.com/w3c/webextensions/issues/414#issuecomment-1623992885
|
|
|
|
// Once supported, add:
|
|
|
|
// cssOrigin: 'USER',
|
|
|
|
const directive = {
|
|
|
|
id: 'css-generichigh',
|
|
|
|
css,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_end',
|
|
|
|
};
|
|
|
|
|
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
|
|
|
context.toAdd.push(directive);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
if (
|
|
|
|
arrayEq(registered.css, css, false) === false ||
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push('css-generichigh');
|
|
|
|
context.toAdd.push(directive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
function registerGeneric(context, genericDetails) {
|
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
|
|
|
|
|
|
|
const excludeHostnames = [];
|
|
|
|
const js = [];
|
|
|
|
for ( const details of rulesetsDetails ) {
|
|
|
|
const hostnames = genericDetails.get(details.id);
|
|
|
|
if ( hostnames !== undefined ) {
|
|
|
|
excludeHostnames.push(...hostnames);
|
|
|
|
}
|
2023-06-04 03:47:40 +02:00
|
|
|
const count = details.css?.generic || 0;
|
|
|
|
if ( count === 0 ) { continue; }
|
[mv3] Add ability to handle entity-based CSS and scriptlet injection filters
This commit adds the ability to inject entity-based plain CSS
filters and also a set of the most commonly used entity-based
scriptlet injection filters.
Since the scripting API is not compatible with entity patterns,
the entity-related content scripts are injected in all documents
and the entity-matching is done by the content script themselves.
Given this, entity-based content scripts are enabled only when
working in the Complete filtering mode, there won't be any
entity-based filters injected in lower modes.
Also, since there is no way to reasonably have access to the
Public Suffix List in the content scripts, the entity-matching
algorithm is an approximation, though I expect false positives
to be rare (time will tell). In the event of such false
positive, simply falling back to Optimal mode will fix the
issue.
The following issues have been fixed at the same time:
Fixed the no-filtering mode related rules having lower priority
then redirect rules, i.e. redirect rules would still be applied
despite disabling all filtering on a site.
Fixed improper detection of changes to the generic-related CSS
content script, potentially causing undue delays when for example
trying to access the popup panel while working in Complete mode.
The scripting MV3 can be quite slow when registering/updating
large content scripts, so uBOL does its best to call the API only
if really needed, but there had been a regression in the recent
builds preventing uBO from properly detecting unchanged content
script parameters.
2022-10-20 21:48:06 +02:00
|
|
|
js.push(`/rulesets/scripting/generic/${details.id}.js`);
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
|
|
|
js.push('/js/scripting/css-generic.js');
|
|
|
|
|
2023-06-13 17:40:41 +02:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2022-10-15 19:05:20 +02:00
|
|
|
const matches = [];
|
|
|
|
const excludeMatches = [];
|
2023-06-13 17:40:41 +02:00
|
|
|
if ( complete.has('all-urls') ) {
|
2023-06-04 03:47:40 +02:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-13 17:40:41 +02:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(optimal));
|
2022-10-15 19:05:20 +02:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(excludeHostnames));
|
|
|
|
matches.push('<all_urls>');
|
|
|
|
} else {
|
|
|
|
matches.push(
|
|
|
|
...ut.matchesFromHostnames(
|
|
|
|
ut.subtractHostnameIters(
|
2023-06-13 17:40:41 +02:00
|
|
|
Array.from(complete),
|
2022-10-15 19:05:20 +02:00
|
|
|
excludeHostnames
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( matches.length === 0 ) { return; }
|
|
|
|
|
|
|
|
const registered = before.get('css-generic');
|
|
|
|
before.delete('css-generic'); // Important!
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
const directive = {
|
|
|
|
id: 'css-generic',
|
|
|
|
js,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_idle',
|
|
|
|
};
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 15:15:59 +02:00
|
|
|
context.toAdd.push(directive);
|
2022-10-15 19:05:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 15:15:59 +02:00
|
|
|
if (
|
|
|
|
arrayEq(registered.js, js, false) === false ||
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push('css-generic');
|
|
|
|
context.toAdd.push(directive);
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
2022-09-20 02:19:55 +02:00
|
|
|
|
2022-09-19 14:55:45 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
function registerProcedural(context) {
|
2022-10-15 19:05:20 +02:00
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
2022-10-10 18:28:24 +02:00
|
|
|
|
|
|
|
const js = [];
|
2023-06-04 03:47:40 +02:00
|
|
|
for ( const rulesetDetails of rulesetsDetails ) {
|
|
|
|
const count = rulesetDetails.css?.procedural || 0;
|
|
|
|
if ( count === 0 ) { continue; }
|
|
|
|
js.push(`/rulesets/scripting/procedural/${rulesetDetails.id}.js`);
|
2022-09-30 20:55:36 +02:00
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
2023-06-13 17:40:41 +02:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-04 03:47:40 +02:00
|
|
|
const matches = [
|
2023-06-13 17:40:41 +02:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-04 03:47:40 +02:00
|
|
|
];
|
|
|
|
if ( matches.length === 0 ) { return; }
|
2022-10-15 19:05:20 +02:00
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
js.push('/js/scripting/css-procedural.js');
|
2022-10-15 19:05:20 +02:00
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
const excludeMatches = [];
|
2023-06-04 03:47:40 +02:00
|
|
|
if ( none.has('all-urls') === false ) {
|
2022-10-15 19:05:20 +02:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-04 03:47:40 +02:00
|
|
|
}
|
2023-06-13 17:40:41 +02:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2022-09-28 01:51:38 +02:00
|
|
|
}
|
2022-10-10 18:28:24 +02:00
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
const registered = before.get('css-procedural');
|
|
|
|
before.delete('css-procedural'); // Important!
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
const directive = {
|
|
|
|
id: 'css-procedural',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_end',
|
|
|
|
};
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 15:15:59 +02:00
|
|
|
context.toAdd.push(directive);
|
2022-10-10 18:28:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
// update
|
2023-06-05 15:15:59 +02:00
|
|
|
if (
|
|
|
|
arrayEq(registered.js, js, false) === false ||
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push('css-procedural');
|
|
|
|
context.toAdd.push(directive);
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
function registerDeclarative(context) {
|
2022-10-15 19:05:20 +02:00
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
|
|
|
|
|
|
|
const js = [];
|
2023-06-04 03:47:40 +02:00
|
|
|
for ( const rulesetDetails of rulesetsDetails ) {
|
|
|
|
const count = rulesetDetails.css?.declarative || 0;
|
|
|
|
if ( count === 0 ) { continue; }
|
|
|
|
js.push(`/rulesets/scripting/declarative/${rulesetDetails.id}.js`);
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
2023-06-13 17:40:41 +02:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-04 03:47:40 +02:00
|
|
|
const matches = [
|
2023-06-13 17:40:41 +02:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-04 03:47:40 +02:00
|
|
|
];
|
|
|
|
if ( matches.length === 0 ) { return; }
|
2022-10-15 19:05:20 +02:00
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
js.push('/js/scripting/css-declarative.js');
|
2022-10-15 19:05:20 +02:00
|
|
|
|
|
|
|
const excludeMatches = [];
|
2023-06-04 03:47:40 +02:00
|
|
|
if ( none.has('all-urls') === false ) {
|
2022-10-15 19:05:20 +02:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-04 03:47:40 +02:00
|
|
|
}
|
2023-06-13 17:40:41 +02:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const registered = before.get('css-declarative');
|
|
|
|
before.delete('css-declarative'); // Important!
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
const directive = {
|
|
|
|
id: 'css-declarative',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
};
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 15:15:59 +02:00
|
|
|
context.toAdd.push(directive);
|
2022-10-10 18:28:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 15:15:59 +02:00
|
|
|
if (
|
|
|
|
arrayEq(registered.js, js, false) === false ||
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push('css-declarative');
|
|
|
|
context.toAdd.push(directive);
|
2022-10-10 18:28:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-28 01:51:38 +02:00
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
function registerSpecific(context) {
|
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
|
|
|
|
|
|
|
const js = [];
|
|
|
|
for ( const rulesetDetails of rulesetsDetails ) {
|
|
|
|
const count = rulesetDetails.css?.specific || 0;
|
|
|
|
if ( count === 0 ) { continue; }
|
|
|
|
js.push(`/rulesets/scripting/specific/${rulesetDetails.id}.js`);
|
|
|
|
}
|
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
2023-06-13 17:40:41 +02:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-04 03:47:40 +02:00
|
|
|
const matches = [
|
2023-06-13 17:40:41 +02:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-04 03:47:40 +02:00
|
|
|
];
|
|
|
|
if ( matches.length === 0 ) { return; }
|
|
|
|
|
|
|
|
js.push('/js/scripting/css-specific.js');
|
|
|
|
|
|
|
|
const excludeMatches = [];
|
|
|
|
if ( none.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
|
|
|
}
|
2023-06-13 17:40:41 +02:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2023-06-04 03:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const registered = before.get('css-specific');
|
|
|
|
before.delete('css-specific'); // Important!
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
const directive = {
|
|
|
|
id: 'css-specific',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
};
|
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 15:15:59 +02:00
|
|
|
context.toAdd.push(directive);
|
2023-06-04 03:47:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 15:15:59 +02:00
|
|
|
if (
|
|
|
|
arrayEq(registered.js, js, false) === false ||
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push('css-specific');
|
|
|
|
context.toAdd.push(directive);
|
2023-06-04 03:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
function registerScriptlet(context, scriptletDetails) {
|
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
const hasBroadHostPermission =
|
2023-06-13 17:40:41 +02:00
|
|
|
filteringModeDetails.optimal.has('all-urls') ||
|
|
|
|
filteringModeDetails.complete.has('all-urls');
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
const permissionRevokedMatches = [
|
|
|
|
...ut.matchesFromHostnames(filteringModeDetails.none),
|
2023-06-13 17:40:41 +02:00
|
|
|
...ut.matchesFromHostnames(filteringModeDetails.basic),
|
2022-10-15 19:05:20 +02:00
|
|
|
];
|
|
|
|
const permissionGrantedHostnames = [
|
2023-06-13 17:40:41 +02:00
|
|
|
...filteringModeDetails.optimal,
|
|
|
|
...filteringModeDetails.complete,
|
2022-10-15 19:05:20 +02:00
|
|
|
];
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
for ( const rulesetId of rulesetsDetails.map(v => v.id) ) {
|
|
|
|
const scriptletList = scriptletDetails.get(rulesetId);
|
|
|
|
if ( scriptletList === undefined ) { continue; }
|
|
|
|
|
|
|
|
for ( const [ token, scriptletHostnames ] of scriptletList ) {
|
|
|
|
const id = `${rulesetId}.${token}`;
|
|
|
|
const registered = before.get(id);
|
|
|
|
|
|
|
|
const matches = [];
|
|
|
|
const excludeMatches = [];
|
2023-06-05 18:14:37 +02:00
|
|
|
let targetHostnames = [];
|
2022-10-15 19:05:20 +02:00
|
|
|
if ( hasBroadHostPermission ) {
|
|
|
|
excludeMatches.push(...permissionRevokedMatches);
|
2023-06-05 18:14:37 +02:00
|
|
|
if ( scriptletHostnames.length > 100 ) {
|
|
|
|
targetHostnames = [ '*' ];
|
|
|
|
} else {
|
|
|
|
targetHostnames = scriptletHostnames;
|
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
} else if ( permissionGrantedHostnames.length !== 0 ) {
|
2023-06-05 18:14:37 +02:00
|
|
|
if ( scriptletHostnames.includes('*') ) {
|
|
|
|
targetHostnames = permissionGrantedHostnames;
|
|
|
|
} else {
|
|
|
|
targetHostnames = ut.intersectHostnameIters(
|
|
|
|
permissionGrantedHostnames,
|
|
|
|
scriptletHostnames
|
|
|
|
);
|
|
|
|
}
|
2022-10-15 19:05:20 +02:00
|
|
|
}
|
2023-06-05 18:14:37 +02:00
|
|
|
if ( targetHostnames.length === 0 ) { continue; }
|
|
|
|
matches.push(...ut.matchesFromHostnames(targetHostnames));
|
2022-10-15 19:05:20 +02:00
|
|
|
|
|
|
|
before.delete(id); // Important!
|
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
const directive = {
|
|
|
|
id,
|
|
|
|
js: [ `/rulesets/scripting/scriptlet/${id}.js` ],
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
};
|
|
|
|
|
2023-08-11 19:22:25 +02:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1736575
|
|
|
|
// `MAIN` world not yet supported in Firefox
|
|
|
|
if ( isGecko === false ) {
|
|
|
|
directive.world = 'MAIN';
|
|
|
|
}
|
|
|
|
|
2022-10-15 19:05:20 +02:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 15:15:59 +02:00
|
|
|
context.toAdd.push(directive);
|
2022-10-15 19:05:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 15:15:59 +02:00
|
|
|
if (
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push(id);
|
|
|
|
context.toAdd.push(directive);
|
2022-09-20 14:24:01 +02:00
|
|
|
}
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
async function registerInjectables(origins) {
|
|
|
|
void origins;
|
|
|
|
|
|
|
|
if ( browser.scripting === undefined ) { return false; }
|
|
|
|
|
|
|
|
const [
|
2022-10-10 18:28:24 +02:00
|
|
|
filteringModeDetails,
|
|
|
|
rulesetsDetails,
|
2022-10-15 19:05:20 +02:00
|
|
|
scriptletDetails,
|
|
|
|
genericDetails,
|
2022-09-28 01:51:38 +02:00
|
|
|
registered,
|
|
|
|
] = await Promise.all([
|
2022-10-10 18:28:24 +02:00
|
|
|
getFilteringModeDetails(),
|
|
|
|
getEnabledRulesetsDetails(),
|
2022-10-15 19:05:20 +02:00
|
|
|
getScriptletDetails(),
|
|
|
|
getGenericDetails(),
|
2022-09-28 01:51:38 +02:00
|
|
|
browser.scripting.getRegisteredContentScripts(),
|
2022-10-10 18:28:24 +02:00
|
|
|
]);
|
2022-10-15 19:05:20 +02:00
|
|
|
const before = new Map(
|
|
|
|
normalizeRegisteredContentScripts(registered).map(
|
|
|
|
entry => [ entry.id, entry ]
|
|
|
|
)
|
|
|
|
);
|
2023-06-05 15:15:59 +02:00
|
|
|
const toAdd = [], toRemove = [];
|
2022-10-10 18:28:24 +02:00
|
|
|
const context = {
|
|
|
|
filteringModeDetails,
|
2022-10-15 19:05:20 +02:00
|
|
|
rulesetsDetails,
|
2022-10-10 18:28:24 +02:00
|
|
|
before,
|
|
|
|
toAdd,
|
|
|
|
toRemove,
|
|
|
|
};
|
2022-09-17 17:22:25 +02:00
|
|
|
|
2023-06-04 03:47:40 +02:00
|
|
|
registerDeclarative(context);
|
|
|
|
registerProcedural(context);
|
2022-10-15 19:05:20 +02:00
|
|
|
registerScriptlet(context, scriptletDetails);
|
2023-06-04 03:47:40 +02:00
|
|
|
registerSpecific(context);
|
2022-10-15 19:05:20 +02:00
|
|
|
registerGeneric(context, genericDetails);
|
2023-07-06 21:45:45 +02:00
|
|
|
registerHighGeneric(context, genericDetails);
|
2022-09-17 17:22:25 +02:00
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
toRemove.push(...Array.from(before.keys()));
|
2022-09-16 21:56:35 +02:00
|
|
|
|
|
|
|
if ( toRemove.length !== 0 ) {
|
2023-06-04 17:32:55 +02:00
|
|
|
ut.ubolLog(`Unregistered ${toRemove} content (css/js)`);
|
2023-06-05 15:15:59 +02:00
|
|
|
await browser.scripting.unregisterContentScripts({ ids: toRemove })
|
|
|
|
.catch(reason => { console.info(reason); });
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
2023-06-05 15:15:59 +02:00
|
|
|
|
2022-09-16 21:56:35 +02:00
|
|
|
if ( toAdd.length !== 0 ) {
|
2023-06-04 17:32:55 +02:00
|
|
|
ut.ubolLog(`Registered ${toAdd.map(v => v.id)} content (css/js)`);
|
2023-06-05 15:15:59 +02:00
|
|
|
await browser.scripting.registerContentScripts(toAdd)
|
|
|
|
.catch(reason => { console.info(reason); });
|
2022-09-17 17:22:25 +02:00
|
|
|
}
|
2022-09-16 21:56:35 +02:00
|
|
|
|
2023-06-05 15:15:59 +02:00
|
|
|
return true;
|
2022-09-16 21:56:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
export {
|
2022-09-28 01:51:38 +02:00
|
|
|
registerInjectables
|
2022-09-16 21:56:35 +02:00
|
|
|
};
|