2019-07-06 18:36:28 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
|
Copyright (C) 2019-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
|
|
|
|
|
|
|
|
|
|
The scriptlets below are meant to be injected only into a
|
|
|
|
|
web page context.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-26 18:31:36 +02:00
|
|
|
|
// Externally added to the private namespace in which scriptlets execute.
|
2023-03-31 02:46:44 +02:00
|
|
|
|
/* global scriptletGlobals */
|
2023-03-26 18:31:36 +02:00
|
|
|
|
|
2019-07-08 14:56:36 +02:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
export const builtinScriptlets = [];
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
|
|
Helper functions
|
|
|
|
|
|
|
|
|
|
These are meant to be used as dependencies to injectable scriptlets.
|
|
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
2023-03-26 18:31:36 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'safe-self.fn',
|
|
|
|
|
fn: safeSelf,
|
|
|
|
|
});
|
|
|
|
|
function safeSelf() {
|
2023-03-31 02:46:44 +02:00
|
|
|
|
if ( scriptletGlobals.has('safeSelf') ) {
|
|
|
|
|
return scriptletGlobals.get('safeSelf');
|
2023-03-26 18:31:36 +02:00
|
|
|
|
}
|
|
|
|
|
const safe = {
|
|
|
|
|
'RegExp': self.RegExp,
|
|
|
|
|
'RegExp_test': self.RegExp.prototype.test,
|
|
|
|
|
'RegExp_exec': self.RegExp.prototype.exec,
|
2023-04-28 13:58:23 +02:00
|
|
|
|
'addEventListener': self.EventTarget.prototype.addEventListener,
|
|
|
|
|
'removeEventListener': self.EventTarget.prototype.removeEventListener,
|
2023-04-15 17:14:14 +02:00
|
|
|
|
'log': console.log.bind(console),
|
2023-05-20 23:18:44 +02:00
|
|
|
|
'uboLog': function(...args) {
|
|
|
|
|
if ( args.length === 0 ) { return; }
|
|
|
|
|
if ( `${args[0]}` === '' ) { return; }
|
|
|
|
|
this.log('[uBO]', ...args);
|
2023-04-02 18:01:58 +02:00
|
|
|
|
},
|
2023-03-26 18:31:36 +02:00
|
|
|
|
};
|
2023-03-31 02:46:44 +02:00
|
|
|
|
scriptletGlobals.set('safeSelf', safe);
|
2023-03-26 18:31:36 +02:00
|
|
|
|
return safe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'pattern-to-regex.fn',
|
|
|
|
|
fn: patternToRegex,
|
|
|
|
|
});
|
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
|
|
|
|
function patternToRegex(pattern, flags = undefined) {
|
|
|
|
|
if ( pattern === '' ) { return /^/; }
|
|
|
|
|
const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern);
|
|
|
|
|
if ( match !== null ) {
|
|
|
|
|
return new RegExp(match[1], match[2] || flags);
|
2023-03-26 15:13:17 +02: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
|
|
|
|
return new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), flags);
|
2023-03-26 15:13:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'get-exception-token.fn',
|
|
|
|
|
fn: getExceptionToken,
|
|
|
|
|
});
|
|
|
|
|
function getExceptionToken() {
|
|
|
|
|
const token =
|
|
|
|
|
String.fromCharCode(Date.now() % 26 + 97) +
|
|
|
|
|
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
|
|
|
|
|
const oe = self.onerror;
|
|
|
|
|
self.onerror = function(msg, ...args) {
|
|
|
|
|
if ( typeof msg === 'string' && msg.includes(token) ) { return true; }
|
|
|
|
|
if ( oe instanceof Function ) {
|
|
|
|
|
return oe.call(this, msg, ...args);
|
|
|
|
|
}
|
|
|
|
|
}.bind();
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 18:01:58 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'should-debug.fn',
|
|
|
|
|
fn: shouldDebug,
|
|
|
|
|
});
|
|
|
|
|
function shouldDebug(details) {
|
|
|
|
|
if ( details instanceof Object === false ) { return false; }
|
|
|
|
|
return scriptletGlobals.has('canDebug') && details.debug;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'should-log.fn',
|
|
|
|
|
fn: shouldLog,
|
|
|
|
|
});
|
|
|
|
|
function shouldLog(details) {
|
|
|
|
|
if ( details instanceof Object === false ) { return false; }
|
|
|
|
|
return scriptletGlobals.has('canDebug') && details.log;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-27 18:52:17 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'run-at.fn',
|
|
|
|
|
fn: runAt,
|
2023-04-28 13:58:23 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
],
|
2023-04-27 18:52:17 +02:00
|
|
|
|
});
|
|
|
|
|
function runAt(fn, when) {
|
|
|
|
|
const intFromReadyState = state => {
|
2023-05-19 18:55:01 +02:00
|
|
|
|
const targets = {
|
|
|
|
|
'loading': 1,
|
|
|
|
|
'interactive': 2, 'end': 2, '2': 2,
|
|
|
|
|
'complete': 3, 'idle': 3, '3': 3,
|
|
|
|
|
};
|
|
|
|
|
const tokens = Array.isArray(state) ? state : [ state ];
|
|
|
|
|
for ( const token of tokens ) {
|
|
|
|
|
const prop = `${token}`;
|
|
|
|
|
if ( targets.hasOwnProperty(prop) === false ) { continue; }
|
|
|
|
|
return targets[prop];
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2023-04-27 18:52:17 +02:00
|
|
|
|
};
|
|
|
|
|
const runAt = intFromReadyState(when);
|
|
|
|
|
if ( intFromReadyState(document.readyState) >= runAt ) {
|
|
|
|
|
fn(); return;
|
|
|
|
|
}
|
|
|
|
|
const onStateChange = ( ) => {
|
|
|
|
|
if ( intFromReadyState(document.readyState) < runAt ) { return; }
|
|
|
|
|
fn();
|
2023-04-28 13:58:23 +02:00
|
|
|
|
safe.removeEventListener.apply(document, args);
|
2023-04-27 18:52:17 +02:00
|
|
|
|
};
|
2023-04-28 13:58:23 +02:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
const args = [ 'readystatechange', onStateChange, { capture: true } ];
|
|
|
|
|
safe.addEventListener.apply(document, args);
|
2023-04-27 18:52:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:59:27 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2023-05-24 16:32:03 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'run-at-html-element.fn',
|
|
|
|
|
fn: runAtHtmlElement,
|
|
|
|
|
});
|
|
|
|
|
function runAtHtmlElement(fn) {
|
|
|
|
|
if ( document.documentElement ) {
|
|
|
|
|
fn();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const observer = new MutationObserver(( ) => {
|
|
|
|
|
observer.disconnect();
|
2023-05-24 21:33:46 +02:00
|
|
|
|
fn();
|
2023-05-24 16:32:03 +02:00
|
|
|
|
});
|
|
|
|
|
observer.observe(document, { childList: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'get-extra-args-entries.fn',
|
|
|
|
|
fn: getExtraArgsEntries,
|
|
|
|
|
});
|
|
|
|
|
function getExtraArgsEntries(args, offset) {
|
|
|
|
|
return args.slice(offset).reduce((out, v, i, a) => {
|
|
|
|
|
if ( (i & 1) === 0 ) {
|
|
|
|
|
const rawValue = a[i+1];
|
|
|
|
|
const value = /^\d+$/.test(rawValue)
|
|
|
|
|
? parseInt(rawValue, 10)
|
|
|
|
|
: rawValue;
|
|
|
|
|
out.push([ a[i], value ]);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'get-extra-args-map.fn',
|
|
|
|
|
fn: getExtraArgsMap,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'get-extra-args-entries.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function getExtraArgsMap(args, offset = 0) {
|
|
|
|
|
return new Map(getExtraArgsEntries(args, offset));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'get-extra-args.fn',
|
|
|
|
|
fn: getExtraArgs,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'get-extra-args-entries.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function getExtraArgs(args, offset = 0) {
|
|
|
|
|
return Object.fromEntries(getExtraArgsEntries(args, offset));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2023-05-24 16:32:03 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'abort-current-script-core.fn',
|
|
|
|
|
fn: abortCurrentScriptCore,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
'get-exception-token.fn',
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
'should-debug.fn',
|
|
|
|
|
'should-log.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
// Issues to mind before changing anything:
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
|
|
|
|
|
function abortCurrentScriptCore(
|
|
|
|
|
arg1 = '',
|
|
|
|
|
arg2 = '',
|
|
|
|
|
arg3 = ''
|
|
|
|
|
) {
|
|
|
|
|
const details = typeof arg1 !== 'object'
|
|
|
|
|
? { target: arg1, needle: arg2, context: arg3 }
|
|
|
|
|
: arg1;
|
|
|
|
|
const { target = '', needle = '', context = '' } = details;
|
|
|
|
|
if ( typeof target !== 'string' ) { return; }
|
|
|
|
|
if ( target === '' ) { return; }
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
const reNeedle = patternToRegex(needle);
|
|
|
|
|
const reContext = patternToRegex(context);
|
|
|
|
|
const thisScript = document.currentScript;
|
|
|
|
|
const chain = target.split('.');
|
|
|
|
|
let owner = window;
|
|
|
|
|
let prop;
|
|
|
|
|
for (;;) {
|
|
|
|
|
prop = chain.shift();
|
|
|
|
|
if ( chain.length === 0 ) { break; }
|
|
|
|
|
owner = owner[prop];
|
|
|
|
|
if ( owner instanceof Object === false ) { return; }
|
|
|
|
|
}
|
|
|
|
|
let value;
|
|
|
|
|
let desc = Object.getOwnPropertyDescriptor(owner, prop);
|
|
|
|
|
if (
|
|
|
|
|
desc instanceof Object === false ||
|
|
|
|
|
desc.get instanceof Function === false
|
|
|
|
|
) {
|
|
|
|
|
value = owner[prop];
|
|
|
|
|
desc = undefined;
|
|
|
|
|
}
|
|
|
|
|
const log = shouldLog(details);
|
|
|
|
|
const debug = shouldDebug(details);
|
|
|
|
|
const exceptionToken = getExceptionToken();
|
|
|
|
|
const scriptTexts = new WeakMap();
|
|
|
|
|
const getScriptText = elem => {
|
|
|
|
|
let text = elem.textContent;
|
|
|
|
|
if ( text.trim() !== '' ) { return text; }
|
|
|
|
|
if ( scriptTexts.has(elem) ) { return scriptTexts.get(elem); }
|
|
|
|
|
const [ , mime, content ] =
|
|
|
|
|
/^data:([^,]*),(.+)$/.exec(elem.src.trim()) ||
|
|
|
|
|
[ '', '', '' ];
|
|
|
|
|
try {
|
|
|
|
|
switch ( true ) {
|
|
|
|
|
case mime.endsWith(';base64'):
|
|
|
|
|
text = self.atob(content);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
text = self.decodeURIComponent(content);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
scriptTexts.set(elem, text);
|
|
|
|
|
return text;
|
|
|
|
|
};
|
|
|
|
|
const validate = ( ) => {
|
|
|
|
|
if ( debug ) { debugger; } // jshint ignore: line
|
|
|
|
|
const e = document.currentScript;
|
|
|
|
|
if ( e instanceof HTMLScriptElement === false ) { return; }
|
|
|
|
|
if ( e === thisScript ) { return; }
|
|
|
|
|
if ( reContext.test(e.src) === false ) { return; }
|
|
|
|
|
if ( log && e.src !== '' ) { safe.uboLog(`matched src: ${e.src}`); }
|
|
|
|
|
const scriptText = getScriptText(e);
|
|
|
|
|
if ( reNeedle.test(scriptText) === false ) { return; }
|
|
|
|
|
if ( log ) { safe.uboLog(`matched script text: ${scriptText}`); }
|
|
|
|
|
throw new ReferenceError(exceptionToken);
|
|
|
|
|
};
|
|
|
|
|
if ( debug ) { debugger; } // jshint ignore: line
|
|
|
|
|
try {
|
|
|
|
|
Object.defineProperty(owner, prop, {
|
|
|
|
|
get: function() {
|
|
|
|
|
validate();
|
|
|
|
|
return desc instanceof Object
|
|
|
|
|
? desc.get.call(owner)
|
|
|
|
|
: value;
|
|
|
|
|
},
|
|
|
|
|
set: function(a) {
|
|
|
|
|
validate();
|
|
|
|
|
if ( desc instanceof Object ) {
|
|
|
|
|
desc.set.call(owner, a);
|
|
|
|
|
} else {
|
|
|
|
|
value = a;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
if ( log ) { safe.uboLog(ex); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2023-05-23 16:59:27 +02:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'set-constant-core.fn',
|
|
|
|
|
fn: setConstantCore,
|
|
|
|
|
dependencies: [
|
2023-05-23 23:03:30 +02:00
|
|
|
|
'run-at.fn',
|
2023-05-23 16:59:27 +02:00
|
|
|
|
'safe-self.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function setConstantCore(
|
|
|
|
|
trusted = false,
|
|
|
|
|
arg1 = '',
|
|
|
|
|
arg2 = '',
|
|
|
|
|
arg3 = ''
|
|
|
|
|
) {
|
|
|
|
|
const details = typeof arg1 !== 'object'
|
|
|
|
|
? { prop: arg1, value: arg2 }
|
|
|
|
|
: arg1;
|
|
|
|
|
if ( arg3 !== '' ) {
|
|
|
|
|
if ( /^\d$/.test(arg3) ) {
|
|
|
|
|
details.options = [ arg3 ];
|
|
|
|
|
} else {
|
|
|
|
|
details.options = Array.from(arguments).slice(3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const { prop: chain = '', value: cValue = '' } = details;
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
|
|
|
|
if ( chain === '' ) { return; }
|
|
|
|
|
const options = details.options || [];
|
|
|
|
|
function setConstant(chain, cValue) {
|
|
|
|
|
const trappedProp = (( ) => {
|
|
|
|
|
const pos = chain.lastIndexOf('.');
|
|
|
|
|
if ( pos === -1 ) { return chain; }
|
|
|
|
|
return chain.slice(pos+1);
|
|
|
|
|
})();
|
|
|
|
|
if ( trappedProp === '' ) { return; }
|
|
|
|
|
const thisScript = document.currentScript;
|
|
|
|
|
const objectDefineProperty = Object.defineProperty.bind(Object);
|
|
|
|
|
const cloakFunc = fn => {
|
|
|
|
|
objectDefineProperty(fn, 'name', { value: trappedProp });
|
|
|
|
|
const proxy = new Proxy(fn, {
|
|
|
|
|
defineProperty(target, prop) {
|
|
|
|
|
if ( prop !== 'toString' ) {
|
|
|
|
|
return Reflect.deleteProperty(...arguments);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
deleteProperty(target, prop) {
|
|
|
|
|
if ( prop !== 'toString' ) {
|
|
|
|
|
return Reflect.deleteProperty(...arguments);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
get(target, prop) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return function() {
|
|
|
|
|
return `function ${trappedProp}() { [native code] }`;
|
|
|
|
|
}.bind(null);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(...arguments);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return proxy;
|
|
|
|
|
};
|
|
|
|
|
if ( cValue === 'undefined' ) {
|
|
|
|
|
cValue = undefined;
|
|
|
|
|
} else if ( cValue === 'false' ) {
|
|
|
|
|
cValue = false;
|
|
|
|
|
} else if ( cValue === 'true' ) {
|
|
|
|
|
cValue = true;
|
|
|
|
|
} else if ( cValue === 'null' ) {
|
|
|
|
|
cValue = null;
|
|
|
|
|
} else if ( cValue === "''" ) {
|
|
|
|
|
cValue = '';
|
|
|
|
|
} else if ( cValue === '[]' ) {
|
|
|
|
|
cValue = [];
|
|
|
|
|
} else if ( cValue === '{}' ) {
|
|
|
|
|
cValue = {};
|
|
|
|
|
} else if ( cValue === 'noopFunc' ) {
|
|
|
|
|
cValue = cloakFunc(function(){});
|
|
|
|
|
} else if ( cValue === 'trueFunc' ) {
|
|
|
|
|
cValue = cloakFunc(function(){ return true; });
|
|
|
|
|
} else if ( cValue === 'falseFunc' ) {
|
|
|
|
|
cValue = cloakFunc(function(){ return false; });
|
|
|
|
|
} else if ( /^-?\d+$/.test(cValue) ) {
|
|
|
|
|
cValue = parseInt(cValue);
|
|
|
|
|
if ( isNaN(cValue) ) { return; }
|
|
|
|
|
if ( Math.abs(cValue) > 0x7FFF ) { return; }
|
|
|
|
|
} else if ( trusted ) {
|
|
|
|
|
if ( cValue.startsWith('{') && cValue.endsWith('}') ) {
|
|
|
|
|
try { cValue = JSON.parse(cValue).value; } catch(ex) { return; }
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( options.includes('asFunction') ) {
|
|
|
|
|
cValue = ( ) => cValue;
|
|
|
|
|
} else if ( options.includes('asCallback') ) {
|
|
|
|
|
cValue = ( ) => (( ) => cValue);
|
|
|
|
|
} else if ( options.includes('asResolved') ) {
|
|
|
|
|
cValue = Promise.resolve(cValue);
|
|
|
|
|
} else if ( options.includes('asRejected') ) {
|
|
|
|
|
cValue = Promise.reject(cValue);
|
|
|
|
|
}
|
|
|
|
|
let aborted = false;
|
|
|
|
|
const mustAbort = function(v) {
|
|
|
|
|
if ( trusted ) { return false; }
|
|
|
|
|
if ( aborted ) { return true; }
|
|
|
|
|
aborted =
|
|
|
|
|
(v !== undefined && v !== null) &&
|
|
|
|
|
(cValue !== undefined && cValue !== null) &&
|
|
|
|
|
(typeof v !== typeof cValue);
|
|
|
|
|
return aborted;
|
|
|
|
|
};
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/156
|
|
|
|
|
// Support multiple trappers for the same property.
|
|
|
|
|
const trapProp = function(owner, prop, configurable, handler) {
|
|
|
|
|
if ( handler.init(configurable ? owner[prop] : cValue) === false ) { return; }
|
|
|
|
|
const odesc = Object.getOwnPropertyDescriptor(owner, prop);
|
|
|
|
|
let prevGetter, prevSetter;
|
|
|
|
|
if ( odesc instanceof Object ) {
|
|
|
|
|
owner[prop] = cValue;
|
|
|
|
|
if ( odesc.get instanceof Function ) {
|
|
|
|
|
prevGetter = odesc.get;
|
|
|
|
|
}
|
|
|
|
|
if ( odesc.set instanceof Function ) {
|
|
|
|
|
prevSetter = odesc.set;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
objectDefineProperty(owner, prop, {
|
|
|
|
|
configurable,
|
|
|
|
|
get() {
|
|
|
|
|
if ( prevGetter !== undefined ) {
|
|
|
|
|
prevGetter();
|
|
|
|
|
}
|
|
|
|
|
return handler.getter(); // cValue
|
|
|
|
|
},
|
|
|
|
|
set(a) {
|
|
|
|
|
if ( prevSetter !== undefined ) {
|
|
|
|
|
prevSetter(a);
|
|
|
|
|
}
|
|
|
|
|
handler.setter(a);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const trapChain = function(owner, chain) {
|
|
|
|
|
const pos = chain.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
trapProp(owner, chain, false, {
|
|
|
|
|
v: undefined,
|
|
|
|
|
init: function(v) {
|
|
|
|
|
if ( mustAbort(v) ) { return false; }
|
|
|
|
|
this.v = v;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
getter: function() {
|
|
|
|
|
return document.currentScript === thisScript
|
|
|
|
|
? this.v
|
|
|
|
|
: cValue;
|
|
|
|
|
},
|
|
|
|
|
setter: function(a) {
|
|
|
|
|
if ( mustAbort(a) === false ) { return; }
|
|
|
|
|
cValue = a;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const prop = chain.slice(0, pos);
|
|
|
|
|
const v = owner[prop];
|
|
|
|
|
chain = chain.slice(pos + 1);
|
|
|
|
|
if ( v instanceof Object || typeof v === 'object' && v !== null ) {
|
|
|
|
|
trapChain(v, chain);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
trapProp(owner, prop, true, {
|
|
|
|
|
v: undefined,
|
|
|
|
|
init: function(v) {
|
|
|
|
|
this.v = v;
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
getter: function() {
|
|
|
|
|
return this.v;
|
|
|
|
|
},
|
|
|
|
|
setter: function(a) {
|
|
|
|
|
this.v = a;
|
|
|
|
|
if ( a instanceof Object ) {
|
|
|
|
|
trapChain(a, chain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
trapChain(window, chain);
|
|
|
|
|
}
|
|
|
|
|
runAt(( ) => {
|
|
|
|
|
setConstant(chain, cValue);
|
|
|
|
|
}, options);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 14:51:26 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'replace-node-text-core.fn',
|
|
|
|
|
fn: replaceNodeTextCore,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'get-extra-args.fn',
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
'run-at.fn',
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function replaceNodeTextCore(
|
|
|
|
|
nodeName = '',
|
|
|
|
|
pattern = '',
|
|
|
|
|
replacement = ''
|
|
|
|
|
) {
|
|
|
|
|
const reNodeName = patternToRegex(nodeName, 'i');
|
|
|
|
|
const rePattern = patternToRegex(pattern, 'gms');
|
|
|
|
|
const extraArgs = getExtraArgs(Array.from(arguments), 3);
|
|
|
|
|
const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.log || 0;
|
|
|
|
|
const reCondition = patternToRegex(extraArgs.condition || '', 'gms');
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
const stop = (takeRecord = true) => {
|
|
|
|
|
if ( takeRecord ) {
|
|
|
|
|
handleMutations(observer.takeRecords());
|
|
|
|
|
}
|
|
|
|
|
observer.disconnect();
|
|
|
|
|
if ( shouldLog !== 0 ) {
|
2023-05-28 16:37:13 +02:00
|
|
|
|
safe.uboLog(`replace-node-text-core.fn: quitting "${pattern}" => "${replacement}"`);
|
2023-05-25 14:51:26 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
let sedCount = extraArgs.sedCount || 0;
|
|
|
|
|
const handleNode = node => {
|
|
|
|
|
const before = node.textContent;
|
|
|
|
|
if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; }
|
|
|
|
|
if ( safe.RegExp_test.call(reCondition, before) === false ) { return true; }
|
|
|
|
|
const after = pattern !== ''
|
|
|
|
|
? before.replace(rePattern, replacement)
|
|
|
|
|
: replacement;
|
|
|
|
|
node.textContent = after;
|
|
|
|
|
if ( shouldLog !== 0 ) {
|
2023-05-28 16:37:13 +02:00
|
|
|
|
safe.uboLog('replace-node-text-core.fn before:\n', before);
|
|
|
|
|
safe.uboLog('replace-node-text-core.fn after:\n', after);
|
2023-05-25 14:51:26 +02:00
|
|
|
|
}
|
|
|
|
|
return sedCount === 0 || (sedCount -= 1) !== 0;
|
|
|
|
|
};
|
|
|
|
|
const handleMutations = mutations => {
|
|
|
|
|
for ( const mutation of mutations ) {
|
|
|
|
|
for ( const node of mutation.addedNodes ) {
|
|
|
|
|
if ( reNodeName.test(node.nodeName) === false ) { continue; }
|
|
|
|
|
if ( handleNode(node) ) { continue; }
|
|
|
|
|
stop(false); return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const observer = new MutationObserver(handleMutations);
|
|
|
|
|
observer.observe(document, { childList: true, subtree: true });
|
|
|
|
|
if ( document.documentElement ) {
|
|
|
|
|
const treeWalker = document.createTreeWalker(
|
|
|
|
|
document.documentElement,
|
|
|
|
|
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT
|
|
|
|
|
);
|
|
|
|
|
let count = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
const node = treeWalker.nextNode();
|
|
|
|
|
count += 1;
|
|
|
|
|
if ( node === null ) { break; }
|
|
|
|
|
if ( reNodeName.test(node.nodeName) === false ) { continue; }
|
|
|
|
|
if ( handleNode(node) ) { continue; }
|
|
|
|
|
stop(); break;
|
|
|
|
|
}
|
|
|
|
|
if ( shouldLog !== 0 ) {
|
2023-05-28 16:37:13 +02:00
|
|
|
|
safe.uboLog(`replace-node-text-core.fn ${count} nodes present before installing mutation observer`);
|
2023-05-25 14:51:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( extraArgs.stay ) { return; }
|
|
|
|
|
runAt(( ) => {
|
|
|
|
|
const quitAfter = extraArgs.quitAfter || 0;
|
|
|
|
|
if ( quitAfter !== 0 ) {
|
|
|
|
|
setTimeout(( ) => { stop(); }, quitAfter);
|
|
|
|
|
} else {
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
}, 'interactive');
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 20:37:02 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'object-prune.fn',
|
|
|
|
|
fn: objectPrune,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
// When no "prune paths" argument is provided, the scriptlet is
|
|
|
|
|
// used for logging purpose and the "needle paths" argument is
|
|
|
|
|
// used to filter logging output.
|
|
|
|
|
//
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1545
|
|
|
|
|
// - Add support for "remove everything if needle matches" case
|
|
|
|
|
function objectPrune(
|
|
|
|
|
obj,
|
|
|
|
|
rawPrunePaths,
|
|
|
|
|
rawNeedlePaths
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof rawPrunePaths !== 'string' ) { return; }
|
|
|
|
|
const prunePaths = rawPrunePaths !== ''
|
|
|
|
|
? rawPrunePaths.split(/ +/)
|
|
|
|
|
: [];
|
|
|
|
|
let needlePaths;
|
|
|
|
|
let log, reLogNeedle;
|
|
|
|
|
if ( prunePaths.length !== 0 ) {
|
|
|
|
|
needlePaths = prunePaths.length !== 0 && rawNeedlePaths !== ''
|
|
|
|
|
? rawNeedlePaths.split(/ +/)
|
|
|
|
|
: [];
|
|
|
|
|
} else {
|
|
|
|
|
log = console.log.bind(console);
|
|
|
|
|
reLogNeedle = patternToRegex(rawNeedlePaths);
|
|
|
|
|
}
|
|
|
|
|
const findOwner = function(root, path, prune = false) {
|
|
|
|
|
let owner = root;
|
|
|
|
|
let chain = path;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if ( typeof owner !== 'object' || owner === null ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const pos = chain.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
if ( prune === false ) {
|
|
|
|
|
return owner.hasOwnProperty(chain);
|
|
|
|
|
}
|
|
|
|
|
if ( chain === '*' ) {
|
|
|
|
|
for ( const key in owner ) {
|
|
|
|
|
if ( owner.hasOwnProperty(key) === false ) { continue; }
|
|
|
|
|
delete owner[key];
|
|
|
|
|
}
|
|
|
|
|
} else if ( owner.hasOwnProperty(chain) ) {
|
|
|
|
|
delete owner[chain];
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const prop = chain.slice(0, pos);
|
|
|
|
|
if (
|
|
|
|
|
prop === '[]' && Array.isArray(owner) ||
|
|
|
|
|
prop === '*' && owner instanceof Object
|
|
|
|
|
) {
|
|
|
|
|
const next = chain.slice(pos + 1);
|
|
|
|
|
let found = false;
|
|
|
|
|
for ( const key of Object.keys(owner) ) {
|
|
|
|
|
found = findOwner(owner[key], next, prune) || found;
|
|
|
|
|
}
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
if ( owner.hasOwnProperty(prop) === false ) { return false; }
|
|
|
|
|
owner = owner[prop];
|
|
|
|
|
chain = chain.slice(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const mustProcess = function(root) {
|
|
|
|
|
for ( const needlePath of needlePaths ) {
|
|
|
|
|
if ( findOwner(root, needlePath) === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
const json = JSON.stringify(obj, null, 2);
|
|
|
|
|
if ( reLogNeedle.test(json) ) {
|
|
|
|
|
log('uBO:', location.hostname, json);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
if ( mustProcess(obj) === false ) { return obj; }
|
|
|
|
|
for ( const path of prunePaths ) {
|
|
|
|
|
findOwner(obj, path, true);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
|
|
Injectable scriptlets
|
|
|
|
|
|
|
|
|
|
These are meant to be used in the MAIN (webpage) execution world.
|
|
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'abort-current-script.js',
|
|
|
|
|
aliases: [ 'acs.js', 'abort-current-inline-script.js', 'acis.js' ],
|
|
|
|
|
fn: abortCurrentScript,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
2023-05-24 16:32:03 +02:00
|
|
|
|
'abort-current-script-core.fn',
|
|
|
|
|
'run-at-html-element.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2022-06-24 19:35:20 +02:00
|
|
|
|
// Issues to mind before changing anything:
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function abortCurrentScript(
|
2023-05-24 16:32:03 +02:00
|
|
|
|
arg1,
|
|
|
|
|
arg2,
|
|
|
|
|
arg3
|
2023-03-24 19:05:18 +01:00
|
|
|
|
) {
|
2023-05-24 16:32:03 +02:00
|
|
|
|
runAtHtmlElement(( ) => {
|
|
|
|
|
abortCurrentScriptCore(arg1, arg2, arg3);
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'abort-on-property-read.js',
|
|
|
|
|
aliases: [ 'aopr.js' ],
|
|
|
|
|
fn: abortOnPropertyRead,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'get-exception-token.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function abortOnPropertyRead(
|
|
|
|
|
chain = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
|
|
|
|
if ( chain === '' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const exceptionToken = getExceptionToken();
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const abort = function() {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
};
|
|
|
|
|
const makeProxy = function(owner, chain) {
|
|
|
|
|
const pos = chain.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
const desc = Object.getOwnPropertyDescriptor(owner, chain);
|
|
|
|
|
if ( !desc || desc.get !== abort ) {
|
|
|
|
|
Object.defineProperty(owner, chain, {
|
|
|
|
|
get: abort,
|
|
|
|
|
set: function(){}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const prop = chain.slice(0, pos);
|
|
|
|
|
let v = owner[prop];
|
2019-07-22 13:32:39 +02:00
|
|
|
|
chain = chain.slice(pos + 1);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
if ( v ) {
|
|
|
|
|
makeProxy(v, chain);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const desc = Object.getOwnPropertyDescriptor(owner, prop);
|
|
|
|
|
if ( desc && desc.set !== undefined ) { return; }
|
|
|
|
|
Object.defineProperty(owner, prop, {
|
|
|
|
|
get: function() { return v; },
|
|
|
|
|
set: function(a) {
|
|
|
|
|
v = a;
|
|
|
|
|
if ( a instanceof Object ) {
|
|
|
|
|
makeProxy(a, chain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const owner = window;
|
|
|
|
|
makeProxy(owner, chain);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'abort-on-property-write.js',
|
|
|
|
|
aliases: [ 'aopw.js' ],
|
|
|
|
|
fn: abortOnPropertyWrite,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'get-exception-token.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function abortOnPropertyWrite(
|
|
|
|
|
prop = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof prop !== 'string' ) { return; }
|
|
|
|
|
if ( prop === '' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const exceptionToken = getExceptionToken();
|
2019-07-06 18:36:28 +02:00
|
|
|
|
let owner = window;
|
|
|
|
|
for (;;) {
|
|
|
|
|
const pos = prop.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) { break; }
|
|
|
|
|
owner = owner[prop.slice(0, pos)];
|
|
|
|
|
if ( owner instanceof Object === false ) { return; }
|
|
|
|
|
prop = prop.slice(pos + 1);
|
|
|
|
|
}
|
|
|
|
|
delete owner[prop];
|
|
|
|
|
Object.defineProperty(owner, prop, {
|
|
|
|
|
set: function() {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'abort-on-stack-trace.js',
|
|
|
|
|
aliases: [ 'aost.js' ],
|
|
|
|
|
fn: abortOnStackTrace,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'get-exception-token.fn',
|
2023-03-26 20:02:21 +02:00
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
'safe-self.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2020-09-22 15:59:04 +02:00
|
|
|
|
// Status is currently experimental
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function abortOnStackTrace(
|
|
|
|
|
chain = '',
|
|
|
|
|
needle = '',
|
|
|
|
|
logLevel = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
2023-03-26 18:31:36 +02:00
|
|
|
|
const safe = safeSelf();
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needle);
|
|
|
|
|
const exceptionToken = getExceptionToken();
|
2020-09-22 15:59:04 +02:00
|
|
|
|
const ErrorCtor = self.Error;
|
|
|
|
|
const mustAbort = function(err) {
|
|
|
|
|
let docURL = self.location.href;
|
|
|
|
|
const pos = docURL.indexOf('#');
|
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
|
docURL = docURL.slice(0, pos);
|
|
|
|
|
}
|
2020-09-23 12:54:44 +02:00
|
|
|
|
// Normalize stack trace
|
2023-03-26 18:31:36 +02:00
|
|
|
|
const reLine = /(.*?@)?(\S+)(:\d+):\d+\)?$/;
|
2020-09-23 12:54:44 +02:00
|
|
|
|
const lines = [];
|
|
|
|
|
for ( let line of err.stack.split(/[\n\r]+/) ) {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
if ( line.includes(exceptionToken) ) { continue; }
|
2020-09-23 12:54:44 +02:00
|
|
|
|
line = line.trim();
|
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 match = safe.RegExp_exec.call(reLine, line);
|
2020-09-23 12:54:44 +02:00
|
|
|
|
if ( match === null ) { continue; }
|
|
|
|
|
let url = match[2];
|
|
|
|
|
if ( url.startsWith('(') ) { url = url.slice(1); }
|
|
|
|
|
if ( url === docURL ) {
|
2020-09-23 15:41:00 +02:00
|
|
|
|
url = 'inlineScript';
|
2020-09-23 12:54:44 +02:00
|
|
|
|
} else if ( url.startsWith('<anonymous>') ) {
|
|
|
|
|
url = 'injectedScript';
|
|
|
|
|
}
|
|
|
|
|
let fn = match[1] !== undefined
|
|
|
|
|
? match[1].slice(0, -1)
|
|
|
|
|
: line.slice(0, match.index).trim();
|
|
|
|
|
if ( fn.startsWith('at') ) { fn = fn.slice(2).trim(); }
|
|
|
|
|
let rowcol = match[3];
|
|
|
|
|
lines.push(' ' + `${fn} ${url}${rowcol}:1`.trim());
|
|
|
|
|
}
|
|
|
|
|
lines[0] = `stackDepth:${lines.length-1}`;
|
|
|
|
|
const stack = lines.join('\t');
|
2023-03-26 18:31:36 +02:00
|
|
|
|
const r = safe.RegExp_test.call(reNeedle, stack);
|
2020-09-22 15:59:04 +02:00
|
|
|
|
if (
|
|
|
|
|
logLevel === '1' ||
|
|
|
|
|
logLevel === '2' && r ||
|
|
|
|
|
logLevel === '3' && !r
|
|
|
|
|
) {
|
2023-04-02 18:01:58 +02:00
|
|
|
|
safe.uboLog(stack.replace(/\t/g, '\n'));
|
2020-09-22 15:59:04 +02:00
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
};
|
|
|
|
|
const makeProxy = function(owner, chain) {
|
|
|
|
|
const pos = chain.indexOf('.');
|
|
|
|
|
if ( pos === -1 ) {
|
|
|
|
|
let v = owner[chain];
|
|
|
|
|
Object.defineProperty(owner, chain, {
|
|
|
|
|
get: function() {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
if ( mustAbort(new ErrorCtor(exceptionToken)) ) {
|
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2020-09-22 15:59:04 +02:00
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
},
|
|
|
|
|
set: function(a) {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
if ( mustAbort(new ErrorCtor(exceptionToken)) ) {
|
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2020-09-22 15:59:04 +02:00
|
|
|
|
}
|
|
|
|
|
v = a;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const prop = chain.slice(0, pos);
|
|
|
|
|
let v = owner[prop];
|
|
|
|
|
chain = chain.slice(pos + 1);
|
|
|
|
|
if ( v ) {
|
|
|
|
|
makeProxy(v, chain);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const desc = Object.getOwnPropertyDescriptor(owner, prop);
|
|
|
|
|
if ( desc && desc.set !== undefined ) { return; }
|
|
|
|
|
Object.defineProperty(owner, prop, {
|
|
|
|
|
get: function() { return v; },
|
|
|
|
|
set: function(a) {
|
|
|
|
|
v = a;
|
|
|
|
|
if ( a instanceof Object ) {
|
|
|
|
|
makeProxy(a, chain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const owner = window;
|
|
|
|
|
makeProxy(owner, chain);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2020-09-22 15:59:04 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2020-09-22 15:59:04 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'addEventListener-defuser.js',
|
|
|
|
|
aliases: [ 'aeld.js' ],
|
|
|
|
|
fn: addEventListenerDefuser,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
'get-extra-args.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
'pattern-to-regex.fn',
|
2023-04-27 18:52:17 +02:00
|
|
|
|
'run-at.fn',
|
2023-03-26 20:02:21 +02:00
|
|
|
|
'safe-self.fn',
|
2023-04-02 18:01:58 +02:00
|
|
|
|
'should-debug.fn',
|
|
|
|
|
'should-log.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2021-05-26 13:22:17 +02:00
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function addEventListenerDefuser(
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
type = '',
|
|
|
|
|
pattern = ''
|
2023-03-24 19:05:18 +01:00
|
|
|
|
) {
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
const extraArgs = getExtraArgs(Array.from(arguments), 2);
|
2023-03-26 20:02:21 +02:00
|
|
|
|
const safe = safeSelf();
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reType = patternToRegex(type);
|
|
|
|
|
const rePattern = patternToRegex(pattern);
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
const log = shouldLog(extraArgs);
|
|
|
|
|
const debug = shouldDebug(extraArgs);
|
2023-04-27 18:52:17 +02:00
|
|
|
|
const trapEddEventListeners = ( ) => {
|
|
|
|
|
const eventListenerHandler = {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
let type, handler;
|
|
|
|
|
try {
|
|
|
|
|
type = String(args[0]);
|
|
|
|
|
handler = String(args[1]);
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
const matchesType = safe.RegExp_test.call(reType, type);
|
|
|
|
|
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
|
|
|
|
|
const matchesEither = matchesType || matchesHandler;
|
|
|
|
|
const matchesBoth = matchesType && matchesHandler;
|
|
|
|
|
if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
|
|
|
|
|
safe.uboLog(`addEventListener('${type}', ${handler})`);
|
|
|
|
|
}
|
|
|
|
|
if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
|
|
|
|
|
debugger; // jshint ignore:line
|
|
|
|
|
}
|
|
|
|
|
if ( matchesBoth ) { return; }
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
2023-05-28 20:56:31 +02:00
|
|
|
|
},
|
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
2023-04-27 18:52:17 +02:00
|
|
|
|
};
|
|
|
|
|
self.EventTarget.prototype.addEventListener = new Proxy(
|
|
|
|
|
self.EventTarget.prototype.addEventListener,
|
|
|
|
|
eventListenerHandler
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
runAt(( ) => {
|
|
|
|
|
trapEddEventListeners();
|
Simplify passing extra parameters in scriptlets
When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.
As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:
github.com##+js(aeld, click, , log, 1)
github.com##+js(aeld, , , runAt, idle, log, 1)
The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
2023-05-24 17:59:17 +02:00
|
|
|
|
}, extraArgs.runAt);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'json-prune.js',
|
|
|
|
|
fn: jsonPrune,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
2023-05-29 20:37:02 +02:00
|
|
|
|
'object-prune.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2020-04-28 15:47:03 +02:00
|
|
|
|
// When no "prune paths" argument is provided, the scriptlet is
|
|
|
|
|
// used for logging purpose and the "needle paths" argument is
|
|
|
|
|
// used to filter logging output.
|
2021-04-06 16:12:34 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1545
|
|
|
|
|
// - Add support for "remove everything if needle matches" case
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function jsonPrune(
|
|
|
|
|
rawPrunePaths = '',
|
|
|
|
|
rawNeedlePaths = ''
|
|
|
|
|
) {
|
2019-09-09 20:06:23 +02:00
|
|
|
|
JSON.parse = new Proxy(JSON.parse, {
|
2023-05-29 20:37:02 +02:00
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
return objectPrune(
|
|
|
|
|
Reflect.apply(target, thisArg, args),
|
|
|
|
|
rawPrunePaths,
|
|
|
|
|
rawNeedlePaths
|
|
|
|
|
);
|
2020-11-08 14:45:33 +01:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
Response.prototype.json = new Proxy(Response.prototype.json, {
|
2023-05-29 20:37:02 +02:00
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
return Reflect.apply(target, thisArg, args).then(o =>
|
|
|
|
|
objectPrune(o, rawPrunePaths, rawNeedlePaths)
|
|
|
|
|
);
|
2019-09-09 20:06:23 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-09-09 20:06:23 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-09-09 20:06:23 +02:00
|
|
|
|
|
2023-05-29 20:37:02 +02:00
|
|
|
|
// There is still code out there which uses `eval` in lieu of `JSON.parse`.
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'evaldata-prune.js',
|
|
|
|
|
fn: evaldataPrune,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'object-prune.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function evaldataPrune(
|
|
|
|
|
rawPrunePaths = '',
|
|
|
|
|
rawNeedlePaths = ''
|
|
|
|
|
) {
|
|
|
|
|
self.eval = new Proxy(self.eval, {
|
|
|
|
|
apply(target, thisArg, args) {
|
2023-05-30 14:38:35 +02:00
|
|
|
|
let data = Reflect.apply(target, thisArg, args);
|
|
|
|
|
if ( typeof data === 'object' ) {
|
|
|
|
|
data = objectPrune(data, rawPrunePaths, rawNeedlePaths);
|
2023-05-29 20:37:02 +02:00
|
|
|
|
}
|
2023-05-30 14:38:35 +02:00
|
|
|
|
return data;
|
2023-05-29 20:37:02 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'nano-setInterval-booster.js',
|
|
|
|
|
aliases: [ 'nano-sib.js' ],
|
|
|
|
|
fn: nanoSetIntervalBooster,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2019-07-10 14:11:51 +02:00
|
|
|
|
// Imported from:
|
|
|
|
|
// https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L126
|
|
|
|
|
//
|
|
|
|
|
// Speed up or down setInterval, 3 optional arguments.
|
|
|
|
|
// The payload matcher, a string literal or a JavaScript RegExp, defaults
|
|
|
|
|
// to match all.
|
|
|
|
|
// delayMatcher
|
|
|
|
|
// The delay matcher, an integer, defaults to 1000.
|
2021-01-23 15:45:44 +01:00
|
|
|
|
// Use `*` to match any delay.
|
2019-07-10 14:11:51 +02:00
|
|
|
|
// boostRatio - The delay multiplier when there is a match, 0.5 speeds up by
|
|
|
|
|
// 2 times and 2 slows down by 2 times, defaults to 0.05 or speed up
|
|
|
|
|
// 20 times. Speed up and down both cap at 50 times.
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function nanoSetIntervalBooster(
|
|
|
|
|
needleArg = '',
|
|
|
|
|
delayArg = '',
|
|
|
|
|
boostArg = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needleArg !== 'string' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needleArg);
|
2021-01-23 15:45:44 +01:00
|
|
|
|
let delay = delayArg !== '*' ? parseInt(delayArg, 10) : -1;
|
|
|
|
|
if ( isNaN(delay) || isFinite(delay) === false ) { delay = 1000; }
|
|
|
|
|
let boost = parseFloat(boostArg);
|
|
|
|
|
boost = isNaN(boost) === false && isFinite(boost)
|
|
|
|
|
? Math.min(Math.max(boost, 0.02), 50)
|
|
|
|
|
: 0.05;
|
|
|
|
|
self.setInterval = new Proxy(self.setInterval, {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
apply: function(target, thisArg, args) {
|
2021-01-23 15:45:44 +01:00
|
|
|
|
const [ a, b ] = args;
|
|
|
|
|
if (
|
|
|
|
|
(delay === -1 || b === delay) &&
|
|
|
|
|
reNeedle.test(a.toString())
|
|
|
|
|
) {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
args[1] = b * boost;
|
|
|
|
|
}
|
|
|
|
|
return target.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'nano-setTimeout-booster.js',
|
|
|
|
|
aliases: [ 'nano-stb.js' ],
|
|
|
|
|
fn: nanoSetTimeoutBooster,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2019-07-10 14:11:51 +02:00
|
|
|
|
// Imported from:
|
|
|
|
|
// https://github.com/NanoAdblocker/NanoFilters/blob/1f3be7211bb0809c5106996f52564bf10c4525f7/NanoFiltersSource/NanoResources.txt#L82
|
|
|
|
|
//
|
|
|
|
|
// Speed up or down setTimeout, 3 optional arguments.
|
|
|
|
|
// funcMatcher
|
|
|
|
|
// The payload matcher, a string literal or a JavaScript RegExp, defaults
|
|
|
|
|
// to match all.
|
|
|
|
|
// delayMatcher
|
|
|
|
|
// The delay matcher, an integer, defaults to 1000.
|
2021-01-23 15:45:44 +01:00
|
|
|
|
// Use `*` to match any delay.
|
2019-07-10 14:11:51 +02:00
|
|
|
|
// boostRatio - The delay multiplier when there is a match, 0.5 speeds up by
|
|
|
|
|
// 2 times and 2 slows down by 2 times, defaults to 0.05 or speed up
|
|
|
|
|
// 20 times. Speed up and down both cap at 50 times.
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function nanoSetTimeoutBooster(
|
|
|
|
|
needleArg = '',
|
|
|
|
|
delayArg = '',
|
|
|
|
|
boostArg = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needleArg !== 'string' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needleArg);
|
2021-01-23 15:45:44 +01:00
|
|
|
|
let delay = delayArg !== '*' ? parseInt(delayArg, 10) : -1;
|
|
|
|
|
if ( isNaN(delay) || isFinite(delay) === false ) { delay = 1000; }
|
|
|
|
|
let boost = parseFloat(boostArg);
|
|
|
|
|
boost = isNaN(boost) === false && isFinite(boost)
|
|
|
|
|
? Math.min(Math.max(boost, 0.02), 50)
|
|
|
|
|
: 0.05;
|
|
|
|
|
self.setTimeout = new Proxy(self.setTimeout, {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
apply: function(target, thisArg, args) {
|
2021-01-23 15:45:44 +01:00
|
|
|
|
const [ a, b ] = args;
|
|
|
|
|
if (
|
|
|
|
|
(delay === -1 || b === delay) &&
|
|
|
|
|
reNeedle.test(a.toString())
|
|
|
|
|
) {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
args[1] = b * boost;
|
|
|
|
|
}
|
|
|
|
|
return target.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'noeval-if.js',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
fn: noEvalIf,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2023-03-26 15:13:17 +02:00
|
|
|
|
function noEvalIf(
|
2023-03-24 19:05:18 +01:00
|
|
|
|
needle = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needle !== 'string' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needle);
|
|
|
|
|
window.eval = new Proxy(window.eval, { // jshint ignore: line
|
2019-07-06 18:36:28 +02:00
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
const a = args[0];
|
2023-03-26 15:13:17 +02:00
|
|
|
|
if ( reNeedle.test(a.toString()) ) { return; }
|
|
|
|
|
return target.apply(thisArg, args);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'no-fetch-if.js',
|
|
|
|
|
fn: noFetchIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function noFetchIf(
|
|
|
|
|
arg1 = '',
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof arg1 !== 'string' ) { return; }
|
2020-12-11 14:29:23 +01:00
|
|
|
|
const needles = [];
|
|
|
|
|
for ( const condition of arg1.split(/\s+/) ) {
|
2020-12-11 15:28:29 +01:00
|
|
|
|
if ( condition === '' ) { continue; }
|
2020-12-11 14:29:23 +01:00
|
|
|
|
const pos = condition.indexOf(':');
|
|
|
|
|
let key, value;
|
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
|
key = condition.slice(0, pos);
|
|
|
|
|
value = condition.slice(pos + 1);
|
|
|
|
|
} else {
|
|
|
|
|
key = 'url';
|
|
|
|
|
value = condition;
|
|
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
|
needles.push({ key, re: patternToRegex(value) });
|
2020-12-11 14:29:23 +01:00
|
|
|
|
}
|
2020-12-11 15:28:29 +01:00
|
|
|
|
const log = needles.length === 0 ? console.log.bind(console) : undefined;
|
2020-12-11 14:29:23 +01:00
|
|
|
|
self.fetch = new Proxy(self.fetch, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
let proceed = true;
|
|
|
|
|
try {
|
2020-12-24 14:26:30 +01:00
|
|
|
|
let details;
|
|
|
|
|
if ( args[0] instanceof self.Request ) {
|
|
|
|
|
details = args[0];
|
|
|
|
|
} else {
|
|
|
|
|
details = Object.assign({ url: args[0] }, args[1]);
|
|
|
|
|
}
|
|
|
|
|
const props = new Map();
|
|
|
|
|
for ( const prop in details ) {
|
|
|
|
|
let v = details[prop];
|
|
|
|
|
if ( typeof v !== 'string' ) {
|
|
|
|
|
try { v = JSON.stringify(v); }
|
|
|
|
|
catch(ex) { }
|
2020-12-11 14:29:23 +01:00
|
|
|
|
}
|
2020-12-24 14:26:30 +01:00
|
|
|
|
if ( typeof v !== 'string' ) { continue; }
|
|
|
|
|
props.set(prop, v);
|
2020-12-11 14:29:23 +01:00
|
|
|
|
}
|
2020-12-11 15:28:29 +01:00
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
const out = Array.from(props)
|
|
|
|
|
.map(a => `${a[0]}:${a[1]}`)
|
|
|
|
|
.join(' ');
|
|
|
|
|
log(`uBO: fetch(${out})`);
|
|
|
|
|
}
|
|
|
|
|
proceed = needles.length === 0;
|
2020-12-11 14:29:23 +01:00
|
|
|
|
for ( const { key, re } of needles ) {
|
|
|
|
|
if (
|
|
|
|
|
props.has(key) === false ||
|
|
|
|
|
re.test(props.get(key)) === false
|
|
|
|
|
) {
|
|
|
|
|
proceed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
return proceed
|
|
|
|
|
? Reflect.apply(target, thisArg, args)
|
|
|
|
|
: Promise.resolve(new Response());
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2021-04-11 13:11:09 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2021-04-11 13:11:09 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'refresh-defuser.js',
|
|
|
|
|
fn: refreshDefuser,
|
|
|
|
|
});
|
2021-10-03 15:46:24 +02:00
|
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function refreshDefuser(
|
|
|
|
|
arg1 = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof arg1 !== 'string' ) { return; }
|
2021-10-03 15:46:24 +02:00
|
|
|
|
const defuse = ( ) => {
|
|
|
|
|
const meta = document.querySelector('meta[http-equiv="refresh" i][content]');
|
|
|
|
|
if ( meta === null ) { return; }
|
2023-03-24 19:05:18 +01:00
|
|
|
|
const s = arg1 === ''
|
2021-10-03 15:46:24 +02:00
|
|
|
|
? meta.getAttribute('content')
|
|
|
|
|
: arg1;
|
|
|
|
|
const ms = Math.max(parseFloat(s) || 0, 0) * 1000;
|
|
|
|
|
setTimeout(( ) => { window.stop(); }, ms);
|
|
|
|
|
};
|
|
|
|
|
if ( document.readyState === 'loading' ) {
|
|
|
|
|
document.addEventListener('DOMContentLoaded', defuse, { once: true });
|
|
|
|
|
} else {
|
|
|
|
|
defuse();
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2021-10-03 15:46:24 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2021-10-03 15:46:24 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'remove-attr.js',
|
|
|
|
|
aliases: [ 'ra.js' ],
|
|
|
|
|
fn: removeAttr,
|
|
|
|
|
});
|
|
|
|
|
function removeAttr(
|
|
|
|
|
token = '',
|
|
|
|
|
selector = '',
|
|
|
|
|
behavior = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof token !== 'string' ) { return; }
|
|
|
|
|
if ( token === '' ) { return; }
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const tokens = token.split(/\s*\|\s*/);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
if ( selector === '' ) {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
selector = `[${tokens.join('],[')}]`;
|
|
|
|
|
}
|
2021-01-08 16:45:35 +01:00
|
|
|
|
let timer;
|
|
|
|
|
const rmattr = ( ) => {
|
|
|
|
|
timer = undefined;
|
2019-07-06 18:36:28 +02:00
|
|
|
|
try {
|
|
|
|
|
const nodes = document.querySelectorAll(selector);
|
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
|
for ( const attr of tokens ) {
|
|
|
|
|
node.removeAttribute(attr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-01-08 16:45:35 +01:00
|
|
|
|
const mutationHandler = mutations => {
|
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
|
let skip = true;
|
|
|
|
|
for ( let i = 0; i < mutations.length && skip; i++ ) {
|
|
|
|
|
const { type, addedNodes, removedNodes } = mutations[i];
|
|
|
|
|
if ( type === 'attributes' ) { skip = false; }
|
|
|
|
|
for ( let j = 0; j < addedNodes.length && skip; j++ ) {
|
|
|
|
|
if ( addedNodes[j].nodeType === 1 ) { skip = false; break; }
|
|
|
|
|
}
|
|
|
|
|
for ( let j = 0; j < removedNodes.length && skip; j++ ) {
|
|
|
|
|
if ( removedNodes[j].nodeType === 1 ) { skip = false; break; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( skip ) { return; }
|
2021-07-03 15:19:24 +02:00
|
|
|
|
timer = self.requestIdleCallback(rmattr, { timeout: 17 });
|
2021-01-08 16:45:35 +01:00
|
|
|
|
};
|
|
|
|
|
const start = ( ) => {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
rmattr();
|
2021-01-08 16:45:35 +01:00
|
|
|
|
if ( /\bstay\b/.test(behavior) === false ) { return; }
|
|
|
|
|
const observer = new MutationObserver(mutationHandler);
|
2021-10-14 15:08:08 +02:00
|
|
|
|
observer.observe(document, {
|
2021-01-08 16:45:35 +01:00
|
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: tokens,
|
|
|
|
|
childList: true,
|
|
|
|
|
subtree: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) {
|
2021-01-08 19:11:09 +01:00
|
|
|
|
self.addEventListener('load', start, { once: true });
|
2021-07-03 15:19:24 +02:00
|
|
|
|
} else if ( document.readyState !== 'loading' || /\basap\b/.test(behavior) ) {
|
2021-01-08 16:45:35 +01:00
|
|
|
|
start();
|
2021-07-03 15:19:24 +02:00
|
|
|
|
} else {
|
|
|
|
|
self.addEventListener('DOMContentLoaded', start, { once: true });
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'remove-class.js',
|
|
|
|
|
aliases: [ 'rc.js' ],
|
|
|
|
|
fn: removeClass,
|
|
|
|
|
});
|
|
|
|
|
function removeClass(
|
|
|
|
|
token = '',
|
|
|
|
|
selector = '',
|
|
|
|
|
behavior = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof token !== 'string' ) { return; }
|
|
|
|
|
if ( token === '' ) { return; }
|
2020-03-18 14:44:18 +01:00
|
|
|
|
const tokens = token.split(/\s*\|\s*/);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
if ( selector === '' ) {
|
2020-03-18 14:44:18 +01:00
|
|
|
|
selector = '.' + tokens.map(a => CSS.escape(a)).join(',.');
|
|
|
|
|
}
|
2021-06-06 14:58:40 +02:00
|
|
|
|
let timer;
|
2020-03-18 14:44:18 +01:00
|
|
|
|
const rmclass = function() {
|
2021-06-06 14:58:40 +02:00
|
|
|
|
timer = undefined;
|
2020-03-18 14:44:18 +01:00
|
|
|
|
try {
|
|
|
|
|
const nodes = document.querySelectorAll(selector);
|
|
|
|
|
for ( const node of nodes ) {
|
|
|
|
|
node.classList.remove(...tokens);
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-06-06 14:58:40 +02:00
|
|
|
|
const mutationHandler = mutations => {
|
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
|
let skip = true;
|
|
|
|
|
for ( let i = 0; i < mutations.length && skip; i++ ) {
|
|
|
|
|
const { type, addedNodes, removedNodes } = mutations[i];
|
|
|
|
|
if ( type === 'attributes' ) { skip = false; }
|
|
|
|
|
for ( let j = 0; j < addedNodes.length && skip; j++ ) {
|
|
|
|
|
if ( addedNodes[j].nodeType === 1 ) { skip = false; break; }
|
|
|
|
|
}
|
|
|
|
|
for ( let j = 0; j < removedNodes.length && skip; j++ ) {
|
|
|
|
|
if ( removedNodes[j].nodeType === 1 ) { skip = false; break; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( skip ) { return; }
|
|
|
|
|
timer = self.requestIdleCallback(rmclass, { timeout: 67 });
|
|
|
|
|
};
|
|
|
|
|
const start = ( ) => {
|
2020-03-18 14:44:18 +01:00
|
|
|
|
rmclass();
|
2021-06-06 14:58:40 +02:00
|
|
|
|
if ( /\bstay\b/.test(behavior) === false ) { return; }
|
|
|
|
|
const observer = new MutationObserver(mutationHandler);
|
2021-10-14 15:08:08 +02:00
|
|
|
|
observer.observe(document, {
|
2021-06-06 14:58:40 +02:00
|
|
|
|
attributes: true,
|
|
|
|
|
attributeFilter: [ 'class' ],
|
|
|
|
|
childList: true,
|
|
|
|
|
subtree: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
if ( document.readyState !== 'complete' && /\bcomplete\b/.test(behavior) ) {
|
|
|
|
|
self.addEventListener('load', start, { once: true });
|
|
|
|
|
} else if ( document.readyState === 'loading' ) {
|
|
|
|
|
self.addEventListener('DOMContentLoaded', start, { once: true });
|
|
|
|
|
} else {
|
|
|
|
|
start();
|
2020-03-18 14:44:18 +01:00
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2020-03-18 14:44:18 +01:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2020-03-18 14:44:18 +01:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'no-requestAnimationFrame-if.js',
|
|
|
|
|
aliases: [ 'norafif.js' ],
|
|
|
|
|
fn: noRequestAnimationFrameIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function noRequestAnimationFrameIf(
|
|
|
|
|
needle = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needle !== 'string' ) { return; }
|
2020-04-15 16:06:53 +02:00
|
|
|
|
const needleNot = needle.charAt(0) === '!';
|
|
|
|
|
if ( needleNot ) { needle = needle.slice(1); }
|
|
|
|
|
const log = needleNot === false && needle === '' ? console.log : undefined;
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needle);
|
2020-04-15 16:06:53 +02:00
|
|
|
|
window.requestAnimationFrame = new Proxy(window.requestAnimationFrame, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
const a = String(args[0]);
|
|
|
|
|
let defuse = false;
|
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
log('uBO: requestAnimationFrame("%s")', a);
|
|
|
|
|
} else {
|
|
|
|
|
defuse = reNeedle.test(a) !== needleNot;
|
|
|
|
|
}
|
|
|
|
|
if ( defuse ) {
|
|
|
|
|
args[0] = function(){};
|
|
|
|
|
}
|
|
|
|
|
return target.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2020-04-15 16:06:53 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2020-04-15 16:06:53 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'set-constant.js',
|
|
|
|
|
aliases: [ 'set.js' ],
|
|
|
|
|
fn: setConstant,
|
2023-04-27 18:52:17 +02:00
|
|
|
|
dependencies: [
|
2023-05-23 16:59:27 +02:00
|
|
|
|
'set-constant-core.fn'
|
2023-04-27 18:52:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function setConstant(
|
2023-05-23 16:59:27 +02:00
|
|
|
|
...args
|
2023-03-24 19:05:18 +01:00
|
|
|
|
) {
|
2023-05-23 16:59:27 +02:00
|
|
|
|
setConstantCore(false, ...args);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'no-setInterval-if.js',
|
|
|
|
|
aliases: [ 'nosiif.js' ],
|
|
|
|
|
fn: noSetIntervalIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function noSetIntervalIf(
|
|
|
|
|
needle = '',
|
|
|
|
|
delay = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needle !== 'string' ) { return; }
|
2019-08-24 19:54:31 +02:00
|
|
|
|
const needleNot = needle.charAt(0) === '!';
|
|
|
|
|
if ( needleNot ) { needle = needle.slice(1); }
|
2023-03-24 19:05:18 +01:00
|
|
|
|
if ( delay === '' ) { delay = undefined; }
|
2020-07-02 17:47:49 +02:00
|
|
|
|
let delayNot = false;
|
|
|
|
|
if ( delay !== undefined ) {
|
|
|
|
|
delayNot = delay.charAt(0) === '!';
|
|
|
|
|
if ( delayNot ) { delay = delay.slice(1); }
|
|
|
|
|
delay = parseInt(delay, 10);
|
|
|
|
|
}
|
|
|
|
|
const log = needleNot === false && needle === '' && delay === undefined
|
2019-08-22 15:32:46 +02:00
|
|
|
|
? console.log
|
|
|
|
|
: undefined;
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needle);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
window.setInterval = new Proxy(window.setInterval, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
2019-08-22 15:32:46 +02:00
|
|
|
|
const a = String(args[0]);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const b = args[1];
|
2019-08-22 15:32:46 +02:00
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
log('uBO: setInterval("%s", %s)', a, b);
|
2019-09-15 17:01:50 +02:00
|
|
|
|
} else {
|
2020-07-02 17:47:49 +02:00
|
|
|
|
let defuse;
|
|
|
|
|
if ( needle !== '' ) {
|
|
|
|
|
defuse = reNeedle.test(a) !== needleNot;
|
|
|
|
|
}
|
|
|
|
|
if ( defuse !== false && delay !== undefined ) {
|
|
|
|
|
defuse = (b === delay || isNaN(b) && isNaN(delay) ) !== delayNot;
|
|
|
|
|
}
|
|
|
|
|
if ( defuse ) {
|
|
|
|
|
args[0] = function(){};
|
|
|
|
|
}
|
2019-08-22 15:32:46 +02:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
return target.apply(thisArg, args);
|
2023-05-19 18:55:01 +02:00
|
|
|
|
},
|
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
2019-07-06 18:36:28 +02:00
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'no-setTimeout-if.js',
|
|
|
|
|
aliases: [ 'nostif.js', 'setTimeout-defuser.js' ],
|
|
|
|
|
fn: noSetTimeoutIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function noSetTimeoutIf(
|
|
|
|
|
needle = '',
|
|
|
|
|
delay = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needle !== 'string' ) { return; }
|
2019-08-24 19:54:31 +02:00
|
|
|
|
const needleNot = needle.charAt(0) === '!';
|
|
|
|
|
if ( needleNot ) { needle = needle.slice(1); }
|
2023-03-24 19:05:18 +01:00
|
|
|
|
if ( delay === '' ) { delay = undefined; }
|
2020-07-02 17:47:49 +02:00
|
|
|
|
let delayNot = false;
|
|
|
|
|
if ( delay !== undefined ) {
|
|
|
|
|
delayNot = delay.charAt(0) === '!';
|
|
|
|
|
if ( delayNot ) { delay = delay.slice(1); }
|
|
|
|
|
delay = parseInt(delay, 10);
|
|
|
|
|
}
|
|
|
|
|
const log = needleNot === false && needle === '' && delay === undefined
|
2019-08-22 15:32:46 +02:00
|
|
|
|
? console.log
|
|
|
|
|
: undefined;
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reNeedle = patternToRegex(needle);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
window.setTimeout = new Proxy(window.setTimeout, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
2019-08-22 15:32:46 +02:00
|
|
|
|
const a = String(args[0]);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const b = args[1];
|
2019-08-22 15:32:46 +02:00
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
log('uBO: setTimeout("%s", %s)', a, b);
|
2019-09-15 17:01:50 +02:00
|
|
|
|
} else {
|
2020-07-02 17:47:49 +02:00
|
|
|
|
let defuse;
|
|
|
|
|
if ( needle !== '' ) {
|
|
|
|
|
defuse = reNeedle.test(a) !== needleNot;
|
|
|
|
|
}
|
|
|
|
|
if ( defuse !== false && delay !== undefined ) {
|
|
|
|
|
defuse = (b === delay || isNaN(b) && isNaN(delay) ) !== delayNot;
|
|
|
|
|
}
|
|
|
|
|
if ( defuse ) {
|
|
|
|
|
args[0] = function(){};
|
|
|
|
|
}
|
2019-08-22 15:32:46 +02:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
return target.apply(thisArg, args);
|
2023-05-19 18:55:01 +02:00
|
|
|
|
},
|
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
2019-07-06 18:36:28 +02:00
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'webrtc-if.js',
|
|
|
|
|
fn: webrtcIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function webrtcIf(
|
|
|
|
|
good = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof good !== 'string' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reGood = patternToRegex(good);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const rtcName = window.RTCPeerConnection
|
|
|
|
|
? 'RTCPeerConnection'
|
|
|
|
|
: (window.webkitRTCPeerConnection ? 'webkitRTCPeerConnection' : '');
|
|
|
|
|
if ( rtcName === '' ) { return; }
|
|
|
|
|
const log = console.log.bind(console);
|
|
|
|
|
const neuteredPeerConnections = new WeakSet();
|
|
|
|
|
const isGoodConfig = function(instance, config) {
|
|
|
|
|
if ( neuteredPeerConnections.has(instance) ) { return false; }
|
|
|
|
|
if ( config instanceof Object === false ) { return true; }
|
|
|
|
|
if ( Array.isArray(config.iceServers) === false ) { return true; }
|
|
|
|
|
for ( const server of config.iceServers ) {
|
|
|
|
|
const urls = typeof server.urls === 'string'
|
|
|
|
|
? [ server.urls ]
|
|
|
|
|
: server.urls;
|
|
|
|
|
if ( Array.isArray(urls) ) {
|
|
|
|
|
for ( const url of urls ) {
|
|
|
|
|
if ( reGood.test(url) ) { return true; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( typeof server.username === 'string' ) {
|
|
|
|
|
if ( reGood.test(server.username) ) { return true; }
|
|
|
|
|
}
|
|
|
|
|
if ( typeof server.credential === 'string' ) {
|
|
|
|
|
if ( reGood.test(server.credential) ) { return true; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
neuteredPeerConnections.add(instance);
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
const peerConnectionCtor = window[rtcName];
|
|
|
|
|
const peerConnectionProto = peerConnectionCtor.prototype;
|
|
|
|
|
peerConnectionProto.createDataChannel =
|
|
|
|
|
new Proxy(peerConnectionProto.createDataChannel, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
if ( isGoodConfig(target, args[1]) === false ) {
|
2020-04-28 17:19:26 +02:00
|
|
|
|
log('uBO:', args[1]);
|
2019-07-11 15:45:53 +02:00
|
|
|
|
return Reflect.apply(target, thisArg, args.slice(0, 1));
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
2019-07-11 15:45:53 +02:00
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
window[rtcName] =
|
|
|
|
|
new Proxy(peerConnectionCtor, {
|
|
|
|
|
construct: function(target, args) {
|
|
|
|
|
if ( isGoodConfig(target, args[0]) === false ) {
|
2020-04-28 17:19:26 +02:00
|
|
|
|
log('uBO:', args[0]);
|
2019-07-11 15:45:53 +02:00
|
|
|
|
return Reflect.construct(target);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
2019-07-11 15:45:53 +02:00
|
|
|
|
return Reflect.construct(target, args);
|
2019-07-06 18:36:28 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'no-xhr-if.js',
|
|
|
|
|
fn: noXhrIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function noXhrIf(
|
|
|
|
|
arg1 = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof arg1 !== 'string' ) { return; }
|
2021-09-11 15:15:39 +02:00
|
|
|
|
const xhrInstances = new WeakMap();
|
|
|
|
|
const needles = [];
|
|
|
|
|
for ( const condition of arg1.split(/\s+/) ) {
|
|
|
|
|
if ( condition === '' ) { continue; }
|
|
|
|
|
const pos = condition.indexOf(':');
|
|
|
|
|
let key, value;
|
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
|
key = condition.slice(0, pos);
|
|
|
|
|
value = condition.slice(pos + 1);
|
|
|
|
|
} else {
|
|
|
|
|
key = 'url';
|
|
|
|
|
value = condition;
|
|
|
|
|
}
|
2023-03-26 15:13:17 +02:00
|
|
|
|
needles.push({ key, re: patternToRegex(value) });
|
2021-09-11 15:15:39 +02:00
|
|
|
|
}
|
|
|
|
|
const log = needles.length === 0 ? console.log.bind(console) : undefined;
|
|
|
|
|
self.XMLHttpRequest = class extends self.XMLHttpRequest {
|
|
|
|
|
open(...args) {
|
|
|
|
|
if ( log !== undefined ) {
|
|
|
|
|
log(`uBO: xhr.open(${args.join(', ')})`);
|
|
|
|
|
} else {
|
|
|
|
|
const argNames = [ 'method', 'url' ];
|
|
|
|
|
const haystack = new Map();
|
|
|
|
|
for ( let i = 0; i < args.length && i < argNames.length; i++ ) {
|
|
|
|
|
haystack.set(argNames[i], args[i]);
|
|
|
|
|
}
|
|
|
|
|
if ( haystack.size !== 0 ) {
|
|
|
|
|
let matches = true;
|
|
|
|
|
for ( const { key, re } of needles ) {
|
|
|
|
|
matches = re.test(haystack.get(key) || '');
|
|
|
|
|
if ( matches === false ) { break; }
|
|
|
|
|
}
|
|
|
|
|
if ( matches ) {
|
|
|
|
|
xhrInstances.set(this, haystack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return super.open(...args);
|
|
|
|
|
}
|
|
|
|
|
send(...args) {
|
|
|
|
|
const haystack = xhrInstances.get(this);
|
|
|
|
|
if ( haystack === undefined ) {
|
|
|
|
|
return super.send(...args);
|
|
|
|
|
}
|
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
|
readyState: { value: 4, writable: false },
|
|
|
|
|
response: { value: '', writable: false },
|
|
|
|
|
responseText: { value: '', writable: false },
|
|
|
|
|
responseURL: { value: haystack.get('url'), writable: false },
|
|
|
|
|
responseXML: { value: '', writable: false },
|
|
|
|
|
status: { value: 200, writable: false },
|
|
|
|
|
statusText: { value: 'OK', writable: false },
|
|
|
|
|
});
|
2022-05-08 17:22:32 +02:00
|
|
|
|
this.dispatchEvent(new Event('readystatechange'));
|
|
|
|
|
this.dispatchEvent(new Event('load'));
|
|
|
|
|
this.dispatchEvent(new Event('loadend'));
|
2021-09-11 15:15:39 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2021-09-11 15:15:39 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2021-09-11 15:15:39 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'window-close-if.js',
|
|
|
|
|
fn: windowCloseIf,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
2021-12-13 14:14:30 +01:00
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/10323#issuecomment-992312847
|
|
|
|
|
// https://github.com/AdguardTeam/Scriptlets/issues/158
|
2022-09-17 18:46:42 +02:00
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/discussions/2270
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function windowCloseIf(
|
|
|
|
|
arg1 = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof arg1 !== 'string' ) { return; }
|
2022-09-17 18:46:42 +02:00
|
|
|
|
let subject = '';
|
2023-03-26 15:13:17 +02:00
|
|
|
|
if ( /^\/.*\/$/.test(arg1) ) {
|
2022-09-17 18:46:42 +02:00
|
|
|
|
subject = window.location.href;
|
2023-03-26 15:13:17 +02:00
|
|
|
|
} else if ( arg1 !== '' ) {
|
2022-09-17 18:46:42 +02:00
|
|
|
|
subject = `${window.location.pathname}${window.location.search}`;
|
2021-12-13 14:14:30 +01:00
|
|
|
|
}
|
|
|
|
|
try {
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const re = patternToRegex(arg1);
|
2022-09-17 18:46:42 +02:00
|
|
|
|
if ( re.test(subject) ) {
|
2021-12-13 14:14:30 +01:00
|
|
|
|
window.close();
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
console.log(ex);
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2021-12-13 14:14:30 +01:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2021-12-13 14:14:30 +01:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'window.name-defuser.js',
|
|
|
|
|
fn: windowNameDefuser,
|
|
|
|
|
});
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1228
|
|
|
|
|
function windowNameDefuser() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
if ( window === window.top ) {
|
|
|
|
|
window.name = '';
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'overlay-buster.js',
|
|
|
|
|
fn: overlayBuster,
|
|
|
|
|
});
|
2019-07-06 18:36:28 +02:00
|
|
|
|
// Experimental: Generic nuisance overlay buster.
|
|
|
|
|
// if this works well and proves to be useful, this may end up
|
|
|
|
|
// as a stock tool in uBO's popup panel.
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function overlayBuster() {
|
|
|
|
|
if ( window !== window.top ) { return; }
|
2019-07-06 18:36:28 +02:00
|
|
|
|
var tstart;
|
|
|
|
|
var ttl = 30000;
|
|
|
|
|
var delay = 0;
|
|
|
|
|
var delayStep = 50;
|
|
|
|
|
var buster = function() {
|
|
|
|
|
var docEl = document.documentElement,
|
|
|
|
|
bodyEl = document.body,
|
|
|
|
|
vw = Math.min(docEl.clientWidth, window.innerWidth),
|
|
|
|
|
vh = Math.min(docEl.clientHeight, window.innerHeight),
|
|
|
|
|
tol = Math.min(vw, vh) * 0.05,
|
|
|
|
|
el = document.elementFromPoint(vw/2, vh/2),
|
|
|
|
|
style, rect;
|
|
|
|
|
for (;;) {
|
|
|
|
|
if ( el === null || el.parentNode === null || el === bodyEl ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
style = window.getComputedStyle(el);
|
|
|
|
|
if ( parseInt(style.zIndex, 10) >= 1000 || style.position === 'fixed' ) {
|
|
|
|
|
rect = el.getBoundingClientRect();
|
|
|
|
|
if ( rect.left <= tol && rect.top <= tol && (vw - rect.right) <= tol && (vh - rect.bottom) < tol ) {
|
|
|
|
|
el.parentNode.removeChild(el);
|
|
|
|
|
tstart = Date.now();
|
|
|
|
|
el = document.elementFromPoint(vw/2, vh/2);
|
|
|
|
|
bodyEl.style.setProperty('overflow', 'auto', 'important');
|
|
|
|
|
docEl.style.setProperty('overflow', 'auto', 'important');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
el = el.parentNode;
|
|
|
|
|
}
|
|
|
|
|
if ( (Date.now() - tstart) < ttl ) {
|
|
|
|
|
delay = Math.min(delay + delayStep, 1000);
|
|
|
|
|
setTimeout(buster, delay);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var domReady = function(ev) {
|
|
|
|
|
if ( ev ) {
|
|
|
|
|
document.removeEventListener(ev.type, domReady);
|
|
|
|
|
}
|
|
|
|
|
tstart = Date.now();
|
|
|
|
|
setTimeout(buster, delay);
|
|
|
|
|
};
|
|
|
|
|
if ( document.readyState === 'loading' ) {
|
|
|
|
|
document.addEventListener('DOMContentLoaded', domReady);
|
|
|
|
|
} else {
|
|
|
|
|
domReady();
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'alert-buster.js',
|
|
|
|
|
fn: alertBuster,
|
|
|
|
|
});
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/8
|
|
|
|
|
function alertBuster() {
|
2022-05-08 17:22:32 +02:00
|
|
|
|
window.alert = new Proxy(window.alert, {
|
|
|
|
|
apply: function(a) {
|
|
|
|
|
console.info(a);
|
|
|
|
|
},
|
2023-05-20 23:18:44 +02:00
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
2022-05-08 17:22:32 +02:00
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'nowebrtc.js',
|
|
|
|
|
fn: noWebrtc,
|
|
|
|
|
});
|
|
|
|
|
// Prevent web pages from using RTCPeerConnection(), and report attempts in console.
|
|
|
|
|
function noWebrtc() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
var rtcName = window.RTCPeerConnection ? 'RTCPeerConnection' : (
|
|
|
|
|
window.webkitRTCPeerConnection ? 'webkitRTCPeerConnection' : ''
|
|
|
|
|
);
|
|
|
|
|
if ( rtcName === '' ) { return; }
|
|
|
|
|
var log = console.log.bind(console);
|
|
|
|
|
var pc = function(cfg) {
|
|
|
|
|
log('Document tried to create an RTCPeerConnection: %o', cfg);
|
|
|
|
|
};
|
|
|
|
|
const noop = function() {
|
|
|
|
|
};
|
|
|
|
|
pc.prototype = {
|
|
|
|
|
close: noop,
|
|
|
|
|
createDataChannel: noop,
|
|
|
|
|
createOffer: noop,
|
|
|
|
|
setRemoteDescription: noop,
|
|
|
|
|
toString: function() {
|
|
|
|
|
return '[object RTCPeerConnection]';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var z = window[rtcName];
|
|
|
|
|
window[rtcName] = pc.bind(window);
|
|
|
|
|
if ( z.prototype ) {
|
|
|
|
|
z.prototype.createDataChannel = function() {
|
|
|
|
|
return {
|
|
|
|
|
close: function() {},
|
|
|
|
|
send: function() {}
|
|
|
|
|
};
|
|
|
|
|
}.bind(null);
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'golem.de.js',
|
|
|
|
|
fn: golemDe,
|
|
|
|
|
});
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/88
|
|
|
|
|
function golemDe() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
const rael = window.addEventListener;
|
|
|
|
|
window.addEventListener = function(a, b) {
|
|
|
|
|
rael(...arguments);
|
|
|
|
|
let haystack;
|
|
|
|
|
try {
|
|
|
|
|
haystack = b.toString();
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
typeof haystack === 'string' &&
|
|
|
|
|
/^\s*function\s*\(\)\s*\{\s*window\.clearTimeout\(r\)\s*\}\s*$/.test(haystack)
|
|
|
|
|
) {
|
|
|
|
|
b();
|
|
|
|
|
}
|
|
|
|
|
}.bind(window);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'adfly-defuser.js',
|
|
|
|
|
fn: adflyDefuser,
|
|
|
|
|
});
|
2019-07-06 18:36:28 +02:00
|
|
|
|
// https://github.com/reek/anti-adblock-killer/issues/3774#issuecomment-348536138
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/883
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function adflyDefuser() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
// Based on AdsBypasser
|
|
|
|
|
// License:
|
|
|
|
|
// https://github.com/adsbypasser/adsbypasser/blob/master/LICENSE
|
|
|
|
|
var isDigit = /^\d$/;
|
|
|
|
|
var handler = function(encodedURL) {
|
|
|
|
|
var var1 = "", var2 = "", i;
|
|
|
|
|
for (i = 0; i < encodedURL.length; i++) {
|
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
|
var1 = var1 + encodedURL.charAt(i);
|
|
|
|
|
} else {
|
|
|
|
|
var2 = encodedURL.charAt(i) + var2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var data = (var1 + var2).split("");
|
|
|
|
|
for (i = 0; i < data.length; i++) {
|
|
|
|
|
if (isDigit.test(data[i])) {
|
|
|
|
|
for (var ii = i + 1; ii < data.length; ii++) {
|
|
|
|
|
if (isDigit.test(data[ii])) {
|
|
|
|
|
var temp = parseInt(data[i],10) ^ parseInt(data[ii],10);
|
|
|
|
|
if (temp < 10) {
|
|
|
|
|
data[i] = temp.toString();
|
|
|
|
|
}
|
|
|
|
|
i = ii;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
data = data.join("");
|
|
|
|
|
var decodedURL = window.atob(data).slice(16, -16);
|
|
|
|
|
window.stop();
|
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
|
window.location.href = decodedURL;
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
var val;
|
|
|
|
|
var flag = true;
|
|
|
|
|
window.Object.defineProperty(window, "ysmm", {
|
|
|
|
|
configurable: false,
|
|
|
|
|
set: function(value) {
|
|
|
|
|
if (flag) {
|
|
|
|
|
flag = false;
|
|
|
|
|
try {
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
|
handler(value);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) { }
|
|
|
|
|
}
|
|
|
|
|
val = value;
|
|
|
|
|
},
|
|
|
|
|
get: function() {
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
window.console.error("Failed to set up Adfly bypasser!");
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'disable-newtab-links.js',
|
|
|
|
|
fn: disableNewtabLinks,
|
|
|
|
|
});
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/913
|
|
|
|
|
function disableNewtabLinks() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
document.addEventListener('click', function(ev) {
|
|
|
|
|
var target = ev.target;
|
|
|
|
|
while ( target !== null ) {
|
|
|
|
|
if ( target.localName === 'a' && target.hasAttribute('target') ) {
|
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
target = target.parentNode;
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 18:36:28 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'cookie-remover.js',
|
|
|
|
|
fn: cookieRemover,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
|
|
|
|
'pattern-to-regex.fn',
|
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
// https://github.com/NanoAdblocker/NanoFilters/issues/149
|
|
|
|
|
function cookieRemover(
|
|
|
|
|
needle = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof needle !== 'string' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reName = patternToRegex(needle);
|
2020-06-24 23:18:14 +02:00
|
|
|
|
const removeCookie = function() {
|
2019-07-06 18:36:28 +02:00
|
|
|
|
document.cookie.split(';').forEach(cookieStr => {
|
|
|
|
|
let pos = cookieStr.indexOf('=');
|
|
|
|
|
if ( pos === -1 ) { return; }
|
|
|
|
|
let cookieName = cookieStr.slice(0, pos).trim();
|
|
|
|
|
if ( !reName.test(cookieName) ) { return; }
|
|
|
|
|
let part1 = cookieName + '=';
|
|
|
|
|
let part2a = '; domain=' + document.location.hostname;
|
|
|
|
|
let part2b = '; domain=.' + document.location.hostname;
|
2020-06-24 23:18:14 +02:00
|
|
|
|
let part2c, part2d;
|
2019-07-06 18:36:28 +02:00
|
|
|
|
let domain = document.domain;
|
2020-06-24 23:18:14 +02:00
|
|
|
|
if ( domain ) {
|
|
|
|
|
if ( domain !== document.location.hostname ) {
|
|
|
|
|
part2c = '; domain=.' + domain;
|
|
|
|
|
}
|
|
|
|
|
if ( domain.startsWith('www.') ) {
|
|
|
|
|
part2d = '; domain=' + domain.replace('www', '');
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
let part3 = '; path=/';
|
|
|
|
|
let part4 = '; Max-Age=-1000; expires=Thu, 01 Jan 1970 00:00:00 GMT';
|
|
|
|
|
document.cookie = part1 + part4;
|
|
|
|
|
document.cookie = part1 + part2a + part4;
|
|
|
|
|
document.cookie = part1 + part2b + part4;
|
|
|
|
|
document.cookie = part1 + part3 + part4;
|
|
|
|
|
document.cookie = part1 + part2a + part3 + part4;
|
|
|
|
|
document.cookie = part1 + part2b + part3 + part4;
|
|
|
|
|
if ( part2c !== undefined ) {
|
|
|
|
|
document.cookie = part1 + part2c + part3 + part4;
|
|
|
|
|
}
|
2020-06-24 23:18:14 +02:00
|
|
|
|
if ( part2d !== undefined ) {
|
|
|
|
|
document.cookie = part1 + part2d + part3 + part4;
|
|
|
|
|
}
|
2019-07-06 18:36:28 +02:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
removeCookie();
|
|
|
|
|
window.addEventListener('beforeunload', removeCookie);
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2019-07-08 14:56:36 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-08 14:56:36 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'xml-prune.js',
|
|
|
|
|
fn: xmlPrune,
|
2023-03-26 15:13:17 +02:00
|
|
|
|
dependencies: [
|
2023-05-27 23:26:19 +02:00
|
|
|
|
'get-extra-args.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
'pattern-to-regex.fn',
|
2023-05-27 23:26:19 +02:00
|
|
|
|
'safe-self.fn',
|
|
|
|
|
'should-log.fn',
|
2023-03-26 15:13:17 +02:00
|
|
|
|
],
|
2023-03-24 19:05:18 +01:00
|
|
|
|
});
|
|
|
|
|
function xmlPrune(
|
|
|
|
|
selector = '',
|
|
|
|
|
selectorCheck = '',
|
|
|
|
|
urlPattern = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof selector !== 'string' ) { return; }
|
2022-09-25 12:57:51 +02:00
|
|
|
|
if ( selector === '' ) { return; }
|
2023-03-26 15:13:17 +02:00
|
|
|
|
const reUrl = patternToRegex(urlPattern);
|
2023-05-27 23:26:19 +02:00
|
|
|
|
const extraArgs = getExtraArgs(Array.from(arguments), 3);
|
|
|
|
|
const log = shouldLog(extraArgs);
|
|
|
|
|
const queryAll = (xmlDoc, selector) => {
|
|
|
|
|
const isXpath = /^xpath\(.+\)$/.test(selector);
|
|
|
|
|
if ( isXpath === false ) {
|
|
|
|
|
return Array.from(xmlDoc.querySelectorAll(selector));
|
|
|
|
|
}
|
|
|
|
|
const xpr = xmlDoc.evaluate(
|
|
|
|
|
selector.slice(6, -1),
|
|
|
|
|
xmlDoc,
|
|
|
|
|
null,
|
|
|
|
|
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
const out = [];
|
|
|
|
|
for ( let i = 0; i < xpr.snapshotLength; i++ ) {
|
|
|
|
|
const node = xpr.snapshotItem(i);
|
|
|
|
|
out.push(node);
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
};
|
2023-05-27 15:14:19 +02:00
|
|
|
|
const pruneFromDoc = xmlDoc => {
|
2022-09-25 02:49:00 +02:00
|
|
|
|
try {
|
|
|
|
|
if ( selectorCheck !== '' && xmlDoc.querySelector(selectorCheck) === null ) {
|
2023-05-27 15:14:19 +02:00
|
|
|
|
return xmlDoc;
|
2022-09-25 02:49:00 +02:00
|
|
|
|
}
|
2023-06-05 14:51:20 +02:00
|
|
|
|
const items = queryAll(xmlDoc, selector);
|
|
|
|
|
if ( items.length !== 0 ) {
|
|
|
|
|
if ( log ) { safeSelf().uboLog(`xmlPrune: removing ${items.length} items`); }
|
|
|
|
|
for ( const item of items ) {
|
|
|
|
|
if ( item.nodeType === 1 ) {
|
|
|
|
|
item.remove();
|
|
|
|
|
} else if ( item.nodeType === 2 ) {
|
|
|
|
|
item.ownerElement.removeAttribute(item.nodeName);
|
|
|
|
|
}
|
|
|
|
|
if ( log ) {
|
|
|
|
|
safeSelf().uboLog(`xmlPrune: ${item.constructor.name}.${item.nodeName} removed`);
|
|
|
|
|
}
|
2022-09-25 02:49:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch(ex) {
|
2023-05-28 16:37:13 +02:00
|
|
|
|
if ( log ) { safeSelf().uboLog(ex); }
|
2022-09-25 02:49:00 +02:00
|
|
|
|
}
|
2023-05-27 15:14:19 +02:00
|
|
|
|
return xmlDoc;
|
|
|
|
|
};
|
|
|
|
|
const pruneFromText = text => {
|
|
|
|
|
if ( (/^\s*</.test(text) && />\s*$/.test(text)) === false ) {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const xmlParser = new DOMParser();
|
|
|
|
|
const xmlDoc = xmlParser.parseFromString(text, 'text/xml');
|
|
|
|
|
pruneFromDoc(xmlDoc);
|
|
|
|
|
const serializer = new XMLSerializer();
|
|
|
|
|
text = serializer.serializeToString(xmlDoc);
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
2022-09-25 02:49:00 +02:00
|
|
|
|
return text;
|
|
|
|
|
};
|
2022-09-25 12:49:41 +02:00
|
|
|
|
const urlFromArg = arg => {
|
|
|
|
|
if ( typeof arg === 'string' ) { return arg; }
|
|
|
|
|
if ( arg instanceof Request ) { return arg.url; }
|
|
|
|
|
return String(arg);
|
|
|
|
|
};
|
2022-09-25 02:49:00 +02:00
|
|
|
|
const realFetch = self.fetch;
|
|
|
|
|
self.fetch = new Proxy(self.fetch, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
2022-09-25 12:57:51 +02:00
|
|
|
|
if ( reUrl.test(urlFromArg(args[0])) === false ) {
|
2022-09-25 02:49:00 +02:00
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
2022-09-25 12:49:41 +02:00
|
|
|
|
return realFetch(...args).then(realResponse =>
|
|
|
|
|
realResponse.text().then(text =>
|
2023-05-27 15:14:19 +02:00
|
|
|
|
new Response(pruneFromText(text), {
|
2022-09-25 12:49:41 +02:00
|
|
|
|
status: realResponse.status,
|
|
|
|
|
statusText: realResponse.statusText,
|
|
|
|
|
headers: realResponse.headers,
|
|
|
|
|
})
|
|
|
|
|
)
|
2022-09-25 02:49:00 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-05-27 15:14:19 +02:00
|
|
|
|
self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, {
|
|
|
|
|
apply: async (target, thisArg, args) => {
|
|
|
|
|
if ( reUrl.test(urlFromArg(args[1])) === false ) {
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
thisArg.addEventListener('readystatechange', function() {
|
|
|
|
|
if ( thisArg.readyState !== 4 ) { return; }
|
|
|
|
|
const type = thisArg.responseType;
|
|
|
|
|
if ( type === 'text' ) {
|
|
|
|
|
const textin = thisArg.responseText;
|
|
|
|
|
const textout = pruneFromText(textin);
|
|
|
|
|
if ( textout === textin ) { return; }
|
|
|
|
|
Object.defineProperty(thisArg, 'response', { value: textout });
|
|
|
|
|
Object.defineProperty(thisArg, 'responseText', { value: textout });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( type === 'document' ) {
|
|
|
|
|
pruneFromDoc(thisArg.response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2022-09-25 02:49:00 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2019-07-08 14:56:36 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'm3u-prune.js',
|
|
|
|
|
fn: m3uPrune,
|
|
|
|
|
});
|
2022-09-27 04:27:50 +02:00
|
|
|
|
// https://en.wikipedia.org/wiki/M3U
|
2023-03-24 19:05:18 +01:00
|
|
|
|
function m3uPrune(
|
|
|
|
|
m3uPattern = '',
|
|
|
|
|
urlPattern = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof m3uPattern !== 'string' ) { return; }
|
2022-09-27 04:27:50 +02:00
|
|
|
|
const regexFromArg = arg => {
|
|
|
|
|
if ( arg === '' ) { return /^/; }
|
2023-03-12 22:45:02 +01:00
|
|
|
|
const match = /^\/(.+)\/([gms]*)$/.exec(arg);
|
|
|
|
|
if ( match !== null ) {
|
|
|
|
|
let flags = match[2] || '';
|
|
|
|
|
if ( flags.includes('m') ) { flags += 's'; }
|
|
|
|
|
return new RegExp(match[1], flags);
|
|
|
|
|
}
|
2022-10-24 18:37:04 +02:00
|
|
|
|
return new RegExp(
|
|
|
|
|
arg.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*+/g, '.*?')
|
|
|
|
|
);
|
2022-09-27 04:27:50 +02:00
|
|
|
|
};
|
|
|
|
|
const reM3u = regexFromArg(m3uPattern);
|
|
|
|
|
const reUrl = regexFromArg(urlPattern);
|
2022-09-27 04:37:11 +02:00
|
|
|
|
const pruneSpliceoutBlock = (lines, i) => {
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-CUE:TYPE="SpliceOut"') === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-ASSET:CAID') ) {
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
}
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-SCTE35:') ) {
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
}
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-CUE-IN') ) {
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
}
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-SCTE35:') ) {
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
const pruneInfBlock = (lines, i) => {
|
|
|
|
|
if ( lines[i].startsWith('#EXTINF') === false ) { return false; }
|
|
|
|
|
if ( reM3u.test(lines[i+1]) === false ) { return false; }
|
|
|
|
|
lines[i] = lines[i+1] = undefined; i += 2;
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-DISCONTINUITY') ) {
|
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2022-09-27 04:27:50 +02:00
|
|
|
|
const pruner = text => {
|
|
|
|
|
if ( (/^\s*#EXTM3U/.test(text)) === false ) { return text; }
|
2023-03-12 22:45:02 +01:00
|
|
|
|
if ( reM3u.multiline ) {
|
|
|
|
|
reM3u.lastIndex = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
const match = reM3u.exec(text);
|
|
|
|
|
if ( match === null ) { break; }
|
|
|
|
|
const before = text.slice(0, match.index);
|
|
|
|
|
if ( before.length === 0 || /[\n\r]+\s*$/.test(before) ) {
|
|
|
|
|
const after = text.slice(match.index + match[0].length);
|
|
|
|
|
if ( after.length === 0 || /^\s*[\n\r]+/.test(after) ) {
|
|
|
|
|
text = before.trim() + '\n' + after.trim();
|
|
|
|
|
reM3u.lastIndex = before.length + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( reM3u.global === false ) { break; }
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-27 04:37:11 +02:00
|
|
|
|
const lines = text.split(/\n\r|\n|\r/);
|
2022-09-27 04:27:50 +02:00
|
|
|
|
for ( let i = 0; i < lines.length; i++ ) {
|
2022-09-27 04:37:11 +02:00
|
|
|
|
if ( lines[i] === undefined ) { continue; }
|
|
|
|
|
if ( pruneSpliceoutBlock(lines, i) ) { continue; }
|
|
|
|
|
if ( pruneInfBlock(lines, i) ) { continue; }
|
|
|
|
|
}
|
|
|
|
|
return lines.filter(l => l !== undefined).join('\n');
|
2022-09-27 04:27:50 +02:00
|
|
|
|
};
|
|
|
|
|
const urlFromArg = arg => {
|
|
|
|
|
if ( typeof arg === 'string' ) { return arg; }
|
|
|
|
|
if ( arg instanceof Request ) { return arg.url; }
|
|
|
|
|
return String(arg);
|
|
|
|
|
};
|
|
|
|
|
const realFetch = self.fetch;
|
|
|
|
|
self.fetch = new Proxy(self.fetch, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
if ( reUrl.test(urlFromArg(args[0])) === false ) {
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
return realFetch(...args).then(realResponse =>
|
|
|
|
|
realResponse.text().then(text =>
|
|
|
|
|
new Response(pruner(text), {
|
|
|
|
|
status: realResponse.status,
|
|
|
|
|
statusText: realResponse.statusText,
|
|
|
|
|
headers: realResponse.headers,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, {
|
|
|
|
|
apply: async (target, thisArg, args) => {
|
|
|
|
|
if ( reUrl.test(urlFromArg(args[1])) === false ) {
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
thisArg.addEventListener('readystatechange', function() {
|
|
|
|
|
if ( thisArg.readyState !== 4 ) { return; }
|
|
|
|
|
const type = thisArg.responseType;
|
|
|
|
|
if ( type !== '' && type !== 'text' ) { return; }
|
|
|
|
|
const textin = thisArg.responseText;
|
|
|
|
|
const textout = pruner(textin);
|
|
|
|
|
if ( textout === textin ) { return; }
|
2022-09-27 04:37:11 +02:00
|
|
|
|
Object.defineProperty(thisArg, 'response', { value: textout });
|
|
|
|
|
Object.defineProperty(thisArg, 'responseText', { value: textout });
|
2022-09-27 04:27:50 +02:00
|
|
|
|
});
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2022-09-27 04:27:50 +02:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2022-09-27 04:27:50 +02:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'href-sanitizer.js',
|
|
|
|
|
fn: hrefSanitizer,
|
|
|
|
|
});
|
|
|
|
|
function hrefSanitizer(
|
|
|
|
|
selector = '',
|
|
|
|
|
source = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof selector !== 'string' ) { return; }
|
2023-03-09 14:49:26 +01:00
|
|
|
|
if ( selector === '' ) { return; }
|
2023-03-09 19:37:06 +01:00
|
|
|
|
if ( source === '' ) { source = 'text'; }
|
2023-03-09 14:49:26 +01:00
|
|
|
|
const sanitizeCopycats = (href, text) => {
|
|
|
|
|
let elems = [];
|
|
|
|
|
try {
|
|
|
|
|
elems = document.querySelectorAll(`a[href="${href}"`);
|
|
|
|
|
}
|
|
|
|
|
catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
for ( const elem of elems ) {
|
|
|
|
|
elem.setAttribute('href', text);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-30 15:13:46 +02:00
|
|
|
|
const validateURL = text => {
|
|
|
|
|
if ( text === '' ) { return ''; }
|
|
|
|
|
if ( /[^\x21-\x7e]/.test(text) ) { return ''; }
|
|
|
|
|
try {
|
|
|
|
|
const url = new URL(text, document.location);
|
|
|
|
|
return url.href;
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
};
|
2023-03-09 19:37:06 +01:00
|
|
|
|
const extractText = (elem, source) => {
|
|
|
|
|
if ( /^\[.*\]$/.test(source) ) {
|
2023-05-30 15:13:46 +02:00
|
|
|
|
return elem.getAttribute(source.slice(1,-1).trim()) || '';
|
|
|
|
|
}
|
|
|
|
|
if ( source.startsWith('?') ) {
|
|
|
|
|
try {
|
|
|
|
|
const url = new URL(elem.href, document.location);
|
|
|
|
|
return url.searchParams.get(source.slice(1)) || '';
|
|
|
|
|
} catch(x) {
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
if ( source === 'text' ) {
|
|
|
|
|
return elem.textContent
|
|
|
|
|
.replace(/^[^\x21-\x7e]+/, '') // remove leading invalid characters
|
|
|
|
|
.replace(/[^\x21-\x7e]+$/, '') // remove trailing invalid characters
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
return '';
|
2023-03-09 19:37:06 +01:00
|
|
|
|
};
|
2023-03-09 14:49:26 +01:00
|
|
|
|
const sanitize = ( ) => {
|
|
|
|
|
let elems = [];
|
|
|
|
|
try {
|
|
|
|
|
elems = document.querySelectorAll(selector);
|
|
|
|
|
}
|
|
|
|
|
catch(ex) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for ( const elem of elems ) {
|
|
|
|
|
if ( elem.localName !== 'a' ) { continue; }
|
|
|
|
|
if ( elem.hasAttribute('href') === false ) { continue; }
|
|
|
|
|
const href = elem.getAttribute('href');
|
2023-03-09 19:37:06 +01:00
|
|
|
|
const text = extractText(elem, source);
|
2023-05-30 15:13:46 +02:00
|
|
|
|
const hrefAfter = validateURL(text);
|
|
|
|
|
if ( hrefAfter === '' ) { continue; }
|
|
|
|
|
if ( hrefAfter === href ) { continue; }
|
|
|
|
|
elem.setAttribute('href', hrefAfter);
|
|
|
|
|
sanitizeCopycats(href, hrefAfter);
|
2023-03-09 14:49:26 +01:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
let observer, timer;
|
|
|
|
|
const onDomChanged = mutations => {
|
|
|
|
|
if ( timer !== undefined ) { return; }
|
|
|
|
|
let shouldSanitize = false;
|
|
|
|
|
for ( const mutation of mutations ) {
|
|
|
|
|
if ( mutation.addedNodes.length === 0 ) { continue; }
|
|
|
|
|
for ( const node of mutation.addedNodes ) {
|
|
|
|
|
if ( node.nodeType !== 1 ) { continue; }
|
|
|
|
|
shouldSanitize = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ( shouldSanitize ) { break; }
|
|
|
|
|
}
|
|
|
|
|
if ( shouldSanitize === false ) { return; }
|
|
|
|
|
timer = self.requestAnimationFrame(( ) => {
|
|
|
|
|
timer = undefined;
|
|
|
|
|
sanitize();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const start = ( ) => {
|
|
|
|
|
if ( sanitize() === false ) { return; }
|
|
|
|
|
observer = new MutationObserver(onDomChanged);
|
|
|
|
|
observer.observe(document.body, {
|
|
|
|
|
subtree: true,
|
|
|
|
|
childList: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
if ( document.readyState === 'loading' ) {
|
|
|
|
|
document.addEventListener('DOMContentLoaded', start, { once: true });
|
|
|
|
|
} else {
|
|
|
|
|
start();
|
|
|
|
|
}
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2023-03-09 14:49:26 +01:00
|
|
|
|
|
2023-03-26 15:13:17 +02:00
|
|
|
|
/******************************************************************************/
|
2023-03-09 14:49:26 +01:00
|
|
|
|
|
2023-03-24 19:05:18 +01:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'call-nothrow.js',
|
|
|
|
|
fn: callNothrow,
|
|
|
|
|
});
|
|
|
|
|
function callNothrow(
|
|
|
|
|
chain = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
|
|
|
|
if ( chain === '' ) { return; }
|
2023-03-14 23:50:01 +01:00
|
|
|
|
const parts = chain.split('.');
|
|
|
|
|
let owner = window, prop;
|
|
|
|
|
for (;;) {
|
|
|
|
|
prop = parts.shift();
|
|
|
|
|
if ( parts.length === 0 ) { break; }
|
|
|
|
|
owner = owner[prop];
|
|
|
|
|
if ( owner instanceof Object === false ) { return; }
|
|
|
|
|
}
|
|
|
|
|
if ( prop === '' ) { return; }
|
|
|
|
|
const fn = owner[prop];
|
|
|
|
|
if ( typeof fn !== 'function' ) { return; }
|
|
|
|
|
owner[prop] = new Proxy(fn, {
|
|
|
|
|
apply: function(...args) {
|
|
|
|
|
let r;
|
|
|
|
|
try {
|
|
|
|
|
r = Reflect.apply(...args);
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-03-24 19:05:18 +01:00
|
|
|
|
}
|
2023-03-14 23:50:01 +01:00
|
|
|
|
|
2023-05-20 23:18:44 +02:00
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'spoof-css.js',
|
|
|
|
|
fn: spoofCSS,
|
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
|
|
|
|
dependencies: [
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
],
|
2023-05-20 23:18:44 +02:00
|
|
|
|
});
|
|
|
|
|
function spoofCSS(
|
|
|
|
|
selector,
|
|
|
|
|
...args
|
|
|
|
|
) {
|
|
|
|
|
if ( typeof selector !== 'string' ) { return; }
|
|
|
|
|
if ( selector === '' ) { return; }
|
|
|
|
|
const toCamelCase = s => s.replace(/-[a-z]/g, s => s.charAt(1).toUpperCase());
|
|
|
|
|
const propToValueMap = new Map();
|
|
|
|
|
for ( let i = 0; i < args.length; i += 2 ) {
|
|
|
|
|
if ( typeof args[i+0] !== 'string' ) { break; }
|
|
|
|
|
if ( args[i+0] === '' ) { break; }
|
|
|
|
|
if ( typeof args[i+1] !== 'string' ) { break; }
|
|
|
|
|
propToValueMap.set(toCamelCase(args[i+0]), args[i+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 safe = safeSelf();
|
|
|
|
|
const canDebug = scriptletGlobals.has('canDebug');
|
|
|
|
|
const shouldDebug = canDebug && propToValueMap.get('debug') || 0;
|
|
|
|
|
const shouldLog = canDebug && propToValueMap.has('log') || 0;
|
|
|
|
|
const spoofStyle = (prop, real) => {
|
|
|
|
|
const normalProp = toCamelCase(prop);
|
|
|
|
|
const shouldSpoof = propToValueMap.has(normalProp);
|
|
|
|
|
const value = shouldSpoof ? propToValueMap.get(normalProp) : real;
|
|
|
|
|
if ( shouldLog === 2 || shouldSpoof && shouldLog === 1 ) {
|
|
|
|
|
safe.uboLog(prop, value);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
};
|
2023-05-20 23:18:44 +02:00
|
|
|
|
self.getComputedStyle = new Proxy(self.getComputedStyle, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
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
|
|
|
|
if ( shouldDebug !== 0 ) { debugger; } // jshint ignore: line
|
2023-05-20 23:18:44 +02:00
|
|
|
|
const style = Reflect.apply(target, thisArg, args);
|
|
|
|
|
const targetElements = new WeakSet(document.querySelectorAll(selector));
|
|
|
|
|
if ( targetElements.has(args[0]) === false ) { return style; }
|
|
|
|
|
const proxiedStyle = new Proxy(style, {
|
|
|
|
|
get(target, prop, receiver) {
|
2023-05-24 20:58:12 +02:00
|
|
|
|
if ( typeof target[prop] === 'function' ) {
|
2023-05-24 22:50:34 +02:00
|
|
|
|
if ( prop === 'getPropertyValue' ) {
|
|
|
|
|
return (function(prop) {
|
|
|
|
|
return spoofStyle(prop, target[prop]);
|
|
|
|
|
}).bind(target);
|
|
|
|
|
}
|
2023-05-24 20:58:12 +02:00
|
|
|
|
return target[prop].bind(target);
|
|
|
|
|
}
|
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
|
|
|
|
return spoofStyle(prop, Reflect.get(target, prop, receiver));
|
2023-05-20 23:18:44 +02:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return proxiedStyle;
|
|
|
|
|
},
|
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
Element.prototype.getBoundingClientRect = new Proxy(Element.prototype.getBoundingClientRect, {
|
|
|
|
|
apply: function(target, thisArg, args) {
|
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
|
|
|
|
if ( shouldDebug !== 0 ) { debugger; } // jshint ignore: line
|
2023-05-20 23:18:44 +02:00
|
|
|
|
const rect = Reflect.apply(target, thisArg, args);
|
|
|
|
|
const targetElements = new WeakSet(document.querySelectorAll(selector));
|
|
|
|
|
if ( targetElements.has(thisArg) === false ) { return rect; }
|
|
|
|
|
let { height, width } = rect;
|
|
|
|
|
if ( propToValueMap.has('width') ) {
|
|
|
|
|
width = parseFloat(propToValueMap.get('width'));
|
|
|
|
|
}
|
|
|
|
|
if ( propToValueMap.has('height') ) {
|
|
|
|
|
height = parseFloat(propToValueMap.get('height'));
|
|
|
|
|
}
|
|
|
|
|
return new self.DOMRect(rect.x, rect.y, width, height);
|
|
|
|
|
},
|
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
}
|
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 14:51:26 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'remove-node-text.js',
|
|
|
|
|
aliases: [ 'rmnt.js' ],
|
|
|
|
|
fn: removeNodeText,
|
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
dependencies: [
|
|
|
|
|
'replace-node-text-core.fn',
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function removeNodeText(
|
|
|
|
|
nodeName,
|
|
|
|
|
condition,
|
|
|
|
|
...extraArgs
|
|
|
|
|
) {
|
|
|
|
|
replaceNodeTextCore(nodeName, '', '', 'condition', condition || '', ...extraArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 15:03:19 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Scriplets below this section are only available for filter lists from
|
|
|
|
|
* trusted sources. They all have the property `requiresTrust` set to `true`.
|
|
|
|
|
*
|
|
|
|
|
* Trusted sources are:
|
|
|
|
|
*
|
|
|
|
|
* - uBO's own filter lists, which name starts with "uBlock filters – ", and
|
|
|
|
|
* maintained at: https://github.com/uBlockOrigin/uAssets
|
|
|
|
|
*
|
|
|
|
|
* - The user's own filters as seen in "My filters" pane in uBO's dashboard.
|
|
|
|
|
*
|
|
|
|
|
* The trustworthiness of filters using these privileged scriptlets are
|
|
|
|
|
* evaluated at filter list compiled time: when a filter using one of the
|
|
|
|
|
* privileged scriptlet originates from a non-trusted filter list source, it
|
|
|
|
|
* is discarded at compile time, specifically from within:
|
|
|
|
|
*
|
|
|
|
|
* - Source: ./src/js/scriptlet-filtering.js
|
|
|
|
|
* - Method: scriptletFilteringEngine.compile(), via normalizeRawFilter()
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
*
|
2023-05-24 21:56:42 +02:00
|
|
|
|
* replace-node-text.js
|
2023-05-23 15:03:19 +02:00
|
|
|
|
*
|
|
|
|
|
* Replace text instance(s) with another text instance inside specific
|
|
|
|
|
* DOM nodes. By default, the scriplet stops and quits at the interactive
|
|
|
|
|
* stage of a document.
|
|
|
|
|
*
|
|
|
|
|
* See commit messages for usage:
|
|
|
|
|
* - https://github.com/gorhill/uBlock/commit/99ce027fd702
|
|
|
|
|
* - https://github.com/gorhill/uBlock/commit/41876336db48
|
|
|
|
|
*
|
|
|
|
|
**/
|
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
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
2023-05-24 20:58:12 +02:00
|
|
|
|
name: 'replace-node-text.js',
|
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
|
|
|
|
requiresTrust: true,
|
2023-05-25 14:51:26 +02:00
|
|
|
|
aliases: [ 'rpnt.js', 'sed.js' /* to be removed */ ],
|
2023-05-24 20:58:12 +02:00
|
|
|
|
fn: replaceNodeText,
|
2023-05-23 02:19:00 +02:00
|
|
|
|
world: 'ISOLATED',
|
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
|
|
|
|
dependencies: [
|
2023-05-25 14:51:26 +02:00
|
|
|
|
'replace-node-text-core.fn',
|
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
|
|
|
|
],
|
|
|
|
|
});
|
2023-05-24 20:58:12 +02:00
|
|
|
|
function replaceNodeText(
|
2023-05-25 14:51:26 +02:00
|
|
|
|
nodeName,
|
|
|
|
|
pattern,
|
|
|
|
|
replacement,
|
|
|
|
|
...extraArgs
|
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
|
|
|
|
) {
|
2023-05-25 14:51:26 +02:00
|
|
|
|
replaceNodeTextCore(nodeName, pattern, replacement, ...extraArgs);
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 21:56:42 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* trusted-set-constant.js
|
|
|
|
|
*
|
|
|
|
|
* Set specified property to any value. This is essentially the same as
|
|
|
|
|
* set-constant.js, but with no restriction as to which values can be used.
|
|
|
|
|
*
|
|
|
|
|
**/
|
2023-05-23 16:59:27 +02:00
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'trusted-set-constant.js',
|
|
|
|
|
requiresTrust: true,
|
2023-05-23 23:58:20 +02:00
|
|
|
|
aliases: [ 'trusted-set.js' ],
|
2023-05-23 16:59:27 +02:00
|
|
|
|
fn: trustedSetConstant,
|
|
|
|
|
dependencies: [
|
|
|
|
|
'set-constant-core.fn'
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
function trustedSetConstant(
|
|
|
|
|
...args
|
|
|
|
|
) {
|
|
|
|
|
setConstantCore(true, ...args);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 17:08:35 +02:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* set-cookie.js
|
|
|
|
|
*
|
|
|
|
|
* Set specified cookie to a specific value.
|
|
|
|
|
*
|
|
|
|
|
* Reference:
|
|
|
|
|
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/set-cookie.js
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
name: 'set-cookie.js',
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
fn: setCookie,
|
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
});
|
|
|
|
|
function setCookie(
|
|
|
|
|
name = '',
|
|
|
|
|
value = '',
|
|
|
|
|
path = '/'
|
|
|
|
|
) {
|
|
|
|
|
if ( name === '' ) { return; }
|
|
|
|
|
const validValues = new Set([
|
|
|
|
|
'true', 'True',
|
|
|
|
|
'false', 'False',
|
|
|
|
|
'yes', 'Yes', 'y', 'Y',
|
|
|
|
|
'no', 'No', 'n', 'N',
|
|
|
|
|
'ok', 'OK',
|
|
|
|
|
]);
|
|
|
|
|
if ( validValues.has(value) === false ) {
|
|
|
|
|
if ( /^\d+$/.test(value) === false ) { return; }
|
|
|
|
|
const n = parseInt(value, 10);
|
|
|
|
|
if ( n < 0 || n > 15 ) { return; }
|
|
|
|
|
}
|
|
|
|
|
const validPaths = new Set([ '/', 'none' ]);
|
|
|
|
|
if ( validPaths.has(path) === false ) { return; }
|
|
|
|
|
const cookieParts = [
|
|
|
|
|
encodeURIComponent(name), '=',
|
|
|
|
|
encodeURIComponent(value),
|
|
|
|
|
];
|
|
|
|
|
if ( path !== 'none' ) {
|
|
|
|
|
cookieParts.push('; path=/');
|
|
|
|
|
}
|
|
|
|
|
document.cookie = cookieParts.join('');
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:59:27 +02:00
|
|
|
|
/******************************************************************************/
|