2019-07-06 16:36:28 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
2023-12-04 17:10:34 +00:00
|
|
|
|
uBlock Origin - a comprehensive, efficient content blocker
|
2019-07-06 16:36:28 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2024-11-08 16:22:31 +00:00
|
|
|
|
import './attribute.js';
|
2024-12-20 15:07:40 +00:00
|
|
|
|
import './href-sanitizer.js';
|
2025-03-28 13:40:22 +00:00
|
|
|
|
import './json-edit.js';
|
2025-03-22 20:01:43 +00:00
|
|
|
|
import './json-prune.js';
|
2025-02-19 19:01:27 +00:00
|
|
|
|
import './noeval.js';
|
2025-03-22 20:01:43 +00:00
|
|
|
|
import './object-prune.js';
|
2025-03-19 16:34:03 +00:00
|
|
|
|
import './prevent-innerHTML.js';
|
|
|
|
|
|
import './prevent-settimeout.js';
|
2024-11-27 20:47:17 +00:00
|
|
|
|
import './replace-argument.js';
|
2024-11-16 14:26:09 +00:00
|
|
|
|
import './spoof-css.js';
|
|
|
|
|
|
|
2025-03-22 20:01:43 +00:00
|
|
|
|
import {
|
|
|
|
|
|
getExceptionTokenFn,
|
|
|
|
|
|
getRandomTokenFn,
|
|
|
|
|
|
matchObjectPropertiesFn,
|
|
|
|
|
|
parsePropertiesToMatchFn,
|
|
|
|
|
|
} from './utils.js';
|
2024-11-16 14:26:09 +00:00
|
|
|
|
import { runAt, runAtHtmlElementFn } from './run-at.js';
|
2024-11-08 16:22:31 +00:00
|
|
|
|
|
|
|
|
|
|
import { getAllCookiesFn } from './cookie.js';
|
|
|
|
|
|
import { getAllLocalStorageFn } from './localstorage.js';
|
2025-03-22 20:01:43 +00:00
|
|
|
|
import { matchesStackTraceFn } from './stack-trace.js';
|
2024-11-27 20:47:17 +00:00
|
|
|
|
import { proxyApplyFn } from './proxy-apply.js';
|
2024-11-08 16:22:31 +00:00
|
|
|
|
import { registeredScriptlets } from './base.js';
|
2024-10-25 23:12:08 +00:00
|
|
|
|
import { safeSelf } from './safe-self.js';
|
2024-11-27 20:47:17 +00:00
|
|
|
|
import { validateConstantFn } from './set-constant.js';
|
2024-10-25 23:12:08 +00:00
|
|
|
|
|
2023-03-26 16:31:36 +00:00
|
|
|
|
// Externally added to the private namespace in which scriptlets execute.
|
2023-03-31 00:46:44 +00:00
|
|
|
|
/* global scriptletGlobals */
|
2023-03-26 16:31:36 +00:00
|
|
|
|
|
2024-11-08 16:22:31 +00:00
|
|
|
|
/* eslint no-prototype-builtins: 0 */
|
2024-10-25 23:12:08 +00:00
|
|
|
|
|
2024-11-08 16:22:31 +00:00
|
|
|
|
export const builtinScriptlets = registeredScriptlets;
|
2024-11-07 15:23:34 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
Helper functions
|
|
|
|
|
|
|
|
|
|
|
|
These are meant to be used as dependencies to injectable scriptlets.
|
|
|
|
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
2023-04-02 16:01:58 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'should-debug.fn',
|
|
|
|
|
|
fn: shouldDebug,
|
|
|
|
|
|
});
|
|
|
|
|
|
function shouldDebug(details) {
|
|
|
|
|
|
if ( details instanceof Object === false ) { return false; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
return scriptletGlobals.canDebug && details.debug;
|
2023-04-02 16:01:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-27 16:52:17 +00:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2023-11-25 16:13:57 +00:00
|
|
|
|
// Reference:
|
|
|
|
|
|
// https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/about-scriptlets.md#prevent-xhr
|
2024-10-03 17:31:52 +00:00
|
|
|
|
//
|
|
|
|
|
|
// Added `trusted` argument to allow for returning arbitrary text. Can only
|
|
|
|
|
|
// be used through scriptlets requiring trusted source.
|
2023-11-25 16:13:57 +00:00
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'generate-content.fn',
|
|
|
|
|
|
fn: generateContentFn,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
2024-10-03 17:31:52 +00:00
|
|
|
|
function generateContentFn(trusted, directive) {
|
2023-11-25 16:13:57 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const randomize = len => {
|
|
|
|
|
|
const chunks = [];
|
|
|
|
|
|
let textSize = 0;
|
|
|
|
|
|
do {
|
|
|
|
|
|
const s = safe.Math_random().toString(36).slice(2);
|
|
|
|
|
|
chunks.push(s);
|
|
|
|
|
|
textSize += s.length;
|
|
|
|
|
|
}
|
|
|
|
|
|
while ( textSize < len );
|
|
|
|
|
|
return chunks.join(' ').slice(0, len);
|
|
|
|
|
|
};
|
|
|
|
|
|
if ( directive === 'true' ) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
return randomize(10);
|
2023-11-25 16:13:57 +00:00
|
|
|
|
}
|
2024-01-01 15:24:47 +00:00
|
|
|
|
if ( directive === 'emptyObj' ) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
return '{}';
|
2024-01-01 15:24:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( directive === 'emptyArr' ) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
return '[]';
|
2024-01-01 15:24:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( directive === 'emptyStr' ) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
return '';
|
2024-01-01 15:24:47 +00:00
|
|
|
|
}
|
2023-11-25 16:13:57 +00:00
|
|
|
|
if ( directive.startsWith('length:') ) {
|
|
|
|
|
|
const match = /^length:(\d+)(?:-(\d+))?$/.exec(directive);
|
2024-10-03 17:31:52 +00:00
|
|
|
|
if ( match === null ) { return ''; }
|
|
|
|
|
|
const min = parseInt(match[1], 10);
|
|
|
|
|
|
const extent = safe.Math_max(parseInt(match[2], 10) || 0, min) - min;
|
|
|
|
|
|
const len = safe.Math_min(min + extent * safe.Math_random(), 500000);
|
|
|
|
|
|
return randomize(len | 0);
|
2023-11-25 16:13:57 +00:00
|
|
|
|
}
|
2024-10-03 17:31:52 +00:00
|
|
|
|
if ( directive.startsWith('war:') ) {
|
|
|
|
|
|
if ( scriptletGlobals.warOrigin === undefined ) { return ''; }
|
2023-11-25 16:13:57 +00:00
|
|
|
|
return new Promise(resolve => {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const warOrigin = scriptletGlobals.warOrigin;
|
2023-11-25 16:13:57 +00:00
|
|
|
|
const warName = directive.slice(4);
|
|
|
|
|
|
const fullpath = [ warOrigin, '/', warName ];
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const warSecret = scriptletGlobals.warSecret;
|
2023-11-25 16:13:57 +00:00
|
|
|
|
if ( warSecret !== undefined ) {
|
|
|
|
|
|
fullpath.push('?secret=', warSecret);
|
|
|
|
|
|
}
|
|
|
|
|
|
const warXHR = new safe.XMLHttpRequest();
|
|
|
|
|
|
warXHR.responseType = 'text';
|
|
|
|
|
|
warXHR.onloadend = ev => {
|
|
|
|
|
|
resolve(ev.target.responseText || '');
|
|
|
|
|
|
};
|
|
|
|
|
|
warXHR.open('GET', fullpath.join(''));
|
|
|
|
|
|
warXHR.send();
|
2024-10-03 17:31:52 +00:00
|
|
|
|
}).catch(( ) => '');
|
2023-11-25 16:13:57 +00:00
|
|
|
|
}
|
2024-10-03 17:31:52 +00:00
|
|
|
|
if ( trusted ) {
|
|
|
|
|
|
return directive;
|
|
|
|
|
|
}
|
|
|
|
|
|
return '';
|
2023-11-25 16:13:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2023-05-24 14:32:03 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'abort-current-script-core.fn',
|
|
|
|
|
|
fn: abortCurrentScriptCore,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'get-exception-token.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
'should-debug.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
// Issues to mind before changing anything:
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
|
|
|
|
|
|
function abortCurrentScriptCore(
|
2023-07-25 13:22:47 +00:00
|
|
|
|
target = '',
|
|
|
|
|
|
needle = '',
|
|
|
|
|
|
context = ''
|
2023-05-24 14:32:03 +00:00
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof target !== 'string' ) { return; }
|
|
|
|
|
|
if ( target === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('abort-current-script', target, needle, context);
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const reNeedle = safe.patternToRegex(needle);
|
|
|
|
|
|
const reContext = safe.patternToRegex(context);
|
|
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
2023-05-24 14:32:03 +00:00
|
|
|
|
const thisScript = document.currentScript;
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const chain = safe.String_split.call(target, '.');
|
2023-05-24 14:32:03 +00:00
|
|
|
|
let owner = window;
|
|
|
|
|
|
let prop;
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
prop = chain.shift();
|
|
|
|
|
|
if ( chain.length === 0 ) { break; }
|
2023-07-05 14:00:31 +00:00
|
|
|
|
if ( prop in owner === false ) { break; }
|
2023-05-24 14:32:03 +00:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2023-07-25 13:22:47 +00:00
|
|
|
|
const debug = shouldDebug(extraArgs);
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const exceptionToken = getExceptionTokenFn();
|
2023-05-24 14:32:03 +00:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2023-05-24 14:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
scriptTexts.set(elem, text);
|
|
|
|
|
|
return text;
|
|
|
|
|
|
};
|
|
|
|
|
|
const validate = ( ) => {
|
|
|
|
|
|
const e = document.currentScript;
|
|
|
|
|
|
if ( e instanceof HTMLScriptElement === false ) { return; }
|
|
|
|
|
|
if ( e === thisScript ) { return; }
|
2023-08-19 23:21:22 +00:00
|
|
|
|
if ( context !== '' && reContext.test(e.src) === false ) {
|
2024-03-20 13:31:17 +00:00
|
|
|
|
// eslint-disable-next-line no-debugger
|
|
|
|
|
|
if ( debug === 'nomatch' || debug === 'all' ) { debugger; }
|
2023-08-19 23:21:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-01-27 11:43:36 +00:00
|
|
|
|
if ( safe.logLevel > 1 && context !== '' ) {
|
2024-01-27 03:38:31 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Matched src\n${e.src}`);
|
2024-01-25 17:20:38 +00:00
|
|
|
|
}
|
2023-05-24 14:32:03 +00:00
|
|
|
|
const scriptText = getScriptText(e);
|
2023-08-19 23:21:22 +00:00
|
|
|
|
if ( reNeedle.test(scriptText) === false ) {
|
2024-03-20 13:31:17 +00:00
|
|
|
|
// eslint-disable-next-line no-debugger
|
|
|
|
|
|
if ( debug === 'nomatch' || debug === 'all' ) { debugger; }
|
2023-08-19 23:21:22 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-01-25 17:20:38 +00:00
|
|
|
|
if ( safe.logLevel > 1 ) {
|
2024-01-27 03:38:31 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Matched text\n${scriptText}`);
|
2024-01-25 17:20:38 +00:00
|
|
|
|
}
|
2024-03-20 13:31:17 +00:00
|
|
|
|
// eslint-disable-next-line no-debugger
|
|
|
|
|
|
if ( debug === 'match' || debug === 'all' ) { debugger; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Aborted');
|
2023-05-24 14:32:03 +00:00
|
|
|
|
throw new ReferenceError(exceptionToken);
|
|
|
|
|
|
};
|
2024-03-20 13:31:17 +00:00
|
|
|
|
// eslint-disable-next-line no-debugger
|
|
|
|
|
|
if ( debug === 'install' ) { debugger; }
|
2023-05-24 14:32:03 +00:00
|
|
|
|
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) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboErr(logPrefix, `Error: ${ex}`);
|
2023-05-24 14:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2023-05-25 12:51:26 +00:00
|
|
|
|
builtinScriptlets.push({
|
2023-10-17 21:33:49 +00:00
|
|
|
|
name: 'replace-node-text.fn',
|
|
|
|
|
|
fn: replaceNodeTextFn,
|
2023-05-25 12:51:26 +00:00
|
|
|
|
dependencies: [
|
2024-08-04 04:15:40 +00:00
|
|
|
|
'get-random-token.fn',
|
2023-05-25 12:51:26 +00:00
|
|
|
|
'run-at.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
2023-10-17 21:33:49 +00:00
|
|
|
|
function replaceNodeTextFn(
|
2023-05-25 12:51:26 +00:00
|
|
|
|
nodeName = '',
|
|
|
|
|
|
pattern = '',
|
|
|
|
|
|
replacement = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('replace-node-text.fn', ...Array.from(arguments));
|
2023-10-14 01:51:13 +00:00
|
|
|
|
const reNodeName = safe.patternToRegex(nodeName, 'i', true);
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const rePattern = safe.patternToRegex(pattern, 'gms');
|
|
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
2024-07-22 13:51:40 +00:00
|
|
|
|
const reIncludes = extraArgs.includes || extraArgs.condition
|
|
|
|
|
|
? safe.patternToRegex(extraArgs.includes || extraArgs.condition, 'ms')
|
|
|
|
|
|
: null;
|
|
|
|
|
|
const reExcludes = extraArgs.excludes
|
|
|
|
|
|
? safe.patternToRegex(extraArgs.excludes, 'ms')
|
|
|
|
|
|
: null;
|
2023-05-25 12:51:26 +00:00
|
|
|
|
const stop = (takeRecord = true) => {
|
|
|
|
|
|
if ( takeRecord ) {
|
|
|
|
|
|
handleMutations(observer.takeRecords());
|
|
|
|
|
|
}
|
|
|
|
|
|
observer.disconnect();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
if ( safe.logLevel > 1 ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, 'Quitting');
|
2023-05-25 12:51:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2024-08-04 00:09:21 +00:00
|
|
|
|
const textContentFactory = (( ) => {
|
|
|
|
|
|
const out = { createScript: s => s };
|
|
|
|
|
|
const { trustedTypes: tt } = self;
|
|
|
|
|
|
if ( tt instanceof Object ) {
|
|
|
|
|
|
if ( typeof tt.getPropertyType === 'function' ) {
|
|
|
|
|
|
if ( tt.getPropertyType('script', 'textContent') === 'TrustedScript' ) {
|
2025-03-22 20:01:43 +00:00
|
|
|
|
return tt.createPolicy(getRandomTokenFn(), out);
|
2024-08-04 00:09:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return out;
|
|
|
|
|
|
})();
|
2023-05-25 12:51:26 +00:00
|
|
|
|
let sedCount = extraArgs.sedCount || 0;
|
|
|
|
|
|
const handleNode = node => {
|
|
|
|
|
|
const before = node.textContent;
|
2024-07-22 13:51:40 +00:00
|
|
|
|
if ( reIncludes ) {
|
|
|
|
|
|
reIncludes.lastIndex = 0;
|
|
|
|
|
|
if ( safe.RegExp_test.call(reIncludes, before) === false ) { return true; }
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( reExcludes ) {
|
|
|
|
|
|
reExcludes.lastIndex = 0;
|
|
|
|
|
|
if ( safe.RegExp_test.call(reExcludes, before) ) { return true; }
|
|
|
|
|
|
}
|
2023-11-06 01:34:11 +00:00
|
|
|
|
rePattern.lastIndex = 0;
|
|
|
|
|
|
if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; }
|
|
|
|
|
|
rePattern.lastIndex = 0;
|
2023-05-25 12:51:26 +00:00
|
|
|
|
const after = pattern !== ''
|
|
|
|
|
|
? before.replace(rePattern, replacement)
|
|
|
|
|
|
: replacement;
|
2024-08-04 00:09:21 +00:00
|
|
|
|
node.textContent = node.nodeName === 'SCRIPT'
|
|
|
|
|
|
? textContentFactory.createScript(after)
|
|
|
|
|
|
: after;
|
2024-01-25 18:30:41 +00:00
|
|
|
|
if ( safe.logLevel > 1 ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Text before:\n${before.trim()}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
safe.uboLog(logPrefix, `Text after:\n${after.trim()}`);
|
2023-05-25 12:51:26 +00: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; }
|
2024-06-18 14:01:27 +00:00
|
|
|
|
if ( node === document.currentScript ) { continue; }
|
2023-05-25 12:51:26 +00:00
|
|
|
|
if ( handleNode(node) ) { continue; }
|
|
|
|
|
|
stop(); break;
|
|
|
|
|
|
}
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `${count} nodes present before installing mutation observer`);
|
2023-05-25 12:51:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( extraArgs.stay ) { return; }
|
|
|
|
|
|
runAt(( ) => {
|
|
|
|
|
|
const quitAfter = extraArgs.quitAfter || 0;
|
|
|
|
|
|
if ( quitAfter !== 0 ) {
|
|
|
|
|
|
setTimeout(( ) => { stop(); }, quitAfter);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 'interactive');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-29 18:37:02 +00:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2023-10-07 15:44:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'replace-fetch-response.fn',
|
|
|
|
|
|
fn: replaceFetchResponseFn,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'match-object-properties.fn',
|
|
|
|
|
|
'parse-properties-to-match.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function replaceFetchResponseFn(
|
|
|
|
|
|
trusted = false,
|
|
|
|
|
|
pattern = '',
|
|
|
|
|
|
replacement = '',
|
|
|
|
|
|
propsToMatch = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( trusted !== true ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('replace-fetch-response', pattern, replacement, propsToMatch);
|
2023-10-07 15:44:18 +00:00
|
|
|
|
if ( pattern === '*' ) { pattern = '.*'; }
|
|
|
|
|
|
const rePattern = safe.patternToRegex(pattern);
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const propNeedles = parsePropertiesToMatchFn(propsToMatch, 'url');
|
2024-06-13 13:32:30 +00:00
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
|
|
|
|
|
|
const reIncludes = extraArgs.includes ? safe.patternToRegex(extraArgs.includes) : null;
|
2023-10-07 15:44:18 +00:00
|
|
|
|
self.fetch = new Proxy(self.fetch, {
|
|
|
|
|
|
apply: function(target, thisArg, args) {
|
|
|
|
|
|
const fetchPromise = Reflect.apply(target, thisArg, args);
|
|
|
|
|
|
if ( pattern === '' ) { return fetchPromise; }
|
|
|
|
|
|
if ( propNeedles.size !== 0 ) {
|
|
|
|
|
|
const objs = [ args[0] instanceof Object ? args[0] : { url: args[0] } ];
|
2023-10-16 23:36:16 +00:00
|
|
|
|
if ( objs[0] instanceof Request ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
try {
|
|
|
|
|
|
objs[0] = safe.Request_clone.call(objs[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(ex) {
|
|
|
|
|
|
safe.uboErr(logPrefix, ex);
|
|
|
|
|
|
}
|
2023-10-07 15:44:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
if ( args[1] instanceof Object ) {
|
|
|
|
|
|
objs.push(args[1]);
|
|
|
|
|
|
}
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const matched = matchObjectPropertiesFn(propNeedles, ...objs);
|
2025-03-20 16:06:58 +00:00
|
|
|
|
if ( matched === undefined ) { return fetchPromise; }
|
|
|
|
|
|
if ( safe.logLevel > 1 ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Matched "propsToMatch":\n\t${matched.join('\n\t')}`);
|
2023-10-07 15:44:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return fetchPromise.then(responseBefore => {
|
|
|
|
|
|
const response = responseBefore.clone();
|
|
|
|
|
|
return response.text().then(textBefore => {
|
2024-06-13 13:32:30 +00:00
|
|
|
|
if ( reIncludes && reIncludes.test(textBefore) === false ) {
|
|
|
|
|
|
return responseBefore;
|
|
|
|
|
|
}
|
2023-10-07 15:44:18 +00:00
|
|
|
|
const textAfter = textBefore.replace(rePattern, replacement);
|
2025-03-20 16:06:58 +00:00
|
|
|
|
if ( textAfter === textBefore ) { return responseBefore; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Replaced');
|
2023-10-07 15:44:18 +00:00
|
|
|
|
const responseAfter = new Response(textAfter, {
|
|
|
|
|
|
status: responseBefore.status,
|
|
|
|
|
|
statusText: responseBefore.statusText,
|
|
|
|
|
|
headers: responseBefore.headers,
|
|
|
|
|
|
});
|
|
|
|
|
|
Object.defineProperties(responseAfter, {
|
|
|
|
|
|
ok: { value: responseBefore.ok },
|
|
|
|
|
|
redirected: { value: responseBefore.redirected },
|
|
|
|
|
|
type: { value: responseBefore.type },
|
|
|
|
|
|
url: { value: responseBefore.url },
|
|
|
|
|
|
});
|
|
|
|
|
|
return responseAfter;
|
|
|
|
|
|
}).catch(reason => {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboErr(logPrefix, reason);
|
2023-10-07 15:44:18 +00:00
|
|
|
|
return responseBefore;
|
|
|
|
|
|
});
|
|
|
|
|
|
}).catch(reason => {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboErr(logPrefix, reason);
|
2023-10-07 15:44:18 +00:00
|
|
|
|
return fetchPromise;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
Ensure scriptlet logging information make it to destination
Avoid race conditions between isolated world-side broadcast channel
and main-side broadcast channel, so as to not lose logging
information if the isolated world-side is not yet ready to
receive through its broadcast channel.
Additionally, added new scriptlet: `trusted-replace-argument`.
[...]##+js(trusted-replace-argument, fn, argpos, argval [,condition, pattern])
Where:
- `fn` is the function we want to proxy through an `apply` handler.
This can also be a class, in which case the scriptlet will proxy
through `construct` handler. At the moment, `fn` must exist at the
time the scriptlet executes.
- `argpos` is the 0-based position of the argument we want to change
- `argval` is the value we want to have for the argument -- the value
is interpreted the same way the value for `set-constant` is
interpreted.
- `condition, pattern` is a vararg which tells the scriptlet to act
only if `pattern` is found in the argument to overwrite.
Example of usage:
alliptvlinks.com##+js(trusted-replace-argument, MutationObserver, 0, noopFunc)
2024-01-26 17:18:30 +00:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2024-10-03 17:31:52 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'prevent-xhr.fn',
|
|
|
|
|
|
fn: preventXhrFn,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'generate-content.fn',
|
|
|
|
|
|
'match-object-properties.fn',
|
|
|
|
|
|
'parse-properties-to-match.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function preventXhrFn(
|
|
|
|
|
|
trusted = false,
|
|
|
|
|
|
propsToMatch = '',
|
|
|
|
|
|
directive = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof propsToMatch !== 'string' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const scriptletName = trusted ? 'trusted-prevent-xhr' : 'prevent-xhr';
|
|
|
|
|
|
const logPrefix = safe.makeLogPrefix(scriptletName, propsToMatch, directive);
|
|
|
|
|
|
const xhrInstances = new WeakMap();
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const propNeedles = parsePropertiesToMatchFn(propsToMatch, 'url');
|
2024-10-03 17:31:52 +00:00
|
|
|
|
const warOrigin = scriptletGlobals.warOrigin;
|
|
|
|
|
|
const safeDispatchEvent = (xhr, type) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
xhr.dispatchEvent(new Event(type));
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
const XHRBefore = XMLHttpRequest.prototype;
|
|
|
|
|
|
self.XMLHttpRequest = class extends self.XMLHttpRequest {
|
2024-10-17 16:11:50 +00:00
|
|
|
|
open(method, url, ...args) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
xhrInstances.delete(this);
|
|
|
|
|
|
if ( warOrigin !== undefined && url.startsWith(warOrigin) ) {
|
2024-10-17 16:11:50 +00:00
|
|
|
|
return super.open(method, url, ...args);
|
2024-10-03 17:31:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
const haystack = { method, url };
|
|
|
|
|
|
if ( propsToMatch === '' && directive === '' ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Called: ${safe.JSON_stringify(haystack, null, 2)}`);
|
2024-10-17 16:11:50 +00:00
|
|
|
|
return super.open(method, url, ...args);
|
2024-10-03 17:31:52 +00:00
|
|
|
|
}
|
2025-03-22 20:01:43 +00:00
|
|
|
|
if ( matchObjectPropertiesFn(propNeedles, haystack) ) {
|
2024-10-03 17:31:52 +00:00
|
|
|
|
const xhrDetails = Object.assign(haystack, {
|
|
|
|
|
|
xhr: this,
|
2024-10-17 16:11:50 +00:00
|
|
|
|
defer: args.length === 0 || !!args[0],
|
2024-10-03 17:31:52 +00:00
|
|
|
|
directive,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'date': '',
|
|
|
|
|
|
'content-type': '',
|
|
|
|
|
|
'content-length': '',
|
|
|
|
|
|
},
|
2024-11-08 13:48:07 +00:00
|
|
|
|
url: haystack.url,
|
2024-10-03 17:31:52 +00:00
|
|
|
|
props: {
|
|
|
|
|
|
response: { value: '' },
|
|
|
|
|
|
responseText: { value: '' },
|
|
|
|
|
|
responseXML: { value: null },
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
xhrInstances.set(this, xhrDetails);
|
|
|
|
|
|
}
|
2024-10-17 16:11:50 +00:00
|
|
|
|
return super.open(method, url, ...args);
|
2024-10-03 17:31:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
send(...args) {
|
|
|
|
|
|
const xhrDetails = xhrInstances.get(this);
|
|
|
|
|
|
if ( xhrDetails === undefined ) {
|
|
|
|
|
|
return super.send(...args);
|
|
|
|
|
|
}
|
|
|
|
|
|
xhrDetails.headers['date'] = (new Date()).toUTCString();
|
|
|
|
|
|
let xhrText = '';
|
|
|
|
|
|
switch ( this.responseType ) {
|
|
|
|
|
|
case 'arraybuffer':
|
|
|
|
|
|
xhrDetails.props.response.value = new ArrayBuffer(0);
|
|
|
|
|
|
xhrDetails.headers['content-type'] = 'application/octet-stream';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'blob':
|
|
|
|
|
|
xhrDetails.props.response.value = new Blob([]);
|
|
|
|
|
|
xhrDetails.headers['content-type'] = 'application/octet-stream';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'document': {
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
|
|
const doc = parser.parseFromString('', 'text/html');
|
|
|
|
|
|
xhrDetails.props.response.value = doc;
|
|
|
|
|
|
xhrDetails.props.responseXML.value = doc;
|
|
|
|
|
|
xhrDetails.headers['content-type'] = 'text/html';
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 'json':
|
|
|
|
|
|
xhrDetails.props.response.value = {};
|
|
|
|
|
|
xhrDetails.props.responseText.value = '{}';
|
|
|
|
|
|
xhrDetails.headers['content-type'] = 'application/json';
|
|
|
|
|
|
break;
|
|
|
|
|
|
default: {
|
|
|
|
|
|
if ( directive === '' ) { break; }
|
|
|
|
|
|
xhrText = generateContentFn(trusted, xhrDetails.directive);
|
|
|
|
|
|
if ( xhrText instanceof Promise ) {
|
|
|
|
|
|
xhrText = xhrText.then(text => {
|
|
|
|
|
|
xhrDetails.props.response.value = text;
|
|
|
|
|
|
xhrDetails.props.responseText.value = text;
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
xhrDetails.props.response.value = xhrText;
|
|
|
|
|
|
xhrDetails.props.responseText.value = xhrText;
|
|
|
|
|
|
}
|
|
|
|
|
|
xhrDetails.headers['content-type'] = 'text/plain';
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( xhrDetails.defer === false ) {
|
|
|
|
|
|
xhrDetails.headers['content-length'] = `${xhrDetails.props.response.value}`.length;
|
|
|
|
|
|
Object.defineProperties(xhrDetails.xhr, {
|
|
|
|
|
|
readyState: { value: 4 },
|
2024-11-08 13:48:07 +00:00
|
|
|
|
responseURL: { value: xhrDetails.url },
|
2024-10-03 17:31:52 +00:00
|
|
|
|
status: { value: 200 },
|
|
|
|
|
|
statusText: { value: 'OK' },
|
|
|
|
|
|
});
|
|
|
|
|
|
Object.defineProperties(xhrDetails.xhr, xhrDetails.props);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
Promise.resolve(xhrText).then(( ) => xhrDetails).then(details => {
|
|
|
|
|
|
Object.defineProperties(details.xhr, {
|
|
|
|
|
|
readyState: { value: 1, configurable: true },
|
2024-11-08 13:48:07 +00:00
|
|
|
|
responseURL: { value: xhrDetails.url },
|
2024-10-03 17:31:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'readystatechange');
|
|
|
|
|
|
return details;
|
|
|
|
|
|
}).then(details => {
|
|
|
|
|
|
xhrDetails.headers['content-length'] = `${details.props.response.value}`.length;
|
|
|
|
|
|
Object.defineProperties(details.xhr, {
|
|
|
|
|
|
readyState: { value: 2, configurable: true },
|
|
|
|
|
|
status: { value: 200 },
|
|
|
|
|
|
statusText: { value: 'OK' },
|
|
|
|
|
|
});
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'readystatechange');
|
|
|
|
|
|
return details;
|
|
|
|
|
|
}).then(details => {
|
|
|
|
|
|
Object.defineProperties(details.xhr, {
|
|
|
|
|
|
readyState: { value: 3, configurable: true },
|
|
|
|
|
|
});
|
|
|
|
|
|
Object.defineProperties(details.xhr, details.props);
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'readystatechange');
|
|
|
|
|
|
return details;
|
|
|
|
|
|
}).then(details => {
|
|
|
|
|
|
Object.defineProperties(details.xhr, {
|
|
|
|
|
|
readyState: { value: 4 },
|
|
|
|
|
|
});
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'readystatechange');
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'load');
|
|
|
|
|
|
safeDispatchEvent(details.xhr, 'loadend');
|
|
|
|
|
|
safe.uboLog(logPrefix, `Prevented with response:\n${details.xhr.response}`);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
getResponseHeader(headerName) {
|
|
|
|
|
|
const xhrDetails = xhrInstances.get(this);
|
|
|
|
|
|
if ( xhrDetails === undefined || this.readyState < this.HEADERS_RECEIVED ) {
|
|
|
|
|
|
return super.getResponseHeader(headerName);
|
|
|
|
|
|
}
|
|
|
|
|
|
const value = xhrDetails.headers[headerName.toLowerCase()];
|
|
|
|
|
|
if ( value !== undefined && value !== '' ) { return value; }
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
getAllResponseHeaders() {
|
|
|
|
|
|
const xhrDetails = xhrInstances.get(this);
|
|
|
|
|
|
if ( xhrDetails === undefined || this.readyState < this.HEADERS_RECEIVED ) {
|
|
|
|
|
|
return super.getAllResponseHeaders();
|
|
|
|
|
|
}
|
|
|
|
|
|
const out = [];
|
|
|
|
|
|
for ( const [ name, value ] of Object.entries(xhrDetails.headers) ) {
|
|
|
|
|
|
if ( !value ) { continue; }
|
|
|
|
|
|
out.push(`${name}: ${value}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( out.length !== 0 ) { out.push(''); }
|
|
|
|
|
|
return out.join('\r\n');
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
self.XMLHttpRequest.prototype.open.toString = function() {
|
|
|
|
|
|
return XHRBefore.open.toString();
|
|
|
|
|
|
};
|
|
|
|
|
|
self.XMLHttpRequest.prototype.send.toString = function() {
|
|
|
|
|
|
return XHRBefore.send.toString();
|
|
|
|
|
|
};
|
|
|
|
|
|
self.XMLHttpRequest.prototype.getResponseHeader.toString = function() {
|
|
|
|
|
|
return XHRBefore.getResponseHeader.toString();
|
|
|
|
|
|
};
|
|
|
|
|
|
self.XMLHttpRequest.prototype.getAllResponseHeaders.toString = function() {
|
|
|
|
|
|
return XHRBefore.getAllResponseHeaders.toString();
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
Injectable scriptlets
|
|
|
|
|
|
|
|
|
|
|
|
These are meant to be used in the MAIN (webpage) execution world.
|
|
|
|
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'abort-current-script.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'acs.js',
|
|
|
|
|
|
'abort-current-inline-script.js',
|
|
|
|
|
|
'acis.js',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: abortCurrentScript,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-05-24 14:32:03 +00:00
|
|
|
|
'abort-current-script-core.fn',
|
|
|
|
|
|
'run-at-html-element.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2022-06-24 17:35:20 +00:00
|
|
|
|
// Issues to mind before changing anything:
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2154
|
2023-07-25 13:22:47 +00:00
|
|
|
|
function abortCurrentScript(...args) {
|
2023-10-15 15:08:15 +00:00
|
|
|
|
runAtHtmlElementFn(( ) => {
|
2023-07-25 13:22:47 +00:00
|
|
|
|
abortCurrentScriptCore(...args);
|
2023-05-24 14:32:03 +00:00
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'abort-on-property-read.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'aopr.js',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: abortOnPropertyRead,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
|
|
|
|
|
'get-exception-token.fn',
|
2024-01-25 17:20:38 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function abortOnPropertyRead(
|
|
|
|
|
|
chain = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
|
|
|
|
|
if ( chain === '' ) { return; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const logPrefix = safe.makeLogPrefix('abort-on-property-read', chain);
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const exceptionToken = getExceptionTokenFn();
|
2019-07-06 16:36:28 +00:00
|
|
|
|
const abort = function() {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Aborted');
|
2023-03-26 13:13:17 +00:00
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2019-07-06 16:36:28 +00: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 11:32:39 +00:00
|
|
|
|
chain = chain.slice(pos + 1);
|
2019-07-06 16:36:28 +00: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 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'abort-on-property-write.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'aopw.js',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: abortOnPropertyWrite,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
|
|
|
|
|
'get-exception-token.fn',
|
2024-01-25 17:20:38 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function abortOnPropertyWrite(
|
|
|
|
|
|
prop = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof prop !== 'string' ) { return; }
|
|
|
|
|
|
if ( prop === '' ) { return; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-02-15 14:47:15 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('abort-on-property-write', prop);
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const exceptionToken = getExceptionTokenFn();
|
2019-07-06 16:36:28 +00: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() {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Aborted');
|
2023-03-26 13:13:17 +00:00
|
|
|
|
throw new ReferenceError(exceptionToken);
|
2019-07-06 16:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'addEventListener-defuser.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'aeld.js',
|
|
|
|
|
|
'prevent-addEventListener.js',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: addEventListenerDefuser,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2024-08-16 15:49:16 +00:00
|
|
|
|
'proxy-apply.fn',
|
2023-04-27 16:52:17 +00:00
|
|
|
|
'run-at.fn',
|
2023-03-26 18:02:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-04-02 16:01:58 +00:00
|
|
|
|
'should-debug.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2021-05-26 11:22:17 +00:00
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
|
2023-03-24 18:05:18 +00: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 15:59:17 +00:00
|
|
|
|
type = '',
|
|
|
|
|
|
pattern = ''
|
2023-03-24 18:05:18 +00:00
|
|
|
|
) {
|
2023-03-26 18:02:21 +00:00
|
|
|
|
const safe = safeSelf();
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 2);
|
2024-02-14 13:23:16 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('prevent-addEventListener', type, pattern);
|
2023-10-24 01:15:00 +00:00
|
|
|
|
const reType = safe.patternToRegex(type, undefined, true);
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const rePattern = safe.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 15:59:17 +00:00
|
|
|
|
const debug = shouldDebug(extraArgs);
|
2024-01-11 16:41:37 +00:00
|
|
|
|
const targetSelector = extraArgs.elements || undefined;
|
2024-02-14 13:23:16 +00:00
|
|
|
|
const elementMatches = elem => {
|
2024-06-11 11:44:43 +00:00
|
|
|
|
if ( targetSelector === 'window' ) { return elem === window; }
|
|
|
|
|
|
if ( targetSelector === 'document' ) { return elem === document; }
|
2024-02-14 13:23:16 +00:00
|
|
|
|
if ( elem && elem.matches && elem.matches(targetSelector) ) { return true; }
|
|
|
|
|
|
const elems = Array.from(document.querySelectorAll(targetSelector));
|
|
|
|
|
|
return elems.includes(elem);
|
|
|
|
|
|
};
|
|
|
|
|
|
const elementDetails = elem => {
|
|
|
|
|
|
if ( elem instanceof Window ) { return 'window'; }
|
|
|
|
|
|
if ( elem instanceof Document ) { return 'document'; }
|
|
|
|
|
|
if ( elem instanceof Element === false ) { return '?'; }
|
|
|
|
|
|
const parts = [];
|
2024-06-25 13:08:46 +00:00
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/discussions/17907#discussioncomment-9871079
|
|
|
|
|
|
const id = String(elem.id);
|
|
|
|
|
|
if ( id !== '' ) { parts.push(`#${CSS.escape(id)}`); }
|
2024-02-14 13:23:16 +00:00
|
|
|
|
for ( let i = 0; i < elem.classList.length; i++ ) {
|
|
|
|
|
|
parts.push(`.${CSS.escape(elem.classList.item(i))}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
for ( let i = 0; i < elem.attributes.length; i++ ) {
|
|
|
|
|
|
const attr = elem.attributes.item(i);
|
|
|
|
|
|
if ( attr.name === 'id' ) { continue; }
|
|
|
|
|
|
if ( attr.name === 'class' ) { continue; }
|
|
|
|
|
|
parts.push(`[${CSS.escape(attr.name)}="${attr.value}"]`);
|
2024-01-11 16:41:37 +00:00
|
|
|
|
}
|
2024-02-14 13:23:16 +00:00
|
|
|
|
return parts.join('');
|
|
|
|
|
|
};
|
|
|
|
|
|
const shouldPrevent = (thisArg, type, handler) => {
|
2024-01-10 17:46:23 +00:00
|
|
|
|
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 ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
|
2024-03-20 13:31:17 +00:00
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
2024-01-10 17:46:23 +00:00
|
|
|
|
}
|
2024-02-14 13:23:16 +00:00
|
|
|
|
if ( matchesBoth && targetSelector !== undefined ) {
|
|
|
|
|
|
if ( elementMatches(thisArg) === false ) { return false; }
|
|
|
|
|
|
}
|
2024-01-10 17:46:23 +00:00
|
|
|
|
return matchesBoth;
|
|
|
|
|
|
};
|
2025-02-24 17:47:54 +00:00
|
|
|
|
const proxyFn = function(context) {
|
|
|
|
|
|
const { callArgs, thisArg } = context;
|
|
|
|
|
|
let t, h;
|
|
|
|
|
|
try {
|
|
|
|
|
|
t = String(callArgs[0]);
|
|
|
|
|
|
if ( typeof callArgs[1] === 'function' ) {
|
|
|
|
|
|
h = String(safe.Function_toString(callArgs[1]));
|
|
|
|
|
|
} else if ( typeof callArgs[1] === 'object' && callArgs[1] !== null ) {
|
|
|
|
|
|
if ( typeof callArgs[1].handleEvent === 'function' ) {
|
|
|
|
|
|
h = String(safe.Function_toString(callArgs[1].handleEvent));
|
2023-05-28 18:56:31 +00:00
|
|
|
|
}
|
2025-02-24 17:47:54 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
h = String(callArgs[1]);
|
2024-08-16 15:49:16 +00:00
|
|
|
|
}
|
2025-02-24 17:47:54 +00:00
|
|
|
|
} catch {
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( type === '' && pattern === '' ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Called: ${t}\n${h}\n${elementDetails(thisArg)}`);
|
|
|
|
|
|
} else if ( shouldPrevent(thisArg, t, h) ) {
|
|
|
|
|
|
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
return context.reflect();
|
|
|
|
|
|
};
|
|
|
|
|
|
runAt(( ) => {
|
|
|
|
|
|
proxyApplyFn('EventTarget.prototype.addEventListener', proxyFn);
|
|
|
|
|
|
proxyApplyFn('document.addEventListener', proxyFn);
|
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 15:59:17 +00:00
|
|
|
|
}, extraArgs.runAt);
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2023-09-28 14:07:03 +00:00
|
|
|
|
name: 'adjust-setInterval.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
2023-09-28 14:07:03 +00:00
|
|
|
|
'nano-setInterval-booster.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
'nano-sib.js',
|
|
|
|
|
|
],
|
2023-09-28 14:07:03 +00:00
|
|
|
|
fn: adjustSetInterval,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-08-08 11:41:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2019-07-10 12:11:51 +00: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 14:45:44 +00:00
|
|
|
|
// Use `*` to match any delay.
|
2019-07-10 12:11:51 +00: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-09-28 14:07:03 +00:00
|
|
|
|
function adjustSetInterval(
|
2023-03-24 18:05:18 +00:00
|
|
|
|
needleArg = '',
|
|
|
|
|
|
delayArg = '',
|
|
|
|
|
|
boostArg = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof needleArg !== 'string' ) { return; }
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const reNeedle = safe.patternToRegex(needleArg);
|
2021-01-23 14:45:44 +00: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)
|
2023-09-14 15:13:58 +00:00
|
|
|
|
? Math.min(Math.max(boost, 0.001), 50)
|
2021-01-23 14:45:44 +00:00
|
|
|
|
: 0.05;
|
|
|
|
|
|
self.setInterval = new Proxy(self.setInterval, {
|
2019-07-06 16:36:28 +00:00
|
|
|
|
apply: function(target, thisArg, args) {
|
2021-01-23 14:45:44 +00:00
|
|
|
|
const [ a, b ] = args;
|
|
|
|
|
|
if (
|
|
|
|
|
|
(delay === -1 || b === delay) &&
|
|
|
|
|
|
reNeedle.test(a.toString())
|
|
|
|
|
|
) {
|
2019-07-06 16:36:28 +00:00
|
|
|
|
args[1] = b * boost;
|
|
|
|
|
|
}
|
|
|
|
|
|
return target.apply(thisArg, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2023-09-28 14:07:03 +00:00
|
|
|
|
name: 'adjust-setTimeout.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
2023-09-28 14:07:03 +00:00
|
|
|
|
'nano-setTimeout-booster.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
'nano-stb.js',
|
|
|
|
|
|
],
|
2023-09-28 14:07:03 +00:00
|
|
|
|
fn: adjustSetTimeout,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-08-08 11:41:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2019-07-10 12:11:51 +00: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 14:45:44 +00:00
|
|
|
|
// Use `*` to match any delay.
|
2019-07-10 12:11:51 +00: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-09-28 14:07:03 +00:00
|
|
|
|
function adjustSetTimeout(
|
2023-03-24 18:05:18 +00:00
|
|
|
|
needleArg = '',
|
|
|
|
|
|
delayArg = '',
|
|
|
|
|
|
boostArg = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof needleArg !== 'string' ) { return; }
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const reNeedle = safe.patternToRegex(needleArg);
|
2021-01-23 14:45:44 +00: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)
|
2023-09-14 15:13:58 +00:00
|
|
|
|
? Math.min(Math.max(boost, 0.001), 50)
|
2021-01-23 14:45:44 +00:00
|
|
|
|
: 0.05;
|
|
|
|
|
|
self.setTimeout = new Proxy(self.setTimeout, {
|
2019-07-06 16:36:28 +00:00
|
|
|
|
apply: function(target, thisArg, args) {
|
2021-01-23 14:45:44 +00:00
|
|
|
|
const [ a, b ] = args;
|
|
|
|
|
|
if (
|
|
|
|
|
|
(delay === -1 || b === delay) &&
|
|
|
|
|
|
reNeedle.test(a.toString())
|
|
|
|
|
|
) {
|
2019-07-06 16:36:28 +00:00
|
|
|
|
args[1] = b * boost;
|
|
|
|
|
|
}
|
|
|
|
|
|
return target.apply(thisArg, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2024-01-25 17:20:38 +00:00
|
|
|
|
name: 'prevent-fetch.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
2024-01-25 17:20:38 +00:00
|
|
|
|
'no-fetch-if.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: noFetchIf,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-11-25 16:13:57 +00:00
|
|
|
|
'generate-content.fn',
|
2024-09-05 15:17:36 +00:00
|
|
|
|
'proxy-apply.fn',
|
2023-08-08 11:41:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function noFetchIf(
|
2023-11-25 16:13:57 +00:00
|
|
|
|
propsToMatch = '',
|
2024-07-13 15:02:54 +00:00
|
|
|
|
responseBody = '',
|
|
|
|
|
|
responseType = ''
|
2023-03-24 18:05:18 +00:00
|
|
|
|
) {
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-07-13 15:02:54 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('prevent-fetch', propsToMatch, responseBody, responseType);
|
2020-12-11 13:29:23 +00:00
|
|
|
|
const needles = [];
|
2024-12-06 16:53:42 +00:00
|
|
|
|
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
|
2020-12-11 14:28:29 +00:00
|
|
|
|
if ( condition === '' ) { continue; }
|
2020-12-11 13:29:23 +00: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;
|
|
|
|
|
|
}
|
2024-09-04 15:32:26 +00:00
|
|
|
|
needles.push({ key, pattern: safe.initPattern(value, { canNegate: true }) });
|
2020-12-11 13:29:23 +00:00
|
|
|
|
}
|
2024-07-13 15:02:54 +00:00
|
|
|
|
const validResponseProps = {
|
|
|
|
|
|
ok: [ false, true ],
|
2024-07-14 16:15:53 +00:00
|
|
|
|
statusText: [ '', 'Not Found' ],
|
|
|
|
|
|
type: [ 'basic', 'cors', 'default', 'error', 'opaque' ],
|
|
|
|
|
|
};
|
|
|
|
|
|
const responseProps = {
|
|
|
|
|
|
statusText: { value: 'OK' },
|
2024-07-13 15:02:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
if ( /^\{.*\}$/.test(responseType) ) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => {
|
|
|
|
|
|
if ( validResponseProps[p] === undefined ) { return; }
|
|
|
|
|
|
if ( validResponseProps[p].includes(v) === false ) { return; }
|
|
|
|
|
|
responseProps[p] = { value: v };
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-01-09 14:33:22 +00:00
|
|
|
|
catch { }
|
2024-07-13 15:02:54 +00:00
|
|
|
|
} else if ( responseType !== '' ) {
|
|
|
|
|
|
if ( validResponseProps.type.includes(responseType) ) {
|
2024-07-14 16:15:53 +00:00
|
|
|
|
responseProps.type = { value: responseType };
|
2024-07-13 15:02:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-17 13:09:19 +00:00
|
|
|
|
proxyApplyFn('fetch', function fetch(context) {
|
|
|
|
|
|
const { callArgs } = context;
|
|
|
|
|
|
const details = callArgs[0] instanceof self.Request
|
|
|
|
|
|
? callArgs[0]
|
|
|
|
|
|
: Object.assign({ url: callArgs[0] }, callArgs[1]);
|
2024-09-05 15:17:36 +00:00
|
|
|
|
let proceed = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const props = new Map();
|
|
|
|
|
|
for ( const prop in details ) {
|
|
|
|
|
|
let v = details[prop];
|
|
|
|
|
|
if ( typeof v !== 'string' ) {
|
|
|
|
|
|
try { v = safe.JSON_stringify(v); }
|
2025-01-09 14:33:22 +00:00
|
|
|
|
catch { }
|
2020-12-11 13:29:23 +00:00
|
|
|
|
}
|
2024-09-05 15:17:36 +00:00
|
|
|
|
if ( typeof v !== 'string' ) { continue; }
|
|
|
|
|
|
props.set(prop, v);
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( safe.logLevel > 1 || propsToMatch === '' && responseBody === '' ) {
|
|
|
|
|
|
const out = Array.from(props).map(a => `${a[0]}:${a[1]}`);
|
|
|
|
|
|
safe.uboLog(logPrefix, `Called: ${out.join('\n')}`);
|
2020-12-11 13:29:23 +00:00
|
|
|
|
}
|
2024-09-05 15:17:36 +00:00
|
|
|
|
if ( propsToMatch === '' && responseBody === '' ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
return context.reflect();
|
2023-11-25 16:13:57 +00:00
|
|
|
|
}
|
2024-09-05 15:17:36 +00:00
|
|
|
|
proceed = needles.length === 0;
|
|
|
|
|
|
for ( const { key, pattern } of needles ) {
|
|
|
|
|
|
if (
|
|
|
|
|
|
pattern.expect && props.has(key) === false ||
|
|
|
|
|
|
safe.testPattern(pattern, props.get(key)) === false
|
|
|
|
|
|
) {
|
|
|
|
|
|
proceed = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2020-12-11 13:29:23 +00:00
|
|
|
|
}
|
2024-09-05 15:17:36 +00:00
|
|
|
|
if ( proceed ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
return context.reflect();
|
2024-09-05 15:17:36 +00:00
|
|
|
|
}
|
2024-10-03 17:31:52 +00:00
|
|
|
|
return Promise.resolve(generateContentFn(false, responseBody)).then(text => {
|
2024-09-05 15:17:36 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Prevented with response "${text}"`);
|
|
|
|
|
|
const response = new Response(text, {
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Length': text.length,
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
const props = Object.assign(
|
|
|
|
|
|
{ url: { value: details.url } },
|
|
|
|
|
|
responseProps
|
|
|
|
|
|
);
|
|
|
|
|
|
safe.Object_defineProperties(response, props);
|
|
|
|
|
|
return response;
|
|
|
|
|
|
});
|
2020-12-11 13:29:23 +00:00
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2021-04-11 11:11:09 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2021-04-11 11:11:09 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2023-09-28 14:07:03 +00:00
|
|
|
|
name: 'prevent-refresh.js',
|
|
|
|
|
|
aliases: [
|
|
|
|
|
|
'refresh-defuser.js',
|
|
|
|
|
|
],
|
|
|
|
|
|
fn: preventRefresh,
|
2023-06-15 23:57:10 +00:00
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
|
dependencies: [
|
2024-01-25 17:20:38 +00:00
|
|
|
|
'safe-self.fn',
|
2023-06-15 23:57:10 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2021-10-03 13:46:24 +00:00
|
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/q0frv0/while_reading_a_sports_article_i_was_redirected/hf7wo9v/
|
2023-09-28 14:07:03 +00:00
|
|
|
|
function preventRefresh(
|
2024-11-01 15:33:34 +00:00
|
|
|
|
delay = ''
|
2023-03-24 18:05:18 +00:00
|
|
|
|
) {
|
2024-11-01 15:33:34 +00:00
|
|
|
|
if ( typeof delay !== 'string' ) { return; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-11-01 15:33:34 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('prevent-refresh', delay);
|
|
|
|
|
|
const stop = content => {
|
|
|
|
|
|
window.stop();
|
|
|
|
|
|
safe.uboLog(logPrefix, `Prevented "${content}"`);
|
|
|
|
|
|
};
|
2021-10-03 13:46:24 +00:00
|
|
|
|
const defuse = ( ) => {
|
|
|
|
|
|
const meta = document.querySelector('meta[http-equiv="refresh" i][content]');
|
|
|
|
|
|
if ( meta === null ) { return; }
|
2024-11-01 15:33:34 +00:00
|
|
|
|
const content = meta.getAttribute('content') || '';
|
|
|
|
|
|
const ms = delay === ''
|
|
|
|
|
|
? Math.max(parseFloat(content) || 0, 0) * 500
|
|
|
|
|
|
: 0;
|
|
|
|
|
|
if ( ms === 0 ) {
|
|
|
|
|
|
stop(content);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setTimeout(( ) => { stop(content); }, ms);
|
|
|
|
|
|
}
|
2021-10-03 13:46:24 +00:00
|
|
|
|
};
|
2024-11-01 15:33:34 +00:00
|
|
|
|
self.addEventListener('load', defuse, { capture: true, once: true });
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2021-10-03 13:46:24 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2021-10-03 13:46:24 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'remove-class.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'rc.js',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
fn: removeClass,
|
2023-07-24 11:33:33 +00:00
|
|
|
|
world: 'ISOLATED',
|
2023-06-15 23:57:10 +00:00
|
|
|
|
dependencies: [
|
|
|
|
|
|
'run-at.fn',
|
2024-04-17 13:17:49 +00:00
|
|
|
|
'safe-self.fn',
|
2023-06-15 23:57:10 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function removeClass(
|
2024-04-17 13:17:49 +00:00
|
|
|
|
rawToken = '',
|
|
|
|
|
|
rawSelector = '',
|
2023-03-24 18:05:18 +00:00
|
|
|
|
behavior = ''
|
|
|
|
|
|
) {
|
2024-04-17 13:17:49 +00:00
|
|
|
|
if ( typeof rawToken !== 'string' ) { return; }
|
|
|
|
|
|
if ( rawToken === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const logPrefix = safe.makeLogPrefix('remove-class', rawToken, rawSelector, behavior);
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const tokens = safe.String_split.call(rawToken, /\s*\|\s*/);
|
2024-04-17 13:17:49 +00:00
|
|
|
|
const selector = tokens
|
|
|
|
|
|
.map(a => `${rawSelector}.${CSS.escape(a)}`)
|
|
|
|
|
|
.join(',');
|
|
|
|
|
|
if ( safe.logLevel > 1 ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Target selector:\n\t${selector}`);
|
2020-03-18 13:44:18 +00:00
|
|
|
|
}
|
2023-07-24 11:33:33 +00:00
|
|
|
|
const mustStay = /\bstay\b/.test(behavior);
|
2021-06-06 12:58:40 +00:00
|
|
|
|
let timer;
|
2024-04-17 13:17:49 +00:00
|
|
|
|
const rmclass = ( ) => {
|
2021-06-06 12:58:40 +00:00
|
|
|
|
timer = undefined;
|
2020-03-18 13:44:18 +00:00
|
|
|
|
try {
|
|
|
|
|
|
const nodes = document.querySelectorAll(selector);
|
|
|
|
|
|
for ( const node of nodes ) {
|
2024-04-17 13:17:49 +00:00
|
|
|
|
node.classList.remove(...tokens);
|
|
|
|
|
|
safe.uboLog(logPrefix, 'Removed class(es)');
|
2020-03-18 13:44:18 +00:00
|
|
|
|
}
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2020-03-18 13:44:18 +00:00
|
|
|
|
}
|
2023-07-24 11:33:33 +00:00
|
|
|
|
if ( mustStay ) { return; }
|
|
|
|
|
|
if ( document.readyState !== 'complete' ) { return; }
|
|
|
|
|
|
observer.disconnect();
|
2020-03-18 13:44:18 +00:00
|
|
|
|
};
|
2021-06-06 12:58:40 +00: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; }
|
2024-05-16 13:28:09 +00:00
|
|
|
|
timer = safe.onIdle(rmclass, { timeout: 67 });
|
2021-06-06 12:58:40 +00:00
|
|
|
|
};
|
2023-07-24 11:33:33 +00:00
|
|
|
|
const observer = new MutationObserver(mutationHandler);
|
2021-06-06 12:58:40 +00:00
|
|
|
|
const start = ( ) => {
|
2020-03-18 13:44:18 +00:00
|
|
|
|
rmclass();
|
2021-10-14 13:08:08 +00:00
|
|
|
|
observer.observe(document, {
|
2021-06-06 12:58:40 +00:00
|
|
|
|
attributes: true,
|
|
|
|
|
|
attributeFilter: [ 'class' ],
|
|
|
|
|
|
childList: true,
|
|
|
|
|
|
subtree: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2023-06-15 23:57:10 +00:00
|
|
|
|
runAt(( ) => {
|
2021-06-06 12:58:40 +00:00
|
|
|
|
start();
|
2023-07-24 11:33:33 +00:00
|
|
|
|
}, /\bcomplete\b/.test(behavior) ? 'idle' : 'loading');
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2020-03-18 13:44:18 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'webrtc-if.js',
|
|
|
|
|
|
fn: webrtcIf,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-08-08 11:41:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function webrtcIf(
|
|
|
|
|
|
good = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof good !== 'string' ) { return; }
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const reGood = safe.patternToRegex(good);
|
2019-07-06 16:36:28 +00: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 15:19:26 +00:00
|
|
|
|
log('uBO:', args[1]);
|
2019-07-11 13:45:53 +00:00
|
|
|
|
return Reflect.apply(target, thisArg, args.slice(0, 1));
|
2019-07-06 16:36:28 +00:00
|
|
|
|
}
|
2019-07-11 13:45:53 +00:00
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
2019-07-06 16:36:28 +00:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
window[rtcName] =
|
|
|
|
|
|
new Proxy(peerConnectionCtor, {
|
|
|
|
|
|
construct: function(target, args) {
|
|
|
|
|
|
if ( isGoodConfig(target, args[0]) === false ) {
|
2020-04-28 15:19:26 +00:00
|
|
|
|
log('uBO:', args[0]);
|
2019-07-11 13:45:53 +00:00
|
|
|
|
return Reflect.construct(target);
|
2019-07-06 16:36:28 +00:00
|
|
|
|
}
|
2019-07-11 13:45:53 +00:00
|
|
|
|
return Reflect.construct(target, args);
|
2019-07-06 16:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2024-10-03 17:31:52 +00:00
|
|
|
|
name: 'prevent-xhr.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
2024-10-03 17:31:52 +00:00
|
|
|
|
'no-xhr-if.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
],
|
2024-10-03 17:31:52 +00:00
|
|
|
|
fn: preventXhr,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2024-10-03 17:31:52 +00:00
|
|
|
|
'prevent-xhr.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2024-10-03 17:31:52 +00:00
|
|
|
|
function preventXhr(...args) {
|
|
|
|
|
|
return preventXhrFn(false, ...args);
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2021-09-11 13:15:39 +00:00
|
|
|
|
|
2024-10-08 23:47:35 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @scriptlet prevent-window-open
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Prevent a webpage from opening new tabs through `window.open()`.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param pattern
|
|
|
|
|
|
* A plain string or regex to match against the `url` argument for the
|
|
|
|
|
|
* prevention to be triggered. If not provided, all calls to `window.open()`
|
|
|
|
|
|
* are prevented.
|
|
|
|
|
|
* If set to the special value `debug` *and* the logger is opened, the scriptlet
|
|
|
|
|
|
* will trigger a `debugger` statement and the prevention will not occur.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param [delay]
|
|
|
|
|
|
* If provided, a decoy will be created or opened, and this parameter states
|
|
|
|
|
|
* the number of seconds to wait for before the decoy is terminated, i.e.
|
|
|
|
|
|
* either removed from the DOM or closed.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param [decoy]
|
|
|
|
|
|
* A string representing the type of decoy to use:
|
|
|
|
|
|
* - `blank`: replace the `url` parameter with `about:blank`
|
|
|
|
|
|
* - `object`: create and append an `object` element to the DOM, and return
|
|
|
|
|
|
* its `contentWindow` property.
|
|
|
|
|
|
* - `frame`: create and append an `iframe` element to the DOM, and return
|
|
|
|
|
|
* its `contentWindow` property.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ##+js(prevent-window-open, ads.example.com/)
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ##+js(prevent-window-open, ads.example.com/, 1, iframe)
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
2021-09-11 13:15:39 +00:00
|
|
|
|
|
2023-06-17 15:53:08 +00:00
|
|
|
|
builtinScriptlets.push({
|
2024-10-08 23:47:35 +00:00
|
|
|
|
name: 'prevent-window-open.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'nowoif.js',
|
2024-10-08 23:47:35 +00:00
|
|
|
|
'no-window-open-if.js',
|
2023-07-18 13:39:06 +00:00
|
|
|
|
'window.open-defuser.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
],
|
2023-06-17 15:53:08 +00:00
|
|
|
|
fn: noWindowOpenIf,
|
|
|
|
|
|
dependencies: [
|
2024-08-16 15:49:16 +00:00
|
|
|
|
'proxy-apply.fn',
|
2023-06-17 15:53:08 +00:00
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function noWindowOpenIf(
|
|
|
|
|
|
pattern = '',
|
|
|
|
|
|
delay = '',
|
|
|
|
|
|
decoy = ''
|
|
|
|
|
|
) {
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('no-window-open-if', pattern, delay, decoy);
|
2023-06-17 15:53:08 +00:00
|
|
|
|
const targetMatchResult = pattern.startsWith('!') === false;
|
|
|
|
|
|
if ( targetMatchResult === false ) {
|
|
|
|
|
|
pattern = pattern.slice(1);
|
|
|
|
|
|
}
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const rePattern = safe.patternToRegex(pattern);
|
2024-08-31 16:36:20 +00:00
|
|
|
|
const autoRemoveAfter = (parseFloat(delay) || 0) * 1000;
|
2024-08-30 14:25:39 +00:00
|
|
|
|
const setTimeout = self.setTimeout;
|
2023-06-17 15:53:08 +00:00
|
|
|
|
const createDecoy = function(tag, urlProp, url) {
|
|
|
|
|
|
const decoyElem = document.createElement(tag);
|
|
|
|
|
|
decoyElem[urlProp] = url;
|
|
|
|
|
|
decoyElem.style.setProperty('height','1px', 'important');
|
|
|
|
|
|
decoyElem.style.setProperty('position','fixed', 'important');
|
|
|
|
|
|
decoyElem.style.setProperty('top','-1px', 'important');
|
|
|
|
|
|
decoyElem.style.setProperty('width','1px', 'important');
|
|
|
|
|
|
document.body.appendChild(decoyElem);
|
2024-08-31 16:36:20 +00:00
|
|
|
|
setTimeout(( ) => { decoyElem.remove(); }, autoRemoveAfter);
|
2023-06-17 15:53:08 +00:00
|
|
|
|
return decoyElem;
|
|
|
|
|
|
};
|
2024-08-31 16:36:20 +00:00
|
|
|
|
const noopFunc = function(){};
|
2024-09-17 13:09:19 +00:00
|
|
|
|
proxyApplyFn('open', function open(context) {
|
2024-10-08 23:47:35 +00:00
|
|
|
|
if ( pattern === 'debug' && safe.logLevel !== 0 ) {
|
|
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
|
|
|
|
|
return context.reflect();
|
|
|
|
|
|
}
|
2024-09-17 13:09:19 +00:00
|
|
|
|
const { callArgs } = context;
|
|
|
|
|
|
const haystack = callArgs.join(' ');
|
2024-08-16 15:49:16 +00:00
|
|
|
|
if ( rePattern.test(haystack) !== targetMatchResult ) {
|
|
|
|
|
|
if ( safe.logLevel > 1 ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Allowed (${callArgs.join(', ')})`);
|
2023-06-17 15:53:08 +00:00
|
|
|
|
}
|
2024-09-17 13:09:19 +00:00
|
|
|
|
return context.reflect();
|
2024-08-16 15:49:16 +00:00
|
|
|
|
}
|
2024-09-17 13:09:19 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Prevented (${callArgs.join(', ')})`);
|
2024-08-30 14:25:39 +00:00
|
|
|
|
if ( delay === '' ) { return null; }
|
|
|
|
|
|
if ( decoy === 'blank' ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
callArgs[0] = 'about:blank';
|
|
|
|
|
|
const r = context.reflect();
|
2024-08-30 14:25:39 +00:00
|
|
|
|
setTimeout(( ) => { r.close(); }, autoRemoveAfter);
|
|
|
|
|
|
return r;
|
|
|
|
|
|
}
|
2024-08-16 15:49:16 +00:00
|
|
|
|
const decoyElem = decoy === 'obj'
|
2024-09-17 13:09:19 +00:00
|
|
|
|
? createDecoy('object', 'data', ...callArgs)
|
|
|
|
|
|
: createDecoy('iframe', 'src', ...callArgs);
|
2024-08-16 15:49:16 +00:00
|
|
|
|
let popup = decoyElem.contentWindow;
|
|
|
|
|
|
if ( typeof popup === 'object' && popup !== null ) {
|
|
|
|
|
|
Object.defineProperty(popup, 'closed', { value: false });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
popup = new Proxy(self, {
|
2024-08-31 16:36:20 +00:00
|
|
|
|
get: function(target, prop, ...args) {
|
2024-08-16 15:49:16 +00:00
|
|
|
|
if ( prop === 'closed' ) { return false; }
|
2024-08-31 16:36:20 +00:00
|
|
|
|
const r = Reflect.get(target, prop, ...args);
|
2024-08-16 15:49:16 +00:00
|
|
|
|
if ( typeof r === 'function' ) { return noopFunc; }
|
2024-08-31 16:36:20 +00:00
|
|
|
|
return r;
|
2024-08-16 15:49:16 +00:00
|
|
|
|
},
|
2024-08-31 16:36:20 +00:00
|
|
|
|
set: function(...args) {
|
|
|
|
|
|
return Reflect.set(...args);
|
2024-08-16 15:49:16 +00:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( safe.logLevel !== 0 ) {
|
|
|
|
|
|
popup = new Proxy(popup, {
|
2024-08-31 16:36:20 +00:00
|
|
|
|
get: function(target, prop, ...args) {
|
|
|
|
|
|
const r = Reflect.get(target, prop, ...args);
|
|
|
|
|
|
safe.uboLog(logPrefix, `popup / get ${prop} === ${r}`);
|
|
|
|
|
|
if ( typeof r === 'function' ) {
|
|
|
|
|
|
return (...args) => { return r.call(target, ...args); };
|
|
|
|
|
|
}
|
|
|
|
|
|
return r;
|
2024-08-16 15:49:16 +00:00
|
|
|
|
},
|
2024-08-31 16:36:20 +00:00
|
|
|
|
set: function(target, prop, value, ...args) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `popup / set ${prop} = ${value}`);
|
|
|
|
|
|
return Reflect.set(target, prop, value, ...args);
|
2024-08-16 15:49:16 +00:00
|
|
|
|
},
|
|
|
|
|
|
});
|
2023-06-17 15:53:08 +00:00
|
|
|
|
}
|
2024-08-16 15:49:16 +00:00
|
|
|
|
return popup;
|
2023-06-17 15:53:08 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
2023-09-28 14:07:03 +00:00
|
|
|
|
name: 'close-window.js',
|
|
|
|
|
|
aliases: [
|
|
|
|
|
|
'window-close-if.js',
|
|
|
|
|
|
],
|
|
|
|
|
|
fn: closeWindow,
|
|
|
|
|
|
world: 'ISOLATED',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-08-08 11:41:21 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2021-12-13 13:14:30 +00:00
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/10323#issuecomment-992312847
|
|
|
|
|
|
// https://github.com/AdguardTeam/Scriptlets/issues/158
|
2022-09-17 16:46:42 +00:00
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/discussions/2270
|
2023-09-28 14:07:03 +00:00
|
|
|
|
function closeWindow(
|
2023-03-24 18:05:18 +00:00
|
|
|
|
arg1 = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof arg1 !== 'string' ) { return; }
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const safe = safeSelf();
|
2022-09-17 16:46:42 +00:00
|
|
|
|
let subject = '';
|
2023-03-26 13:13:17 +00:00
|
|
|
|
if ( /^\/.*\/$/.test(arg1) ) {
|
2022-09-17 16:46:42 +00:00
|
|
|
|
subject = window.location.href;
|
2023-03-26 13:13:17 +00:00
|
|
|
|
} else if ( arg1 !== '' ) {
|
2022-09-17 16:46:42 +00:00
|
|
|
|
subject = `${window.location.pathname}${window.location.search}`;
|
2021-12-13 13:14:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
try {
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const re = safe.patternToRegex(arg1);
|
2022-09-17 16:46:42 +00:00
|
|
|
|
if ( re.test(subject) ) {
|
2021-12-13 13:14:30 +00:00
|
|
|
|
window.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch(ex) {
|
|
|
|
|
|
console.log(ex);
|
|
|
|
|
|
}
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2021-12-13 13:14:30 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2021-12-13 13:14:30 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'window.name-defuser.js',
|
|
|
|
|
|
fn: windowNameDefuser,
|
|
|
|
|
|
});
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1228
|
|
|
|
|
|
function windowNameDefuser() {
|
2019-07-06 16:36:28 +00:00
|
|
|
|
if ( window === window.top ) {
|
|
|
|
|
|
window.name = '';
|
|
|
|
|
|
}
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'overlay-buster.js',
|
|
|
|
|
|
fn: overlayBuster,
|
|
|
|
|
|
});
|
2019-07-06 16:36:28 +00: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.
|
2025-03-06 14:50:37 +00:00
|
|
|
|
function overlayBuster(allFrames) {
|
2025-03-06 19:15:02 +00:00
|
|
|
|
if ( allFrames === '' && window !== window.top ) { return; }
|
2019-07-06 16:36:28 +00: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 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'alert-buster.js',
|
|
|
|
|
|
fn: alertBuster,
|
|
|
|
|
|
});
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/8
|
|
|
|
|
|
function alertBuster() {
|
2022-05-08 15:22:32 +00:00
|
|
|
|
window.alert = new Proxy(window.alert, {
|
|
|
|
|
|
apply: function(a) {
|
|
|
|
|
|
console.info(a);
|
|
|
|
|
|
},
|
2024-09-18 14:34:18 +00:00
|
|
|
|
get(target, prop) {
|
2023-05-20 21:18:44 +00:00
|
|
|
|
if ( prop === 'toString' ) {
|
|
|
|
|
|
return target.toString.bind(target);
|
|
|
|
|
|
}
|
2024-09-18 14:34:18 +00:00
|
|
|
|
return Reflect.get(target, prop);
|
2023-05-20 21:18:44 +00:00
|
|
|
|
},
|
2022-05-08 15:22:32 +00:00
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'nowebrtc.js',
|
|
|
|
|
|
fn: noWebrtc,
|
|
|
|
|
|
});
|
|
|
|
|
|
// Prevent web pages from using RTCPeerConnection(), and report attempts in console.
|
|
|
|
|
|
function noWebrtc() {
|
2019-07-06 16:36:28 +00: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 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'disable-newtab-links.js',
|
|
|
|
|
|
fn: disableNewtabLinks,
|
|
|
|
|
|
});
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/913
|
|
|
|
|
|
function disableNewtabLinks() {
|
2025-02-25 18:02:44 +00:00
|
|
|
|
document.addEventListener('click', ev => {
|
|
|
|
|
|
let target = ev.target;
|
2019-07-06 16:36:28 +00:00
|
|
|
|
while ( target !== null ) {
|
|
|
|
|
|
if ( target.localName === 'a' && target.hasAttribute('target') ) {
|
|
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
target = target.parentNode;
|
|
|
|
|
|
}
|
2025-02-25 18:02:44 +00:00
|
|
|
|
}, { capture: true });
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-06 16:36:28 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'xml-prune.js',
|
|
|
|
|
|
fn: xmlPrune,
|
2023-03-26 13:13:17 +00:00
|
|
|
|
dependencies: [
|
2023-05-27 21:26:19 +00:00
|
|
|
|
'safe-self.fn',
|
2023-03-26 13:13:17 +00:00
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function xmlPrune(
|
|
|
|
|
|
selector = '',
|
|
|
|
|
|
selectorCheck = '',
|
|
|
|
|
|
urlPattern = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof selector !== 'string' ) { return; }
|
2022-09-25 10:57:51 +00:00
|
|
|
|
if ( selector === '' ) { return; }
|
2023-07-27 12:39:28 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('xml-prune', selector, selectorCheck, urlPattern);
|
2023-08-08 11:41:21 +00:00
|
|
|
|
const reUrl = safe.patternToRegex(urlPattern);
|
|
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
2023-05-27 21:26:19 +00:00
|
|
|
|
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 13:14:19 +00:00
|
|
|
|
const pruneFromDoc = xmlDoc => {
|
2022-09-25 00:49:00 +00:00
|
|
|
|
try {
|
|
|
|
|
|
if ( selectorCheck !== '' && xmlDoc.querySelector(selectorCheck) === null ) {
|
2023-05-27 13:14:19 +00:00
|
|
|
|
return xmlDoc;
|
2022-09-25 00:49:00 +00:00
|
|
|
|
}
|
2023-07-27 12:39:28 +00:00
|
|
|
|
if ( extraArgs.logdoc ) {
|
|
|
|
|
|
const serializer = new XMLSerializer();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Document is\n\t${serializer.serializeToString(xmlDoc)}`);
|
2023-07-27 12:39:28 +00:00
|
|
|
|
}
|
2023-06-05 12:51:20 +00:00
|
|
|
|
const items = queryAll(xmlDoc, selector);
|
2023-07-27 12:39:28 +00:00
|
|
|
|
if ( items.length === 0 ) { return xmlDoc; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Removing ${items.length} items`);
|
2023-07-27 12:39:28 +00:00
|
|
|
|
for ( const item of items ) {
|
|
|
|
|
|
if ( item.nodeType === 1 ) {
|
|
|
|
|
|
item.remove();
|
|
|
|
|
|
} else if ( item.nodeType === 2 ) {
|
|
|
|
|
|
item.ownerElement.removeAttribute(item.nodeName);
|
2022-09-25 00:49:00 +00:00
|
|
|
|
}
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `${item.constructor.name}.${item.nodeName} removed`);
|
2022-09-25 00:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
} catch(ex) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboErr(logPrefix, `Error: ${ex}`);
|
2022-09-25 00:49:00 +00:00
|
|
|
|
}
|
2023-05-27 13:14:19 +00: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);
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2023-05-27 13:14:19 +00:00
|
|
|
|
}
|
2022-09-25 00:49:00 +00:00
|
|
|
|
return text;
|
|
|
|
|
|
};
|
2022-09-25 10:49:41 +00:00
|
|
|
|
const urlFromArg = arg => {
|
|
|
|
|
|
if ( typeof arg === 'string' ) { return arg; }
|
|
|
|
|
|
if ( arg instanceof Request ) { return arg.url; }
|
|
|
|
|
|
return String(arg);
|
|
|
|
|
|
};
|
2022-09-25 00:49:00 +00:00
|
|
|
|
self.fetch = new Proxy(self.fetch, {
|
|
|
|
|
|
apply: function(target, thisArg, args) {
|
2023-08-20 12:36:16 +00:00
|
|
|
|
const fetchPromise = Reflect.apply(target, thisArg, args);
|
2022-09-25 10:57:51 +00:00
|
|
|
|
if ( reUrl.test(urlFromArg(args[0])) === false ) {
|
2023-08-20 12:36:16 +00:00
|
|
|
|
return fetchPromise;
|
|
|
|
|
|
}
|
|
|
|
|
|
return fetchPromise.then(responseBefore => {
|
|
|
|
|
|
const response = responseBefore.clone();
|
|
|
|
|
|
return response.text().then(text => {
|
|
|
|
|
|
const responseAfter = new Response(pruneFromText(text), {
|
|
|
|
|
|
status: responseBefore.status,
|
|
|
|
|
|
statusText: responseBefore.statusText,
|
|
|
|
|
|
headers: responseBefore.headers,
|
|
|
|
|
|
});
|
|
|
|
|
|
Object.defineProperties(responseAfter, {
|
|
|
|
|
|
ok: { value: responseBefore.ok },
|
|
|
|
|
|
redirected: { value: responseBefore.redirected },
|
|
|
|
|
|
type: { value: responseBefore.type },
|
|
|
|
|
|
url: { value: responseBefore.url },
|
|
|
|
|
|
});
|
|
|
|
|
|
return responseAfter;
|
|
|
|
|
|
}).catch(( ) =>
|
|
|
|
|
|
responseBefore
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2022-09-25 00:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-05-27 13:14:19 +00: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;
|
2023-07-27 12:39:28 +00:00
|
|
|
|
if (
|
|
|
|
|
|
type === 'document' ||
|
|
|
|
|
|
type === '' && thisArg.responseXML instanceof XMLDocument
|
|
|
|
|
|
) {
|
2023-07-25 13:05:39 +00:00
|
|
|
|
pruneFromDoc(thisArg.responseXML);
|
2023-12-22 15:15:37 +00:00
|
|
|
|
const serializer = new XMLSerializer();
|
|
|
|
|
|
const textout = serializer.serializeToString(thisArg.responseXML);
|
|
|
|
|
|
Object.defineProperty(thisArg, 'responseText', { value: textout });
|
2024-09-03 15:15:16 +00:00
|
|
|
|
if ( typeof thisArg.response === 'string' ) {
|
|
|
|
|
|
Object.defineProperty(thisArg, 'response', { value: textout });
|
|
|
|
|
|
}
|
2023-07-25 13:05:39 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-07-27 12:39:28 +00:00
|
|
|
|
if (
|
|
|
|
|
|
type === 'text' ||
|
|
|
|
|
|
type === '' && typeof thisArg.responseText === 'string'
|
|
|
|
|
|
) {
|
2023-05-27 13:14:19 +00:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2022-09-25 00:49:00 +00:00
|
|
|
|
|
2023-03-26 13:13:17 +00:00
|
|
|
|
/******************************************************************************/
|
2019-07-08 12:56:36 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'm3u-prune.js',
|
|
|
|
|
|
fn: m3uPrune,
|
2023-06-18 18:29:11 +00:00
|
|
|
|
dependencies: [
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
2022-09-27 02:27:50 +00:00
|
|
|
|
// https://en.wikipedia.org/wiki/M3U
|
2023-03-24 18:05:18 +00:00
|
|
|
|
function m3uPrune(
|
|
|
|
|
|
m3uPattern = '',
|
|
|
|
|
|
urlPattern = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof m3uPattern !== 'string' ) { return; }
|
2023-06-18 18:29:11 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('m3u-prune', m3uPattern, urlPattern);
|
|
|
|
|
|
const toLog = [];
|
2022-09-27 02:27:50 +00:00
|
|
|
|
const regexFromArg = arg => {
|
|
|
|
|
|
if ( arg === '' ) { return /^/; }
|
2023-03-12 21:45:02 +00: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 16:37:04 +00:00
|
|
|
|
return new RegExp(
|
|
|
|
|
|
arg.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*+/g, '.*?')
|
|
|
|
|
|
);
|
2022-09-27 02:27:50 +00:00
|
|
|
|
};
|
|
|
|
|
|
const reM3u = regexFromArg(m3uPattern);
|
|
|
|
|
|
const reUrl = regexFromArg(urlPattern);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
const pruneSpliceoutBlock = (lines, i) => {
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-CUE:TYPE="SpliceOut"') === false ) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-ASSET:CAID') ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-SCTE35:') ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-CUE-IN') ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-SCTE35:') ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
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; }
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push('Discarding', `\t${lines[i]}, \t${lines[i+1]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = lines[i+1] = undefined; i += 2;
|
|
|
|
|
|
if ( lines[i].startsWith('#EXT-X-DISCONTINUITY') ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
toLog.push(`\t${lines[i]}`);
|
2022-09-27 02:37:11 +00:00
|
|
|
|
lines[i] = undefined; i += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
};
|
2022-09-27 02:27:50 +00:00
|
|
|
|
const pruner = text => {
|
|
|
|
|
|
if ( (/^\s*#EXTM3U/.test(text)) === false ) { return text; }
|
2024-04-11 19:44:07 +00:00
|
|
|
|
if ( m3uPattern === '' ) {
|
|
|
|
|
|
safe.uboLog(` Content:\n${text}`);
|
|
|
|
|
|
return text;
|
|
|
|
|
|
}
|
2023-03-12 21:45:02 +00:00
|
|
|
|
if ( reM3u.multiline ) {
|
|
|
|
|
|
reM3u.lastIndex = 0;
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
const match = reM3u.exec(text);
|
|
|
|
|
|
if ( match === null ) { break; }
|
2023-06-18 18:29:11 +00:00
|
|
|
|
let discard = match[0];
|
|
|
|
|
|
let before = text.slice(0, match.index);
|
|
|
|
|
|
if (
|
|
|
|
|
|
/^[\n\r]+/.test(discard) === false &&
|
|
|
|
|
|
/[\n\r]+$/.test(before) === false
|
|
|
|
|
|
) {
|
|
|
|
|
|
const startOfLine = /[^\n\r]+$/.exec(before);
|
|
|
|
|
|
if ( startOfLine !== null ) {
|
|
|
|
|
|
before = before.slice(0, startOfLine.index);
|
|
|
|
|
|
discard = startOfLine[0] + discard;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let after = text.slice(match.index + match[0].length);
|
|
|
|
|
|
if (
|
|
|
|
|
|
/[\n\r]+$/.test(discard) === false &&
|
|
|
|
|
|
/^[\n\r]+/.test(after) === false
|
|
|
|
|
|
) {
|
|
|
|
|
|
const endOfLine = /^[^\n\r]+/.exec(after);
|
|
|
|
|
|
if ( endOfLine !== null ) {
|
|
|
|
|
|
after = after.slice(endOfLine.index);
|
|
|
|
|
|
discard += discard + endOfLine[0];
|
2023-03-12 21:45:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-06-18 18:29:11 +00:00
|
|
|
|
text = before.trim() + '\n' + after.trim();
|
|
|
|
|
|
reM3u.lastIndex = before.length + 1;
|
2024-12-06 16:53:42 +00:00
|
|
|
|
toLog.push('Discarding', ...safe.String_split.call(discard, /\n+/).map(s => `\t${s}`));
|
2023-03-12 21:45:02 +00:00
|
|
|
|
if ( reM3u.global === false ) { break; }
|
|
|
|
|
|
}
|
2023-06-18 18:29:11 +00:00
|
|
|
|
return text;
|
2023-03-12 21:45:02 +00:00
|
|
|
|
}
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const lines = safe.String_split.call(text, /\n\r|\n|\r/);
|
2022-09-27 02:27:50 +00:00
|
|
|
|
for ( let i = 0; i < lines.length; i++ ) {
|
2022-09-27 02:37:11 +00: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 02:27:50 +00: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 =>
|
2024-01-25 17:20:38 +00:00
|
|
|
|
realResponse.text().then(text => {
|
|
|
|
|
|
const response = new Response(pruner(text), {
|
2022-09-27 02:27:50 +00:00
|
|
|
|
status: realResponse.status,
|
|
|
|
|
|
statusText: realResponse.statusText,
|
|
|
|
|
|
headers: realResponse.headers,
|
2024-01-25 17:20:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
if ( toLog.length !== 0 ) {
|
|
|
|
|
|
toLog.unshift(logPrefix);
|
|
|
|
|
|
safe.uboLog(toLog.join('\n'));
|
|
|
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
|
|
|
})
|
2022-09-27 02:27:50 +00: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 !== '' && type !== 'text' ) { return; }
|
|
|
|
|
|
const textin = thisArg.responseText;
|
|
|
|
|
|
const textout = pruner(textin);
|
|
|
|
|
|
if ( textout === textin ) { return; }
|
2022-09-27 02:37:11 +00:00
|
|
|
|
Object.defineProperty(thisArg, 'response', { value: textout });
|
|
|
|
|
|
Object.defineProperty(thisArg, 'responseText', { value: textout });
|
2024-01-25 17:20:38 +00:00
|
|
|
|
if ( toLog.length !== 0 ) {
|
|
|
|
|
|
toLog.unshift(logPrefix);
|
|
|
|
|
|
safe.uboLog(toLog.join('\n'));
|
|
|
|
|
|
}
|
2022-09-27 02:27:50 +00:00
|
|
|
|
});
|
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2022-09-27 02:27:50 +00:00
|
|
|
|
|
2023-07-06 12:51:31 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* @scriptlet call-nothrow
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Prevent a function call from throwing. The function will be called, however
|
|
|
|
|
|
* should it throw, the scriptlet will silently process the exception and
|
|
|
|
|
|
* returns as if no exception has occurred.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ### Syntax
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```text
|
|
|
|
|
|
* example.org##+js(call-nothrow, propertyChain)
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* - `propertyChain`: a chain of dot-separated properties which leads to the
|
|
|
|
|
|
* function to be trapped.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ### Examples
|
|
|
|
|
|
*
|
|
|
|
|
|
* example.org##+js(call-nothrow, Object.defineProperty)
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
2023-03-09 13:49:26 +00:00
|
|
|
|
|
2023-03-24 18:05:18 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'call-nothrow.js',
|
|
|
|
|
|
fn: callNothrow,
|
2024-12-06 16:53:42 +00:00
|
|
|
|
dependencies: [
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
2023-03-24 18:05:18 +00:00
|
|
|
|
});
|
|
|
|
|
|
function callNothrow(
|
|
|
|
|
|
chain = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( typeof chain !== 'string' ) { return; }
|
|
|
|
|
|
if ( chain === '' ) { return; }
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const parts = safe.String_split.call(chain, '.');
|
2023-03-14 22:50:01 +00:00
|
|
|
|
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);
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2023-03-14 22:50:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2023-03-24 18:05:18 +00:00
|
|
|
|
}
|
2023-03-14 22:50:01 +00:00
|
|
|
|
|
2023-05-25 12:51:26 +00:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'remove-node-text.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'rmnt.js',
|
|
|
|
|
|
],
|
2023-05-25 12:51:26 +00:00
|
|
|
|
fn: removeNodeText,
|
|
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
|
dependencies: [
|
2023-10-17 21:33:49 +00:00
|
|
|
|
'replace-node-text.fn',
|
2023-05-25 12:51:26 +00:00
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function removeNodeText(
|
|
|
|
|
|
nodeName,
|
2024-07-20 13:43:08 +00:00
|
|
|
|
includes,
|
2023-05-25 12:51:26 +00:00
|
|
|
|
...extraArgs
|
|
|
|
|
|
) {
|
2024-07-20 13:43:08 +00:00
|
|
|
|
replaceNodeTextFn(nodeName, '', '', 'includes', includes || '', ...extraArgs);
|
2023-05-25 12:51:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-28 15:26:45 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* @scriptlet prevent-canvas
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Prevent usage of specific or all (default) canvas APIs.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ### Syntax
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```text
|
|
|
|
|
|
* example.com##+js(prevent-canvas [, contextType])
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* - `contextType`: A specific type of canvas API to prevent (default to all
|
|
|
|
|
|
* APIs). Can be a string or regex which will be matched against the type
|
|
|
|
|
|
* used in getContext() call. Prepend with `!` to test for no-match.
|
|
|
|
|
|
*
|
|
|
|
|
|
* ### Examples
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1. Prevent `example.com` from accessing all canvas APIs
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```adblock
|
|
|
|
|
|
* example.com##+js(prevent-canvas)
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* 2. Prevent access to any flavor of WebGL API, everywhere
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```adblock
|
|
|
|
|
|
* *##+js(prevent-canvas, /webgl/)
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* 3. Prevent `example.com` from accessing any flavor of canvas API except `2d`
|
|
|
|
|
|
*
|
|
|
|
|
|
* ```adblock
|
|
|
|
|
|
* example.com##+js(prevent-canvas, !2d)
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*
|
|
|
|
|
|
* ### References
|
|
|
|
|
|
*
|
|
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'prevent-canvas.js',
|
|
|
|
|
|
fn: preventCanvas,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function preventCanvas(
|
|
|
|
|
|
contextType = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const pattern = safe.initPattern(contextType, { canNegate: true });
|
|
|
|
|
|
const proto = globalThis.HTMLCanvasElement.prototype;
|
|
|
|
|
|
proto.getContext = new Proxy(proto.getContext, {
|
|
|
|
|
|
apply(target, thisArg, args) {
|
|
|
|
|
|
if ( safe.testPattern(pattern, args[0]) ) { return null; }
|
|
|
|
|
|
return Reflect.apply(target, thisArg, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-29 16:51:44 +00:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'multiup.js',
|
|
|
|
|
|
fn: multiup,
|
|
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
|
});
|
|
|
|
|
|
function multiup() {
|
|
|
|
|
|
const handler = ev => {
|
|
|
|
|
|
const target = ev.target;
|
|
|
|
|
|
if ( target.matches('button[link]') === false ) { return; }
|
|
|
|
|
|
const ancestor = target.closest('form');
|
|
|
|
|
|
if ( ancestor === null ) { return; }
|
|
|
|
|
|
if ( ancestor !== target.parentElement ) { return; }
|
|
|
|
|
|
const link = (target.getAttribute('link') || '').trim();
|
|
|
|
|
|
if ( link === '' ) { return; }
|
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
document.location.href = link;
|
|
|
|
|
|
};
|
|
|
|
|
|
document.addEventListener('click', handler, { capture: true });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-27 23:17:28 +00:00
|
|
|
|
|
2023-07-04 11:13:22 +00:00
|
|
|
|
|
2023-05-23 13:03:19 +00: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 19:56:42 +00:00
|
|
|
|
* replace-node-text.js
|
2023-05-23 13:03:19 +00: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 18:16:56 +00:00
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
2023-11-14 18:53:29 +00:00
|
|
|
|
name: 'trusted-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 18:16:56 +00:00
|
|
|
|
requiresTrust: true,
|
2023-06-28 23:35:22 +00:00
|
|
|
|
aliases: [
|
2023-11-14 18:53:29 +00:00
|
|
|
|
'trusted-rpnt.js',
|
|
|
|
|
|
'replace-node-text.js',
|
2023-06-28 23:35:22 +00:00
|
|
|
|
'rpnt.js',
|
|
|
|
|
|
],
|
2023-05-24 18:58:12 +00:00
|
|
|
|
fn: replaceNodeText,
|
2023-05-23 00:19:00 +00: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 18:16:56 +00:00
|
|
|
|
dependencies: [
|
2023-10-17 21:33:49 +00:00
|
|
|
|
'replace-node-text.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 18:16:56 +00:00
|
|
|
|
],
|
|
|
|
|
|
});
|
2023-05-24 18:58:12 +00:00
|
|
|
|
function replaceNodeText(
|
2023-05-25 12:51:26 +00: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 18:16:56 +00:00
|
|
|
|
) {
|
2023-10-17 21:33:49 +00:00
|
|
|
|
replaceNodeTextFn(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 18:16:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-05 15:55:47 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* trusted-replace-fetch-response.js
|
|
|
|
|
|
*
|
|
|
|
|
|
* Replaces response text content of fetch requests if all given parameters
|
|
|
|
|
|
* match.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Reference:
|
|
|
|
|
|
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-replace-fetch-response.js
|
|
|
|
|
|
*
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-replace-fetch-response.js',
|
|
|
|
|
|
requiresTrust: true,
|
2024-01-20 15:33:36 +00:00
|
|
|
|
aliases: [
|
|
|
|
|
|
'trusted-rpfr.js',
|
|
|
|
|
|
],
|
2023-08-05 15:55:47 +00:00
|
|
|
|
fn: trustedReplaceFetchResponse,
|
|
|
|
|
|
dependencies: [
|
2023-10-07 15:44:18 +00:00
|
|
|
|
'replace-fetch-response.fn',
|
2023-08-05 15:55:47 +00:00
|
|
|
|
],
|
|
|
|
|
|
});
|
2023-10-07 15:44:18 +00:00
|
|
|
|
function trustedReplaceFetchResponse(...args) {
|
|
|
|
|
|
replaceFetchResponseFn(true, ...args);
|
2023-08-05 15:55:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-23 14:59:27 +00:00
|
|
|
|
/******************************************************************************/
|
2023-09-04 18:54:57 +00:00
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-replace-xhr-response.js',
|
2023-09-05 18:11:33 +00:00
|
|
|
|
requiresTrust: true,
|
2023-09-04 18:54:57 +00:00
|
|
|
|
fn: trustedReplaceXhrResponse,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'match-object-properties.fn',
|
|
|
|
|
|
'parse-properties-to-match.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedReplaceXhrResponse(
|
|
|
|
|
|
pattern = '',
|
|
|
|
|
|
replacement = '',
|
|
|
|
|
|
propsToMatch = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-replace-xhr-response', pattern, replacement, propsToMatch);
|
2023-09-04 18:54:57 +00:00
|
|
|
|
const xhrInstances = new WeakMap();
|
|
|
|
|
|
if ( pattern === '*' ) { pattern = '.*'; }
|
|
|
|
|
|
const rePattern = safe.patternToRegex(pattern);
|
2025-03-22 20:01:43 +00:00
|
|
|
|
const propNeedles = parsePropertiesToMatchFn(propsToMatch, 'url');
|
2024-06-13 13:32:30 +00:00
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
|
|
|
|
|
const reIncludes = extraArgs.includes ? safe.patternToRegex(extraArgs.includes) : null;
|
2023-09-04 18:54:57 +00:00
|
|
|
|
self.XMLHttpRequest = class extends self.XMLHttpRequest {
|
|
|
|
|
|
open(method, url, ...args) {
|
|
|
|
|
|
const outerXhr = this;
|
2023-09-05 18:11:33 +00:00
|
|
|
|
const xhrDetails = { method, url };
|
2023-09-04 18:54:57 +00:00
|
|
|
|
let outcome = 'match';
|
|
|
|
|
|
if ( propNeedles.size !== 0 ) {
|
2025-03-22 20:01:43 +00:00
|
|
|
|
if ( matchObjectPropertiesFn(propNeedles, xhrDetails) === undefined ) {
|
2023-09-04 18:54:57 +00:00
|
|
|
|
outcome = 'nomatch';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( outcome === 'match' ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
if ( safe.logLevel > 1 ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, `Matched "propsToMatch"`);
|
|
|
|
|
|
}
|
2023-09-04 18:54:57 +00:00
|
|
|
|
xhrInstances.set(outerXhr, xhrDetails);
|
|
|
|
|
|
}
|
|
|
|
|
|
return super.open(method, url, ...args);
|
|
|
|
|
|
}
|
2023-09-05 18:11:33 +00:00
|
|
|
|
get response() {
|
|
|
|
|
|
const innerResponse = super.response;
|
|
|
|
|
|
const xhrDetails = xhrInstances.get(this);
|
2023-09-04 18:54:57 +00:00
|
|
|
|
if ( xhrDetails === undefined ) {
|
2023-09-05 18:11:33 +00:00
|
|
|
|
return innerResponse;
|
2023-09-04 18:54:57 +00:00
|
|
|
|
}
|
2023-09-22 13:33:02 +00:00
|
|
|
|
const responseLength = typeof innerResponse === 'string'
|
|
|
|
|
|
? innerResponse.length
|
|
|
|
|
|
: undefined;
|
|
|
|
|
|
if ( xhrDetails.lastResponseLength !== responseLength ) {
|
|
|
|
|
|
xhrDetails.response = undefined;
|
|
|
|
|
|
xhrDetails.lastResponseLength = responseLength;
|
2023-09-05 18:11:33 +00:00
|
|
|
|
}
|
2023-09-22 13:33:02 +00:00
|
|
|
|
if ( xhrDetails.response !== undefined ) {
|
|
|
|
|
|
return xhrDetails.response;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( typeof innerResponse !== 'string' ) {
|
|
|
|
|
|
return (xhrDetails.response = innerResponse);
|
2023-09-05 18:11:33 +00:00
|
|
|
|
}
|
2024-06-13 13:32:30 +00:00
|
|
|
|
if ( reIncludes && reIncludes.test(innerResponse) === false ) {
|
|
|
|
|
|
return (xhrDetails.response = innerResponse);
|
|
|
|
|
|
}
|
2023-09-05 18:11:33 +00:00
|
|
|
|
const textBefore = innerResponse;
|
|
|
|
|
|
const textAfter = textBefore.replace(rePattern, replacement);
|
2024-01-25 17:20:38 +00:00
|
|
|
|
if ( textAfter !== textBefore ) {
|
|
|
|
|
|
safe.uboLog(logPrefix, 'Match');
|
2023-09-05 18:11:33 +00:00
|
|
|
|
}
|
2023-09-22 13:33:02 +00:00
|
|
|
|
return (xhrDetails.response = textAfter);
|
2023-09-05 18:11:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
get responseText() {
|
2023-09-14 15:13:58 +00:00
|
|
|
|
const response = this.response;
|
|
|
|
|
|
if ( typeof response !== 'string' ) {
|
|
|
|
|
|
return super.responseText;
|
|
|
|
|
|
}
|
|
|
|
|
|
return response;
|
2023-09-04 18:54:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-15 15:08:15 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* trusted-click-element.js
|
|
|
|
|
|
*
|
|
|
|
|
|
* Reference API:
|
|
|
|
|
|
* https://github.com/AdguardTeam/Scriptlets/blob/master/src/scriptlets/trusted-click-element.js
|
|
|
|
|
|
*
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-click-element.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedClickElement,
|
|
|
|
|
|
world: 'ISOLATED',
|
|
|
|
|
|
dependencies: [
|
2024-01-20 15:33:36 +00:00
|
|
|
|
'get-all-cookies.fn',
|
|
|
|
|
|
'get-all-local-storage.fn',
|
2023-10-15 15:08:15 +00:00
|
|
|
|
'run-at-html-element.fn',
|
2023-10-16 23:53:48 +00:00
|
|
|
|
'safe-self.fn',
|
2023-10-15 15:08:15 +00:00
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedClickElement(
|
|
|
|
|
|
selectors = '',
|
2024-01-20 15:33:36 +00:00
|
|
|
|
extraMatch = '',
|
2023-10-15 15:08:15 +00:00
|
|
|
|
delay = ''
|
|
|
|
|
|
) {
|
2023-10-16 23:53:48 +00:00
|
|
|
|
const safe = safeSelf();
|
2024-01-25 17:20:38 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-click-element', selectors, extraMatch, delay);
|
2023-10-16 23:53:48 +00:00
|
|
|
|
|
2024-01-20 15:33:36 +00:00
|
|
|
|
if ( extraMatch !== '' ) {
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const assertions = safe.String_split.call(extraMatch, ',').map(s => {
|
2024-01-20 15:33:36 +00:00
|
|
|
|
const pos1 = s.indexOf(':');
|
|
|
|
|
|
const s1 = pos1 !== -1 ? s.slice(0, pos1) : s;
|
|
|
|
|
|
const not = s1.startsWith('!');
|
|
|
|
|
|
const type = not ? s1.slice(1) : s1;
|
|
|
|
|
|
const s2 = pos1 !== -1 ? s.slice(pos1+1).trim() : '';
|
|
|
|
|
|
if ( s2 === '' ) { return; }
|
|
|
|
|
|
const out = { not, type };
|
|
|
|
|
|
const match = /^\/(.+)\/(i?)$/.exec(s2);
|
|
|
|
|
|
if ( match !== null ) {
|
|
|
|
|
|
out.re = new RegExp(match[1], match[2] || undefined);
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}
|
|
|
|
|
|
const pos2 = s2.indexOf('=');
|
|
|
|
|
|
const key = pos2 !== -1 ? s2.slice(0, pos2).trim() : s2;
|
|
|
|
|
|
const value = pos2 !== -1 ? s2.slice(pos2+1).trim() : '';
|
|
|
|
|
|
out.re = new RegExp(`^${this.escapeRegexChars(key)}=${this.escapeRegexChars(value)}`);
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}).filter(details => details !== undefined);
|
|
|
|
|
|
const allCookies = assertions.some(o => o.type === 'cookie')
|
|
|
|
|
|
? getAllCookiesFn()
|
|
|
|
|
|
: [];
|
|
|
|
|
|
const allStorageItems = assertions.some(o => o.type === 'localStorage')
|
|
|
|
|
|
? getAllLocalStorageFn()
|
|
|
|
|
|
: [];
|
|
|
|
|
|
const hasNeedle = (haystack, needle) => {
|
|
|
|
|
|
for ( const { key, value } of haystack ) {
|
|
|
|
|
|
if ( needle.test(`${key}=${value}`) ) { return true; }
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
for ( const { not, type, re } of assertions ) {
|
|
|
|
|
|
switch ( type ) {
|
|
|
|
|
|
case 'cookie':
|
|
|
|
|
|
if ( hasNeedle(allCookies, re) === not ) { return; }
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'localStorage':
|
|
|
|
|
|
if ( hasNeedle(allStorageItems, re) === not ) { return; }
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-14 15:36:17 +00:00
|
|
|
|
const getShadowRoot = elem => {
|
|
|
|
|
|
// Firefox
|
|
|
|
|
|
if ( elem.openOrClosedShadowRoot ) {
|
|
|
|
|
|
return elem.openOrClosedShadowRoot;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Chromium
|
|
|
|
|
|
if ( typeof chrome === 'object' ) {
|
|
|
|
|
|
if ( chrome.dom && chrome.dom.openOrClosedShadowRoot ) {
|
|
|
|
|
|
return chrome.dom.openOrClosedShadowRoot(elem);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-12-04 13:02:07 +00:00
|
|
|
|
const querySelectorEx = (selector, context = document) => {
|
|
|
|
|
|
const pos = selector.indexOf(' >>> ');
|
|
|
|
|
|
if ( pos === -1 ) { return context.querySelector(selector); }
|
|
|
|
|
|
const outside = selector.slice(0, pos).trim();
|
|
|
|
|
|
const inside = selector.slice(pos + 5).trim();
|
|
|
|
|
|
const elem = context.querySelector(outside);
|
|
|
|
|
|
if ( elem === null ) { return null; }
|
2024-05-14 15:36:17 +00:00
|
|
|
|
const shadowRoot = getShadowRoot(elem);
|
2023-12-04 13:02:07 +00:00
|
|
|
|
return shadowRoot && querySelectorEx(inside, shadowRoot);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const selectorList = safe.String_split.call(selectors, /\s*,\s*/)
|
2023-10-15 15:08:15 +00:00
|
|
|
|
.filter(s => {
|
|
|
|
|
|
try {
|
2023-12-04 13:02:07 +00:00
|
|
|
|
void querySelectorEx(s);
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2023-10-15 15:08:15 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
});
|
|
|
|
|
|
if ( selectorList.length === 0 ) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
const clickDelay = parseInt(delay, 10) || 1;
|
|
|
|
|
|
const t0 = Date.now();
|
|
|
|
|
|
const tbye = t0 + 10000;
|
2023-10-19 21:23:05 +00:00
|
|
|
|
let tnext = selectorList.length !== 1 ? t0 : t0 + clickDelay;
|
2023-10-15 15:08:15 +00:00
|
|
|
|
|
|
|
|
|
|
const terminate = ( ) => {
|
|
|
|
|
|
selectorList.length = 0;
|
|
|
|
|
|
next.stop();
|
|
|
|
|
|
observe.stop();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const next = notFound => {
|
2023-10-16 23:53:48 +00:00
|
|
|
|
if ( selectorList.length === 0 ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Completed');
|
2023-10-16 23:53:48 +00:00
|
|
|
|
return terminate();
|
|
|
|
|
|
}
|
2023-10-15 15:08:15 +00:00
|
|
|
|
const tnow = Date.now();
|
2023-10-16 23:53:48 +00:00
|
|
|
|
if ( tnow >= tbye ) {
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Timed out');
|
2023-10-16 23:53:48 +00:00
|
|
|
|
return terminate();
|
|
|
|
|
|
}
|
2023-10-15 15:08:15 +00:00
|
|
|
|
if ( notFound ) { observe(); }
|
2023-10-19 21:23:05 +00:00
|
|
|
|
const delay = Math.max(notFound ? tbye - tnow : tnext - tnow, 1);
|
2023-10-15 15:08:15 +00:00
|
|
|
|
next.timer = setTimeout(( ) => {
|
|
|
|
|
|
next.timer = undefined;
|
|
|
|
|
|
process();
|
|
|
|
|
|
}, delay);
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Waiting for ${selectorList[0]}...`);
|
2023-10-15 15:08:15 +00:00
|
|
|
|
};
|
|
|
|
|
|
next.stop = ( ) => {
|
|
|
|
|
|
if ( next.timer === undefined ) { return; }
|
|
|
|
|
|
clearTimeout(next.timer);
|
|
|
|
|
|
next.timer = undefined;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const observe = ( ) => {
|
|
|
|
|
|
if ( observe.observer !== undefined ) { return; }
|
|
|
|
|
|
observe.observer = new MutationObserver(( ) => {
|
|
|
|
|
|
if ( observe.timer !== undefined ) { return; }
|
|
|
|
|
|
observe.timer = setTimeout(( ) => {
|
|
|
|
|
|
observe.timer = undefined;
|
|
|
|
|
|
process();
|
|
|
|
|
|
}, 20);
|
|
|
|
|
|
});
|
|
|
|
|
|
observe.observer.observe(document, {
|
|
|
|
|
|
attributes: true,
|
|
|
|
|
|
childList: true,
|
|
|
|
|
|
subtree: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
observe.stop = ( ) => {
|
|
|
|
|
|
if ( observe.timer !== undefined ) {
|
|
|
|
|
|
clearTimeout(observe.timer);
|
|
|
|
|
|
observe.timer = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( observe.observer ) {
|
|
|
|
|
|
observe.observer.disconnect();
|
|
|
|
|
|
observe.observer = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const process = ( ) => {
|
|
|
|
|
|
next.stop();
|
|
|
|
|
|
if ( Date.now() < tnext ) { return next(); }
|
|
|
|
|
|
const selector = selectorList.shift();
|
|
|
|
|
|
if ( selector === undefined ) { return terminate(); }
|
2023-12-04 13:02:07 +00:00
|
|
|
|
const elem = querySelectorEx(selector);
|
2023-10-15 15:08:15 +00:00
|
|
|
|
if ( elem === null ) {
|
|
|
|
|
|
selectorList.unshift(selector);
|
|
|
|
|
|
return next(true);
|
|
|
|
|
|
}
|
2024-01-25 17:20:38 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Clicked ${selector}`);
|
2023-10-15 20:40:35 +00:00
|
|
|
|
elem.click();
|
2023-10-15 15:08:15 +00:00
|
|
|
|
tnext += clickDelay;
|
|
|
|
|
|
next();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
runAtHtmlElementFn(process);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-04 18:54:57 +00:00
|
|
|
|
/******************************************************************************/
|
Add `trusted-prune-inbound-object` scriptlet
As per discussion with filter list maintainers.
To perform object pruning for any given call which has an object
as argument (hence "inbound").
Since `json-prune-stringify` scriptlet is a specific form of
pruning inbound objects, it has been removed.
The arguments for `trusted-prune-inbound-object` in order are:
- The name of the property to trap. Must be a function, and must
exist when the scriptlet tries to install the trap.
- The position of the object to prune in the argument list when
the trapped function is called. The position is 1-based and
must be an integer greater than 0.
- The properties to prune (as with `json-prune`)
- The properties which must all be present for pruning to occur
(as with `json-prune`)
- Varargs:
- `, dontOverwrite, 1`: do not modify the target inbound object
Examples:
Remove `title` and `name` properties before passing the object to
`JSON.stringify` call:
example.org##+js(trusted-prune-inbound-object, JSON.stringify, 1, title name)
Remove `status` property before passing the object to `Object.keys`
call but do not modify caller's instance of the object:
example.org##+js(trusted-prune-inbound-object, Object.keys, 1, status, , dontOverwrite, 1)
2023-10-21 13:31:50 +00:00
|
|
|
|
|
2024-04-01 15:27:19 +00:00
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-replace-outbound-text.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedReplaceOutboundText,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'proxy-apply.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedReplaceOutboundText(
|
|
|
|
|
|
propChain = '',
|
2024-08-27 16:49:35 +00:00
|
|
|
|
rawPattern = '',
|
|
|
|
|
|
rawReplacement = '',
|
2024-04-02 15:04:27 +00:00
|
|
|
|
...args
|
2024-04-01 15:27:19 +00:00
|
|
|
|
) {
|
|
|
|
|
|
if ( propChain === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
2024-08-27 16:49:35 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-replace-outbound-text', propChain, rawPattern, rawReplacement, ...args);
|
|
|
|
|
|
const rePattern = safe.patternToRegex(rawPattern);
|
|
|
|
|
|
const replacement = rawReplacement.startsWith('json:')
|
|
|
|
|
|
? safe.JSON_parse(rawReplacement.slice(5))
|
|
|
|
|
|
: rawReplacement;
|
2024-04-02 15:04:27 +00:00
|
|
|
|
const extraArgs = safe.getExtraArgs(args);
|
|
|
|
|
|
const reCondition = safe.patternToRegex(extraArgs.condition || '');
|
2024-09-17 13:09:19 +00:00
|
|
|
|
proxyApplyFn(propChain, function(context) {
|
|
|
|
|
|
const encodedTextBefore = context.reflect();
|
2024-06-05 12:40:02 +00:00
|
|
|
|
let textBefore = encodedTextBefore;
|
|
|
|
|
|
if ( extraArgs.encoding === 'base64' ) {
|
|
|
|
|
|
try { textBefore = self.atob(encodedTextBefore); }
|
2025-01-09 14:33:22 +00:00
|
|
|
|
catch { return encodedTextBefore; }
|
2024-06-05 12:40:02 +00:00
|
|
|
|
}
|
2024-08-27 16:49:35 +00:00
|
|
|
|
if ( rawPattern === '' ) {
|
2024-06-05 12:40:02 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Decoded outbound text:\n', textBefore);
|
|
|
|
|
|
return encodedTextBefore;
|
2024-04-01 15:27:19 +00:00
|
|
|
|
}
|
2024-04-02 15:04:27 +00:00
|
|
|
|
reCondition.lastIndex = 0;
|
2024-06-05 12:40:02 +00:00
|
|
|
|
if ( reCondition.test(textBefore) === false ) { return encodedTextBefore; }
|
2024-04-02 15:04:27 +00:00
|
|
|
|
const textAfter = textBefore.replace(rePattern, replacement);
|
2024-06-05 12:40:02 +00:00
|
|
|
|
if ( textAfter === textBefore ) { return encodedTextBefore; }
|
2024-04-02 15:04:27 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Matched and replaced');
|
|
|
|
|
|
if ( safe.logLevel > 1 ) {
|
2024-06-05 12:40:02 +00:00
|
|
|
|
safe.uboLog(logPrefix, 'Modified decoded outbound text:\n', textAfter);
|
|
|
|
|
|
}
|
|
|
|
|
|
let encodedTextAfter = textAfter;
|
|
|
|
|
|
if ( extraArgs.encoding === 'base64' ) {
|
|
|
|
|
|
encodedTextAfter = self.btoa(textAfter);
|
2024-04-01 15:27:19 +00:00
|
|
|
|
}
|
2024-06-05 12:40:02 +00:00
|
|
|
|
return encodedTextAfter;
|
2024-04-01 15:27:19 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-09 17:03:50 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* Reference:
|
|
|
|
|
|
* https://github.com/AdguardTeam/Scriptlets/blob/5a92d79489/wiki/about-trusted-scriptlets.md#trusted-suppress-native-method
|
|
|
|
|
|
*
|
|
|
|
|
|
* This is a first version with current limitations:
|
|
|
|
|
|
* - Does not support matching arguments which are object or array
|
|
|
|
|
|
* - Does not support `stack` parameter
|
|
|
|
|
|
*
|
|
|
|
|
|
* If `signatureStr` parameter is not declared, the scriptlet will log all calls
|
|
|
|
|
|
* to `methodPath` along with the arguments passed and will not prevent the
|
|
|
|
|
|
* trapped method.
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-suppress-native-method.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedSuppressNativeMethod,
|
|
|
|
|
|
dependencies: [
|
2024-12-11 13:47:10 +00:00
|
|
|
|
'matches-stack-trace.fn',
|
2024-07-09 17:03:50 +00:00
|
|
|
|
'proxy-apply.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedSuppressNativeMethod(
|
|
|
|
|
|
methodPath = '',
|
|
|
|
|
|
signature = '',
|
|
|
|
|
|
how = '',
|
|
|
|
|
|
stack = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( methodPath === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
2024-12-11 13:47:10 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-suppress-native-method', methodPath, signature, how, stack);
|
2024-12-06 16:53:42 +00:00
|
|
|
|
const signatureArgs = safe.String_split.call(signature, /\s*\|\s*/).map(v => {
|
2024-07-09 17:03:50 +00:00
|
|
|
|
if ( /^".*"$/.test(v) ) {
|
|
|
|
|
|
return { type: 'pattern', re: safe.patternToRegex(v.slice(1, -1)) };
|
|
|
|
|
|
}
|
2024-12-26 13:10:13 +00:00
|
|
|
|
if ( /^\/.+\/$/.test(v) ) {
|
|
|
|
|
|
return { type: 'pattern', re: safe.patternToRegex(v) };
|
|
|
|
|
|
}
|
2024-07-09 17:03:50 +00:00
|
|
|
|
if ( v === 'false' ) {
|
|
|
|
|
|
return { type: 'exact', value: false };
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( v === 'true' ) {
|
|
|
|
|
|
return { type: 'exact', value: true };
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( v === 'null' ) {
|
|
|
|
|
|
return { type: 'exact', value: null };
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( v === 'undefined' ) {
|
|
|
|
|
|
return { type: 'exact', value: undefined };
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-12-11 13:47:10 +00:00
|
|
|
|
const stackNeedle = safe.initPattern(stack, { canNegate: true });
|
2024-09-17 13:09:19 +00:00
|
|
|
|
proxyApplyFn(methodPath, function(context) {
|
|
|
|
|
|
const { callArgs } = context;
|
2024-07-09 17:03:50 +00:00
|
|
|
|
if ( signature === '' ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Arguments:\n${callArgs.join('\n')}`);
|
|
|
|
|
|
return context.reflect();
|
2024-07-09 17:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
for ( let i = 0; i < signatureArgs.length; i++ ) {
|
|
|
|
|
|
const signatureArg = signatureArgs[i];
|
|
|
|
|
|
if ( signatureArg === undefined ) { continue; }
|
2024-11-08 13:32:19 +00:00
|
|
|
|
const targetArg = i < callArgs.length ? callArgs[i] : undefined;
|
2024-07-09 17:03:50 +00:00
|
|
|
|
if ( signatureArg.type === 'exact' ) {
|
|
|
|
|
|
if ( targetArg !== signatureArg.value ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
return context.reflect();
|
2024-07-09 17:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( signatureArg.type === 'pattern' ) {
|
|
|
|
|
|
if ( safe.RegExp_test.call(signatureArg.re, targetArg) === false ) {
|
2024-09-17 13:09:19 +00:00
|
|
|
|
return context.reflect();
|
2024-07-09 17:03:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-11 13:47:10 +00:00
|
|
|
|
if ( stackNeedle.matchAll !== true ) {
|
2024-12-12 15:18:42 +00:00
|
|
|
|
const logLevel = safe.logLevel > 1 ? 'all' : '';
|
|
|
|
|
|
if ( matchesStackTraceFn(stackNeedle, logLevel) === false ) {
|
2024-12-11 13:47:10 +00:00
|
|
|
|
return context.reflect();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-08 13:32:19 +00:00
|
|
|
|
if ( how === 'debug' ) {
|
|
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
|
|
|
|
|
return context.reflect();
|
|
|
|
|
|
}
|
2024-09-17 13:09:19 +00:00
|
|
|
|
safe.uboLog(logPrefix, `Suppressed:\n${callArgs.join('\n')}`);
|
2024-07-09 17:03:50 +00:00
|
|
|
|
if ( how === 'abort' ) {
|
|
|
|
|
|
throw new ReferenceError();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-03 17:31:52 +00:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
*
|
|
|
|
|
|
* Trusted version of prevent-xhr(), which allows the use of an arbitrary
|
|
|
|
|
|
* string as response text.
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-prevent-xhr.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedPreventXhr,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'prevent-xhr.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedPreventXhr(...args) {
|
|
|
|
|
|
return preventXhrFn(true, ...args);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-04 16:24:35 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @trustedScriptlet trusted-prevent-dom-bypass
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Prevent the bypassing of uBO scriptlets through anonymous embedded context.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Ensure that a target method in the embedded context is using the
|
|
|
|
|
|
* corresponding parent context's method (which is assumed to be
|
|
|
|
|
|
* properly patched), or to replace the embedded context with that of the
|
|
|
|
|
|
* parent context.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Root issue:
|
|
|
|
|
|
* https://issues.chromium.org/issues/40202434
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param methodPath
|
|
|
|
|
|
* The method which calls must be intercepted. The arguments
|
|
|
|
|
|
* of the intercepted calls are assumed to be HTMLElement, anything else will
|
|
|
|
|
|
* be ignored.
|
|
|
|
|
|
*
|
2024-10-05 11:41:44 +00:00
|
|
|
|
* @param [targetProp]
|
2024-10-04 16:24:35 +00:00
|
|
|
|
* The method in the embedded context which should be delegated to the
|
|
|
|
|
|
* parent context. If no method is specified, the embedded context becomes
|
|
|
|
|
|
* the parent one, i.e. all properties of the embedded context will be that
|
|
|
|
|
|
* of the parent context.
|
|
|
|
|
|
*
|
2024-10-05 11:41:44 +00:00
|
|
|
|
* @example
|
|
|
|
|
|
* ##+js(trusted-prevent-dom-bypass, Element.prototype.append, open)
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ##+js(trusted-prevent-dom-bypass, Element.prototype.appendChild, XMLHttpRequest)
|
|
|
|
|
|
*
|
2024-10-04 16:24:35 +00:00
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-prevent-dom-bypass.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedPreventDomBypass,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'proxy-apply.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedPreventDomBypass(
|
|
|
|
|
|
methodPath = '',
|
2024-10-05 11:41:44 +00:00
|
|
|
|
targetProp = ''
|
2024-10-04 16:24:35 +00:00
|
|
|
|
) {
|
|
|
|
|
|
if ( methodPath === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
2024-10-05 11:41:44 +00:00
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-prevent-dom-bypass', methodPath, targetProp);
|
2024-10-04 16:24:35 +00:00
|
|
|
|
proxyApplyFn(methodPath, function(context) {
|
2024-10-05 11:41:44 +00:00
|
|
|
|
const elems = new Set(context.callArgs.filter(e => e instanceof HTMLElement));
|
2024-10-04 16:24:35 +00:00
|
|
|
|
const r = context.reflect();
|
|
|
|
|
|
if ( elems.length === 0 ) { return r; }
|
|
|
|
|
|
for ( const elem of elems ) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
if ( `${elem.contentWindow}` !== '[object Window]' ) { continue; }
|
2024-10-05 11:41:44 +00:00
|
|
|
|
if ( elem.contentWindow.location.href !== 'about:blank' ) {
|
|
|
|
|
|
if ( elem.contentWindow.location.href !== self.location.href ) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( targetProp !== '' ) {
|
2025-03-25 16:12:00 +00:00
|
|
|
|
let me = self, it = elem.contentWindow;
|
|
|
|
|
|
let chain = targetProp;
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
|
const pos = chain.indexOf('.');
|
|
|
|
|
|
if ( pos === -1 ) { break; }
|
2025-03-25 16:29:46 +00:00
|
|
|
|
const prop = chain.slice(0, pos);
|
2025-03-25 16:12:00 +00:00
|
|
|
|
me = me[prop]; it = it[prop];
|
|
|
|
|
|
chain = chain.slice(pos+1);
|
|
|
|
|
|
}
|
|
|
|
|
|
it[chain] = me[chain];
|
2024-10-04 16:24:35 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
Object.defineProperty(elem, 'contentWindow', { value: self });
|
|
|
|
|
|
}
|
|
|
|
|
|
safe.uboLog(logPrefix, 'Bypass prevented');
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2024-10-04 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return r;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-05 12:35:43 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @trustedScriptlet trusted-override-element-method
|
|
|
|
|
|
*
|
|
|
|
|
|
* @description
|
|
|
|
|
|
* Override the behavior of a method on matching elements.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param methodPath
|
|
|
|
|
|
* The method which calls must be intercepted.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param [selector]
|
|
|
|
|
|
* A CSS selector which the target element must match. If not specified,
|
|
|
|
|
|
* the override will occur for all elements.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param [disposition]
|
|
|
|
|
|
* How the override should be handled. If not specified, the overridden call
|
|
|
|
|
|
* will be equivalent to an empty function. If set to `throw`, an exception
|
|
|
|
|
|
* will be thrown. Any other value will be validated and returned as a
|
|
|
|
|
|
* supported safe constant.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ##+js(trusted-override-element-method, HTMLAnchorElement.prototype.click, a[target="_blank"][style])
|
|
|
|
|
|
*
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
|
builtinScriptlets.push({
|
|
|
|
|
|
name: 'trusted-override-element-method.js',
|
|
|
|
|
|
requiresTrust: true,
|
|
|
|
|
|
fn: trustedOverrideElementMethod,
|
|
|
|
|
|
dependencies: [
|
|
|
|
|
|
'proxy-apply.fn',
|
|
|
|
|
|
'safe-self.fn',
|
|
|
|
|
|
'validate-constant.fn',
|
|
|
|
|
|
],
|
|
|
|
|
|
});
|
|
|
|
|
|
function trustedOverrideElementMethod(
|
|
|
|
|
|
methodPath = '',
|
|
|
|
|
|
selector = '',
|
|
|
|
|
|
disposition = ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
if ( methodPath === '' ) { return; }
|
|
|
|
|
|
const safe = safeSelf();
|
|
|
|
|
|
const logPrefix = safe.makeLogPrefix('trusted-override-element-method', methodPath, selector, disposition);
|
2025-03-15 11:50:28 +00:00
|
|
|
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
2024-10-05 12:35:43 +00:00
|
|
|
|
proxyApplyFn(methodPath, function(context) {
|
|
|
|
|
|
let override = selector === '';
|
|
|
|
|
|
if ( override === false ) {
|
|
|
|
|
|
const { thisArg } = context;
|
|
|
|
|
|
try {
|
|
|
|
|
|
override = thisArg.closest(selector) === thisArg;
|
2025-01-09 14:33:22 +00:00
|
|
|
|
} catch {
|
2024-10-05 12:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( override === false ) {
|
|
|
|
|
|
return context.reflect();
|
|
|
|
|
|
}
|
|
|
|
|
|
safe.uboLog(logPrefix, 'Overridden');
|
|
|
|
|
|
if ( disposition === '' ) { return; }
|
2024-10-06 15:07:42 +00:00
|
|
|
|
if ( disposition === 'debug' && safe.logLevel !== 0 ) {
|
|
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
|
|
|
|
|
}
|
2024-10-05 12:35:43 +00:00
|
|
|
|
if ( disposition === 'throw' ) {
|
|
|
|
|
|
throw new ReferenceError();
|
|
|
|
|
|
}
|
2025-03-15 11:50:28 +00:00
|
|
|
|
return validateConstantFn(true, disposition, extraArgs);
|
2024-10-05 12:35:43 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-01 15:27:19 +00:00
|
|
|
|
/******************************************************************************/
|