2022-09-16 13:56:35 -06: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';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
import { browser } from './ext.js';
|
2022-09-16 13:56:35 -06:00
|
|
|
import { fetchJSON } from './fetch.js';
|
2022-10-10 10:28:24 -06:00
|
|
|
import { getFilteringModeDetails } from './mode-manager.js';
|
|
|
|
import { getEnabledRulesetsDetails } from './ruleset-manager.js';
|
2022-09-20 06:24:01 -06:00
|
|
|
|
2022-09-27 17:51:38 -06:00
|
|
|
import * as ut from './utils.js';
|
2022-09-16 13:56:35 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-04-07 11:00:01 -06:00
|
|
|
const isGecko = browser.runtime.getURL('').startsWith('moz-extension://');
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const resourceDetailPromises = new Map();
|
2022-09-16 13:56:35 -06:00
|
|
|
|
2022-10-15 11:05:20 -06: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 13:56:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
// Important: We need to sort the arrays for fast comparison
|
2022-10-15 11:05:20 -06:00
|
|
|
const arrayEq = (a = [], b = [], sort = true) => {
|
2022-10-10 10:28:24 -06:00
|
|
|
const alen = a.length;
|
|
|
|
if ( alen !== b.length ) { return false; }
|
2022-10-15 11:05:20 -06:00
|
|
|
if ( sort ) { a.sort(); b.sort(); }
|
2022-10-10 10:28:24 -06:00
|
|
|
for ( let i = 0; i < alen; i++ ) {
|
|
|
|
if ( a[i] !== b[i] ) { return false; }
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 11:05:20 -06: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 ) {
|
|
|
|
const { js } = entry;
|
|
|
|
for ( let i = 0; i < js.length; i++ ) {
|
|
|
|
const path = js[i];
|
|
|
|
if ( path.startsWith('/') ) { continue; }
|
|
|
|
js[i] = `/${path}`;
|
|
|
|
}
|
2022-09-16 13:56:35 -06:00
|
|
|
}
|
2022-10-15 11:05:20 -06:00
|
|
|
return registered;
|
2022-10-10 10:28:24 -06:00
|
|
|
};
|
|
|
|
|
2022-10-15 11:05:20 -06: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-03 19:47:40 -06: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 13:48:06 -06:00
|
|
|
js.push(`/rulesets/scripting/generic/${details.id}.js`);
|
2022-10-15 11:05:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
|
|
|
js.push('/js/scripting/css-generic.js');
|
|
|
|
|
2023-06-13 09:40:41 -06:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2022-10-15 11:05:20 -06:00
|
|
|
const matches = [];
|
|
|
|
const excludeMatches = [];
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( complete.has('all-urls') ) {
|
2023-06-03 19:47:40 -06:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-13 09:40:41 -06:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(optimal));
|
2022-10-15 11:05:20 -06:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(excludeHostnames));
|
|
|
|
matches.push('<all_urls>');
|
|
|
|
} else {
|
|
|
|
matches.push(
|
|
|
|
...ut.matchesFromHostnames(
|
|
|
|
ut.subtractHostnameIters(
|
2023-06-13 09:40:41 -06:00
|
|
|
Array.from(complete),
|
2022-10-15 11:05:20 -06:00
|
|
|
excludeHostnames
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( matches.length === 0 ) { return; }
|
|
|
|
|
|
|
|
const registered = before.get('css-generic');
|
|
|
|
before.delete('css-generic'); // Important!
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
const directive = {
|
|
|
|
id: 'css-generic',
|
|
|
|
js,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_idle',
|
|
|
|
};
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 07:15:59 -06:00
|
|
|
context.toAdd.push(directive);
|
2022-10-15 11:05:20 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 07:15:59 -06: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 13:56:35 -06:00
|
|
|
}
|
2022-10-15 11:05:20 -06:00
|
|
|
}
|
2022-09-19 18:19:55 -06:00
|
|
|
|
2022-09-19 06:55:45 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
function registerProcedural(context) {
|
2022-10-15 11:05:20 -06:00
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
2022-10-10 10:28:24 -06:00
|
|
|
|
|
|
|
const js = [];
|
2023-06-03 19:47:40 -06: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 12:55:36 -06:00
|
|
|
}
|
2022-10-15 11:05:20 -06:00
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
2023-06-13 09:40:41 -06:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-03 19:47:40 -06:00
|
|
|
const matches = [
|
2023-06-13 09:40:41 -06:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-03 19:47:40 -06:00
|
|
|
];
|
|
|
|
if ( matches.length === 0 ) { return; }
|
2022-10-15 11:05:20 -06:00
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
js.push('/js/scripting/css-procedural.js');
|
2022-10-15 11:05:20 -06:00
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
const excludeMatches = [];
|
2023-06-03 19:47:40 -06:00
|
|
|
if ( none.has('all-urls') === false ) {
|
2022-10-15 11:05:20 -06:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-03 19:47:40 -06:00
|
|
|
}
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2022-09-27 17:51:38 -06:00
|
|
|
}
|
2022-10-10 10:28:24 -06:00
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const registered = before.get('css-procedural');
|
|
|
|
before.delete('css-procedural'); // Important!
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
const directive = {
|
|
|
|
id: 'css-procedural',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_end',
|
|
|
|
};
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 07:15:59 -06:00
|
|
|
context.toAdd.push(directive);
|
2022-10-10 10:28:24 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
// update
|
2023-06-05 07:15:59 -06: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 11:05:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
function registerDeclarative(context) {
|
2022-10-15 11:05:20 -06:00
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
|
|
|
|
|
|
|
const js = [];
|
2023-06-03 19:47:40 -06: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 11:05:20 -06:00
|
|
|
}
|
|
|
|
if ( js.length === 0 ) { return; }
|
|
|
|
|
2023-06-13 09:40:41 -06:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-03 19:47:40 -06:00
|
|
|
const matches = [
|
2023-06-13 09:40:41 -06:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-03 19:47:40 -06:00
|
|
|
];
|
|
|
|
if ( matches.length === 0 ) { return; }
|
2022-10-15 11:05:20 -06:00
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
js.push('/js/scripting/css-declarative.js');
|
2022-10-15 11:05:20 -06:00
|
|
|
|
|
|
|
const excludeMatches = [];
|
2023-06-03 19:47:40 -06:00
|
|
|
if ( none.has('all-urls') === false ) {
|
2022-10-15 11:05:20 -06:00
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(none));
|
2023-06-03 19:47:40 -06:00
|
|
|
}
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2022-10-15 11:05:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const registered = before.get('css-declarative');
|
|
|
|
before.delete('css-declarative'); // Important!
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
const directive = {
|
|
|
|
id: 'css-declarative',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 07:15:59 -06:00
|
|
|
context.toAdd.push(directive);
|
2022-10-10 10:28:24 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 07:15:59 -06: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 10:28:24 -06:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 17:51:38 -06:00
|
|
|
|
2022-09-16 13:56:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-06-03 19:47:40 -06: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 09:40:41 -06:00
|
|
|
const { none, basic, optimal, complete } = filteringModeDetails;
|
2023-06-03 19:47:40 -06:00
|
|
|
const matches = [
|
2023-06-13 09:40:41 -06:00
|
|
|
...ut.matchesFromHostnames(optimal),
|
|
|
|
...ut.matchesFromHostnames(complete),
|
2023-06-03 19:47:40 -06: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 09:40:41 -06:00
|
|
|
if ( basic.has('all-urls') === false ) {
|
|
|
|
excludeMatches.push(...ut.matchesFromHostnames(basic));
|
2023-06-03 19:47:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const registered = before.get('css-specific');
|
|
|
|
before.delete('css-specific'); // Important!
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
const directive = {
|
|
|
|
id: 'css-specific',
|
|
|
|
js,
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
};
|
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 07:15:59 -06:00
|
|
|
context.toAdd.push(directive);
|
2023-06-03 19:47:40 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 07:15:59 -06: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-03 19:47:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
function registerScriptlet(context, scriptletDetails) {
|
2023-04-07 08:19:43 -06:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1736575
|
|
|
|
// `MAIN` world not yet supported in Firefox
|
2023-04-07 11:00:01 -06:00
|
|
|
if ( isGecko ) { return; }
|
2023-04-07 08:19:43 -06:00
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const { before, filteringModeDetails, rulesetsDetails } = context;
|
2022-09-16 13:56:35 -06:00
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const hasBroadHostPermission =
|
2023-06-13 09:40:41 -06:00
|
|
|
filteringModeDetails.optimal.has('all-urls') ||
|
|
|
|
filteringModeDetails.complete.has('all-urls');
|
2022-09-16 13:56:35 -06:00
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const permissionRevokedMatches = [
|
|
|
|
...ut.matchesFromHostnames(filteringModeDetails.none),
|
2023-06-13 09:40:41 -06:00
|
|
|
...ut.matchesFromHostnames(filteringModeDetails.basic),
|
2022-10-15 11:05:20 -06:00
|
|
|
];
|
|
|
|
const permissionGrantedHostnames = [
|
2023-06-13 09:40:41 -06:00
|
|
|
...filteringModeDetails.optimal,
|
|
|
|
...filteringModeDetails.complete,
|
2022-10-15 11:05:20 -06:00
|
|
|
];
|
2022-09-16 13:56:35 -06:00
|
|
|
|
2022-10-15 11:05:20 -06: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 10:14:37 -06:00
|
|
|
let targetHostnames = [];
|
2022-10-15 11:05:20 -06:00
|
|
|
if ( hasBroadHostPermission ) {
|
|
|
|
excludeMatches.push(...permissionRevokedMatches);
|
2023-06-05 10:14:37 -06:00
|
|
|
if ( scriptletHostnames.length > 100 ) {
|
|
|
|
targetHostnames = [ '*' ];
|
|
|
|
} else {
|
|
|
|
targetHostnames = scriptletHostnames;
|
|
|
|
}
|
2022-10-15 11:05:20 -06:00
|
|
|
} else if ( permissionGrantedHostnames.length !== 0 ) {
|
2023-06-05 10:14:37 -06:00
|
|
|
if ( scriptletHostnames.includes('*') ) {
|
|
|
|
targetHostnames = permissionGrantedHostnames;
|
|
|
|
} else {
|
|
|
|
targetHostnames = ut.intersectHostnameIters(
|
|
|
|
permissionGrantedHostnames,
|
|
|
|
scriptletHostnames
|
|
|
|
);
|
|
|
|
}
|
2022-10-15 11:05:20 -06:00
|
|
|
}
|
2023-06-05 10:14:37 -06:00
|
|
|
if ( targetHostnames.length === 0 ) { continue; }
|
|
|
|
matches.push(...ut.matchesFromHostnames(targetHostnames));
|
2022-10-15 11:05:20 -06:00
|
|
|
|
|
|
|
before.delete(id); // Important!
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
const directive = {
|
|
|
|
id,
|
|
|
|
js: [ `/rulesets/scripting/scriptlet/${id}.js` ],
|
|
|
|
allFrames: true,
|
|
|
|
matches,
|
|
|
|
excludeMatches,
|
|
|
|
runAt: 'document_start',
|
|
|
|
world: 'MAIN',
|
|
|
|
};
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
// register
|
|
|
|
if ( registered === undefined ) {
|
2023-06-05 07:15:59 -06:00
|
|
|
context.toAdd.push(directive);
|
2022-10-15 11:05:20 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
2023-06-05 07:15:59 -06:00
|
|
|
if (
|
|
|
|
arrayEq(registered.matches, matches) === false ||
|
|
|
|
arrayEq(registered.excludeMatches, excludeMatches) === false
|
|
|
|
) {
|
|
|
|
context.toRemove.push(id);
|
|
|
|
context.toAdd.push(directive);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2022-09-16 13:56:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-27 17:51:38 -06:00
|
|
|
async function registerInjectables(origins) {
|
|
|
|
void origins;
|
|
|
|
|
|
|
|
if ( browser.scripting === undefined ) { return false; }
|
|
|
|
|
|
|
|
const [
|
2022-10-10 10:28:24 -06:00
|
|
|
filteringModeDetails,
|
|
|
|
rulesetsDetails,
|
2022-10-15 11:05:20 -06:00
|
|
|
scriptletDetails,
|
|
|
|
genericDetails,
|
2022-09-27 17:51:38 -06:00
|
|
|
registered,
|
|
|
|
] = await Promise.all([
|
2022-10-10 10:28:24 -06:00
|
|
|
getFilteringModeDetails(),
|
|
|
|
getEnabledRulesetsDetails(),
|
2022-10-15 11:05:20 -06:00
|
|
|
getScriptletDetails(),
|
|
|
|
getGenericDetails(),
|
2022-09-27 17:51:38 -06:00
|
|
|
browser.scripting.getRegisteredContentScripts(),
|
2022-10-10 10:28:24 -06:00
|
|
|
]);
|
2022-10-15 11:05:20 -06:00
|
|
|
const before = new Map(
|
|
|
|
normalizeRegisteredContentScripts(registered).map(
|
|
|
|
entry => [ entry.id, entry ]
|
|
|
|
)
|
|
|
|
);
|
2023-06-05 07:15:59 -06:00
|
|
|
const toAdd = [], toRemove = [];
|
2022-10-10 10:28:24 -06:00
|
|
|
const context = {
|
|
|
|
filteringModeDetails,
|
2022-10-15 11:05:20 -06:00
|
|
|
rulesetsDetails,
|
2022-10-10 10:28:24 -06:00
|
|
|
before,
|
|
|
|
toAdd,
|
|
|
|
toRemove,
|
|
|
|
};
|
2022-09-17 09:22:25 -06:00
|
|
|
|
2023-06-03 19:47:40 -06:00
|
|
|
registerDeclarative(context);
|
|
|
|
registerProcedural(context);
|
2022-10-15 11:05:20 -06:00
|
|
|
registerScriptlet(context, scriptletDetails);
|
2023-06-03 19:47:40 -06:00
|
|
|
registerSpecific(context);
|
2022-10-15 11:05:20 -06:00
|
|
|
registerGeneric(context, genericDetails);
|
2022-09-17 09:22:25 -06:00
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
toRemove.push(...Array.from(before.keys()));
|
2022-09-16 13:56:35 -06:00
|
|
|
|
|
|
|
if ( toRemove.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ut.ubolLog(`Unregistered ${toRemove} content (css/js)`);
|
2023-06-05 07:15:59 -06:00
|
|
|
await browser.scripting.unregisterContentScripts({ ids: toRemove })
|
|
|
|
.catch(reason => { console.info(reason); });
|
2022-09-16 13:56:35 -06:00
|
|
|
}
|
2023-06-05 07:15:59 -06:00
|
|
|
|
2022-09-16 13:56:35 -06:00
|
|
|
if ( toAdd.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ut.ubolLog(`Registered ${toAdd.map(v => v.id)} content (css/js)`);
|
2023-06-05 07:15:59 -06:00
|
|
|
await browser.scripting.registerContentScripts(toAdd)
|
|
|
|
.catch(reason => { console.info(reason); });
|
2022-09-17 09:22:25 -06:00
|
|
|
}
|
2022-09-16 13:56:35 -06:00
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
return true;
|
2022-09-16 13:56:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
export {
|
2022-09-27 17:51:38 -06:00
|
|
|
registerInjectables
|
2022-09-16 13:56:35 -06:00
|
|
|
};
|