2015-11-23 13:52:50 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-08-11 16:39:43 +02:00
|
|
|
Copyright (C) 2015-present Raymond Hill
|
2015-11-23 13:52:50 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-16 18:05:24 +02:00
|
|
|
import redirectableResources from './redirect-resources.js';
|
|
|
|
|
2021-07-31 14:38:33 +02:00
|
|
|
import {
|
|
|
|
LineIterator,
|
|
|
|
orphanizeString,
|
|
|
|
} from './text-utils.js';
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2015-11-26 23:56:30 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-08 14:56:36 +02:00
|
|
|
const extToMimeMap = new Map([
|
2023-01-23 22:39:46 +01:00
|
|
|
[ 'css', 'text/css' ],
|
2023-03-26 15:13:17 +02:00
|
|
|
[ 'fn', 'fn/javascript' ], // invented mime type for internal use
|
2019-07-08 14:56:36 +02:00
|
|
|
[ 'gif', 'image/gif' ],
|
|
|
|
[ 'html', 'text/html' ],
|
2022-12-11 16:08:10 +01:00
|
|
|
[ 'js', 'text/javascript' ],
|
2019-07-08 14:56:36 +02:00
|
|
|
[ 'mp3', 'audio/mp3' ],
|
|
|
|
[ 'mp4', 'video/mp4' ],
|
|
|
|
[ 'png', 'image/png' ],
|
|
|
|
[ 'txt', 'text/plain' ],
|
2020-12-29 15:05:28 +01:00
|
|
|
[ 'xml', 'text/xml' ],
|
2019-07-08 14:56:36 +02:00
|
|
|
]);
|
|
|
|
|
2019-08-13 14:16:21 +02:00
|
|
|
const typeToMimeMap = new Map([
|
|
|
|
[ 'main_frame', 'text/html' ],
|
|
|
|
[ 'other', 'text/plain' ],
|
2022-12-11 16:08:10 +01:00
|
|
|
[ 'script', 'text/javascript' ],
|
2019-08-13 14:16:21 +02:00
|
|
|
[ 'stylesheet', 'text/css' ],
|
|
|
|
[ 'sub_frame', 'text/html' ],
|
|
|
|
[ 'xmlhttprequest', 'text/plain' ],
|
|
|
|
]);
|
|
|
|
|
2019-07-08 14:56:36 +02:00
|
|
|
const validMimes = new Set(extToMimeMap.values());
|
2018-02-15 23:25:38 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
const mimeFromName = name => {
|
2019-07-06 18:36:28 +02:00
|
|
|
const match = /\.([^.]+)$/.exec(name);
|
2023-03-26 15:13:17 +02:00
|
|
|
if ( match === null ) { return ''; }
|
|
|
|
return extToMimeMap.get(match[1]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeTopCommentBlock = text => {
|
|
|
|
return text.replace(/^\/\*[\S\s]+?\n\*\/\s*/, '');
|
2019-07-06 18:36:28 +02:00
|
|
|
};
|
|
|
|
|
2023-08-14 16:03:50 +02:00
|
|
|
// vAPI.warSecret is optional, it could be absent in some environments,
|
2021-08-02 15:23:48 +02:00
|
|
|
// i.e. nodejs for example. Probably the best approach is to have the
|
|
|
|
// "web_accessible_resources secret" added outside by the client of this
|
|
|
|
// module, but for now I just want to remove an obstacle to modularization.
|
|
|
|
const warSecret = typeof vAPI === 'object' && vAPI !== null
|
2023-08-14 16:03:50 +02:00
|
|
|
? vAPI.warSecret.short
|
2021-08-02 15:23:48 +02:00
|
|
|
: ( ) => '';
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
const RESOURCES_SELFIE_VERSION = 7;
|
|
|
|
const RESOURCES_SELFIE_NAME = 'compiled/redirectEngine/resources';
|
|
|
|
|
2018-02-15 23:25:38 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
class RedirectEntry {
|
2019-08-13 14:16:21 +02:00
|
|
|
constructor() {
|
|
|
|
this.mime = '';
|
|
|
|
this.data = '';
|
|
|
|
this.warURL = undefined;
|
2020-10-09 19:50:54 +02:00
|
|
|
this.params = undefined;
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
this.requiresTrust = false;
|
2023-05-23 02:19:00 +02:00
|
|
|
this.world = 'MAIN';
|
2023-03-26 15:13:17 +02:00
|
|
|
this.dependencies = [];
|
2019-08-13 14:16:21 +02:00
|
|
|
}
|
2015-11-26 23:56:30 +01:00
|
|
|
|
2019-08-13 14:16:21 +02:00
|
|
|
// Prevent redirection to web accessible resources when the request is
|
|
|
|
// of type 'xmlhttprequest', because XMLHttpRequest.responseURL would
|
|
|
|
// cause leakage of extension id. See:
|
|
|
|
// - https://stackoverflow.com/a/8056313
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=998076
|
2019-08-16 19:43:41 +02:00
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/cpxm1v/
|
|
|
|
// User-supplied resources may already be base64 encoded.
|
2019-08-13 14:16:21 +02:00
|
|
|
|
|
|
|
toURL(fctxt, asDataURI = false) {
|
|
|
|
if (
|
|
|
|
this.warURL !== undefined &&
|
|
|
|
asDataURI !== true &&
|
|
|
|
fctxt instanceof Object &&
|
|
|
|
fctxt.type !== 'xmlhttprequest'
|
|
|
|
) {
|
2021-08-02 15:23:48 +02:00
|
|
|
const params = [];
|
|
|
|
const secret = warSecret();
|
|
|
|
if ( secret !== '' ) { params.push(`secret=${secret}`); }
|
2020-10-09 19:50:54 +02:00
|
|
|
if ( this.params !== undefined ) {
|
|
|
|
for ( const name of this.params ) {
|
|
|
|
const value = fctxt[name];
|
|
|
|
if ( value === undefined ) { continue; }
|
2021-08-02 15:23:48 +02:00
|
|
|
params.push(`${name}=${encodeURIComponent(value)}`);
|
2020-10-09 19:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-02 15:23:48 +02:00
|
|
|
let url = `${this.warURL}`;
|
|
|
|
if ( params.length !== 0 ) {
|
|
|
|
url += `?${params.join('&')}`;
|
|
|
|
}
|
2020-10-09 19:50:54 +02:00
|
|
|
return url;
|
2019-08-13 14:16:21 +02:00
|
|
|
}
|
|
|
|
if ( this.data === undefined ) { return; }
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/701
|
|
|
|
if ( this.data === '' ) {
|
|
|
|
const mime = typeToMimeMap.get(fctxt.type);
|
2023-03-26 15:13:17 +02:00
|
|
|
if ( mime === '' ) { return; }
|
2019-08-13 14:16:21 +02:00
|
|
|
return `data:${mime},`;
|
|
|
|
}
|
|
|
|
if ( this.data.startsWith('data:') === false ) {
|
2019-08-16 19:43:41 +02:00
|
|
|
if ( this.mime.indexOf(';') === -1 ) {
|
|
|
|
this.data = `data:${this.mime};base64,${btoa(this.data)}`;
|
|
|
|
} else {
|
|
|
|
this.data = `data:${this.mime},${this.data}`;
|
|
|
|
}
|
2015-12-23 17:02:36 +01:00
|
|
|
}
|
2019-08-13 14:16:21 +02:00
|
|
|
return this.data;
|
2015-11-26 23:56:30 +01:00
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
2019-08-13 14:16:21 +02:00
|
|
|
toContent() {
|
|
|
|
if ( this.data.startsWith('data:') ) {
|
|
|
|
const pos = this.data.indexOf(',');
|
|
|
|
const base64 = this.data.endsWith(';base64', pos);
|
|
|
|
this.data = this.data.slice(pos + 1);
|
|
|
|
if ( base64 ) {
|
|
|
|
this.data = atob(this.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.data;
|
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
static fromDetails(details) {
|
2019-08-13 14:16:21 +02:00
|
|
|
const r = new RedirectEntry();
|
2023-08-14 16:03:50 +02:00
|
|
|
Object.assign(r, details);
|
2019-08-13 14:16:21 +02:00
|
|
|
return r;
|
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
}
|
2015-11-23 13:52:50 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-11-24 01:18:25 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
class RedirectEngine {
|
|
|
|
constructor() {
|
|
|
|
this.aliases = new Map();
|
|
|
|
this.resources = new Map();
|
|
|
|
this.reset();
|
|
|
|
this.modifyTime = Date.now();
|
|
|
|
this.resourceNameRegister = '';
|
|
|
|
}
|
2022-09-06 19:47:52 +02:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
reset() {
|
|
|
|
}
|
2022-09-06 19:47:52 +02:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
freeze() {
|
2020-11-28 17:26:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
tokenToURL(
|
|
|
|
fctxt,
|
|
|
|
token,
|
|
|
|
asDataURI = false
|
|
|
|
) {
|
|
|
|
const entry = this.resources.get(this.aliases.get(token) || token);
|
|
|
|
if ( entry === undefined ) { return; }
|
|
|
|
this.resourceNameRegister = token;
|
|
|
|
return entry.toURL(fctxt, asDataURI);
|
|
|
|
}
|
2020-11-28 17:26:28 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
tokenToDNR(token) {
|
|
|
|
const entry = this.resources.get(this.aliases.get(token) || token);
|
|
|
|
if ( entry === undefined ) { return; }
|
|
|
|
if ( entry.warURL === undefined ) { return; }
|
|
|
|
return entry.warURL;
|
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
hasToken(token) {
|
|
|
|
if ( token === 'none' ) { return true; }
|
|
|
|
const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */;
|
|
|
|
if ( asDataURI ) {
|
|
|
|
token = token.slice(1);
|
|
|
|
}
|
|
|
|
return this.resources.get(this.aliases.get(token) || token) !== undefined;
|
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
tokenRequiresTrust(token) {
|
|
|
|
const entry = this.resources.get(this.aliases.get(token) || token);
|
|
|
|
return entry && entry.requiresTrust === true || false;
|
|
|
|
}
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
async toSelfie() {
|
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
async fromSelfie() {
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-29 17:04:42 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
contentFromName(name, mime = '') {
|
|
|
|
const entry = this.resources.get(this.aliases.get(name) || name);
|
|
|
|
if ( entry === undefined ) { return; }
|
|
|
|
if ( entry.mime.startsWith(mime) === false ) { return; }
|
|
|
|
return {
|
|
|
|
js: entry.toContent(),
|
2023-05-23 02:19:00 +02:00
|
|
|
world: entry.world,
|
2023-03-26 15:13:17 +02:00
|
|
|
dependencies: entry.dependencies.slice(),
|
|
|
|
};
|
2015-12-23 17:02:36 +01:00
|
|
|
}
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
// https://github.com/uBlockOrigin/uAssets/commit/deefe8755511
|
|
|
|
// Consider 'none' a reserved keyword, to be used to disable redirection.
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1419
|
|
|
|
// Append newlines to raw text to ensure processing of trailing resource.
|
2015-12-23 17:02:36 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
resourcesFromString(text) {
|
|
|
|
const lineIter = new LineIterator(
|
|
|
|
removeTopCommentBlock(text) + '\n\n'
|
|
|
|
);
|
|
|
|
const reNonEmptyLine = /\S/;
|
|
|
|
let fields, encoded, details;
|
|
|
|
|
|
|
|
while ( lineIter.eot() === false ) {
|
|
|
|
const line = lineIter.next();
|
|
|
|
if ( line.startsWith('#') ) { continue; }
|
|
|
|
if ( line.startsWith('// ') ) { continue; }
|
|
|
|
|
|
|
|
if ( fields === undefined ) {
|
|
|
|
if ( line === '' ) { continue; }
|
|
|
|
// Modern parser
|
|
|
|
if ( line.startsWith('/// ') ) {
|
|
|
|
const name = line.slice(4).trim();
|
|
|
|
fields = [ name, mimeFromName(name) ];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Legacy parser
|
|
|
|
const head = line.trim().split(/\s+/);
|
|
|
|
if ( head.length !== 2 ) { continue; }
|
|
|
|
if ( head[0] === 'none' ) { continue; }
|
|
|
|
let pos = head[1].indexOf(';');
|
|
|
|
if ( pos === -1 ) { pos = head[1].length; }
|
|
|
|
if ( validMimes.has(head[1].slice(0, pos)) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
encoded = head[1].indexOf(';') !== -1;
|
|
|
|
fields = head;
|
2019-07-08 14:56:36 +02:00
|
|
|
continue;
|
2019-07-06 18:36:28 +02:00
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
|
|
|
|
if ( line.startsWith('/// ') ) {
|
|
|
|
if ( details === undefined ) {
|
|
|
|
details = [];
|
|
|
|
}
|
|
|
|
const [ prop, value ] = line.slice(4).trim().split(/\s+/);
|
|
|
|
if ( value !== undefined ) {
|
|
|
|
details.push({ prop, value });
|
|
|
|
}
|
2019-07-08 14:56:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
if ( reNonEmptyLine.test(line) ) {
|
|
|
|
fields.push(encoded ? line.trim() : line);
|
|
|
|
continue;
|
2019-07-06 18:36:28 +02:00
|
|
|
}
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
// No more data, add the resource.
|
|
|
|
const name = this.aliases.get(fields[0]) || fields[0];
|
|
|
|
const mime = fields[1];
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
const data = orphanizeString(
|
2023-03-26 15:13:17 +02:00
|
|
|
fields.slice(2).join(encoded ? '' : '\n')
|
|
|
|
);
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
this.resources.set(name, RedirectEntry.fromDetails({ mime, data }));
|
2023-03-26 15:13:17 +02:00
|
|
|
if ( Array.isArray(details) ) {
|
2023-05-28 18:42:47 +02:00
|
|
|
const resource = this.resources.get(name);
|
2023-03-26 15:13:17 +02:00
|
|
|
for ( const { prop, value } of details ) {
|
2023-05-28 18:42:47 +02:00
|
|
|
switch ( prop ) {
|
|
|
|
case 'alias':
|
|
|
|
this.aliases.set(value, name);
|
|
|
|
break;
|
|
|
|
case 'world':
|
|
|
|
if ( /^isolated$/i.test(value) === false ) { break; }
|
|
|
|
resource.world = 'ISOLATED';
|
|
|
|
break;
|
|
|
|
case 'dependency':
|
|
|
|
if ( this.resources.has(value) === false ) { break; }
|
|
|
|
resource.dependencies.push(value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
}
|
2020-12-11 16:34:33 +01:00
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
|
|
|
|
fields = undefined;
|
|
|
|
details = undefined;
|
2019-07-03 15:40:12 +02:00
|
|
|
}
|
2015-11-24 19:21:14 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
this.modifyTime = Date.now();
|
2015-11-24 19:21:14 +01:00
|
|
|
}
|
2015-11-24 05:34:03 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
loadBuiltinResources(fetcher) {
|
|
|
|
this.resources = new Map();
|
|
|
|
this.aliases = new Map();
|
|
|
|
|
|
|
|
const fetches = [
|
|
|
|
import('/assets/resources/scriptlets.js').then(module => {
|
|
|
|
for ( const scriptlet of module.builtinScriptlets ) {
|
2023-08-14 16:03:50 +02:00
|
|
|
const details = {};
|
|
|
|
details.mime = mimeFromName(scriptlet.name);
|
|
|
|
details.data = scriptlet.fn.toString();
|
|
|
|
for ( const [ k, v ] of Object.entries(scriptlet) ) {
|
|
|
|
if ( k === 'fn' ) { continue; }
|
|
|
|
details[k] = v;
|
|
|
|
}
|
|
|
|
const entry = RedirectEntry.fromDetails(details);
|
|
|
|
this.resources.set(details.name, entry);
|
|
|
|
if ( Array.isArray(details.aliases) === false ) { continue; }
|
|
|
|
for ( const alias of details.aliases ) {
|
2023-03-26 15:13:17 +02:00
|
|
|
this.aliases.set(alias, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.modifyTime = Date.now();
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
const store = (name, data = undefined) => {
|
|
|
|
const details = redirectableResources.get(name);
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
const entry = RedirectEntry.fromDetails({
|
2023-03-26 15:13:17 +02:00
|
|
|
mime: mimeFromName(name),
|
|
|
|
data,
|
|
|
|
warURL: `/web_accessible_resources/${name}`,
|
|
|
|
params: details.params,
|
|
|
|
});
|
|
|
|
this.resources.set(name, entry);
|
|
|
|
if ( details.alias === undefined ) { return; }
|
|
|
|
if ( Array.isArray(details.alias) ) {
|
|
|
|
for ( const alias of details.alias ) {
|
2023-03-24 19:05:18 +01:00
|
|
|
this.aliases.set(alias, name);
|
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
} else {
|
|
|
|
this.aliases.set(details.alias, name);
|
2023-03-24 19:05:18 +01:00
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const processBlob = (name, blob) => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = ( ) => {
|
|
|
|
store(name, reader.result);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
reader.onabort = reader.onerror = ( ) => {
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const processText = (name, text) => {
|
|
|
|
store(name, removeTopCommentBlock(text));
|
|
|
|
};
|
|
|
|
|
|
|
|
const process = result => {
|
|
|
|
const match = /^\/web_accessible_resources\/([^?]+)/.exec(result.url);
|
|
|
|
if ( match === null ) { return; }
|
|
|
|
const name = match[1];
|
|
|
|
return result.content instanceof Blob
|
|
|
|
? processBlob(name, result.content)
|
|
|
|
: processText(name, result.content);
|
|
|
|
};
|
|
|
|
|
|
|
|
for ( const [ name, details ] of redirectableResources ) {
|
|
|
|
if ( typeof details.data !== 'string' ) {
|
|
|
|
store(name);
|
|
|
|
continue;
|
2020-12-09 14:16:28 +01:00
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
fetches.push(
|
|
|
|
fetcher(`/web_accessible_resources/${name}`, {
|
|
|
|
responseType: details.data
|
|
|
|
}).then(
|
|
|
|
result => process(result)
|
|
|
|
)
|
|
|
|
);
|
2019-07-03 15:40:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
return Promise.all(fetches);
|
|
|
|
}
|
2019-07-03 15:40:12 +02:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
getResourceDetails() {
|
|
|
|
const out = new Map([
|
|
|
|
[ 'none', { canInject: false, canRedirect: true, aliasOf: '' } ],
|
|
|
|
]);
|
|
|
|
for ( const [ name, entry ] of this.resources ) {
|
|
|
|
out.set(name, {
|
|
|
|
canInject: typeof entry.data === 'string',
|
|
|
|
canRedirect: entry.warURL !== undefined,
|
|
|
|
aliasOf: '',
|
|
|
|
extensionPath: entry.warURL,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for ( const [ alias, name ] of this.aliases ) {
|
|
|
|
const original = out.get(name);
|
|
|
|
if ( original === undefined ) { continue; }
|
|
|
|
const aliased = Object.assign({}, original);
|
|
|
|
aliased.aliasOf = name;
|
|
|
|
out.set(alias, aliased);
|
|
|
|
}
|
|
|
|
return Array.from(out).sort((a, b) => {
|
|
|
|
return a[0].localeCompare(b[0]);
|
2020-06-16 01:05:39 +02:00
|
|
|
});
|
2020-06-15 15:15:13 +02:00
|
|
|
}
|
2018-02-15 23:25:38 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
selfieFromResources(storage) {
|
|
|
|
storage.put(
|
|
|
|
RESOURCES_SELFIE_NAME,
|
|
|
|
JSON.stringify({
|
|
|
|
version: RESOURCES_SELFIE_VERSION,
|
|
|
|
aliases: Array.from(this.aliases),
|
|
|
|
resources: Array.from(this.resources),
|
|
|
|
})
|
|
|
|
);
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
|
|
|
|
async resourcesFromSelfie(storage) {
|
|
|
|
const result = await storage.get(RESOURCES_SELFIE_NAME);
|
|
|
|
let selfie;
|
|
|
|
try {
|
|
|
|
selfie = JSON.parse(result.content);
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
selfie instanceof Object === false ||
|
|
|
|
selfie.version !== RESOURCES_SELFIE_VERSION ||
|
|
|
|
Array.isArray(selfie.resources) === false
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.aliases = new Map(selfie.aliases);
|
|
|
|
this.resources = new Map();
|
|
|
|
for ( const [ token, entry ] of selfie.resources ) {
|
Add trusted-source support for privileged scriptlets
At the moment, the only filter lists deemed from a "trusted source"
are uBO-specific filter lists (i.e. "uBlock filters -- ..."), and
the user's own filters from "My filters".
A new scriptlet which can only be used by filter lists from trusted
sources has been introduced: `sed.js`.
The new `sed.js` scriptlet provides the ability to perform
text-level substitutions. Usage:
example.org##+js(sed, nodeName, pattern, replacement, ...)
`nodeName`
The name of the node for which the text content must be substituted.
Valid node names can be found at:
https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
`pattern`
A string or regex to find in the text content of the node as the target of
substitution.
`replacement`
The replacement text. Can be omitted if the goal is to delete the text which
matches the pattern. Cannot be omitted if extra pairs of parameters have to be
used (see below).
Optionally, extra pairs of parameters to modify the behavior of the scriptlet:
`condition, pattern`
A string or regex which must be found in the text content of the node
in order for the substitution to occur.
`sedCount, n`
This will cause the scriptlet to stop after n instances of substitution. Since
a mutation oberver is used by the scriptlet, it's advised to stop it whenever
it becomes pointless. Default to zero, which means the scriptlet never stops.
`tryCount, n`
This will cause the scriptlet to stop after n instances of mutation observer
run (regardless of whether a substitution occurred). Default to zero, which
means the scriptlet never stops.
`log, 1`
This will cause the scriptlet to output information at the console, useful as
a debugging tool for filter authors. The logging ability is supported only
in the dev build of uBO.
Examples of usage:
example.com##+js(sed, script, /devtoolsDetector\.launch\(\)\;/, , sedCount, 1)
example.com##+js(sed, #text, /^Advertisement$/)
2023-05-21 20:16:56 +02:00
|
|
|
this.resources.set(token, RedirectEntry.fromDetails(entry));
|
2023-03-26 15:13:17 +02:00
|
|
|
}
|
|
|
|
return true;
|
2019-09-15 13:58:28 +02:00
|
|
|
}
|
2018-02-15 23:25:38 +01:00
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
invalidateResourcesSelfie(storage) {
|
|
|
|
storage.remove(RESOURCES_SELFIE_NAME);
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 23:25:38 +01:00
|
|
|
|
2015-11-23 13:52:50 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const redirectEngine = new RedirectEngine();
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
export { redirectEngine };
|
2015-11-23 13:52:50 +01:00
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
/******************************************************************************/
|