1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Simplify passing extra parameters in scriptlets

When scriptlets can receive extra optional paramaters, these will
now be passed as pair of extra paramaters in the filter declaration,
whereas each pair is a `name, value` instance.

As a result, the optional paramaters that can be passed to the
`aeld` scriptlet can be passed this way, i.e. no longer need
a JSON approach, example:

    github.com##+js(aeld, click, , log, 1)
    github.com##+js(aeld, , , runAt, idle, log, 1)

The non-optional paramaters are always positional, after which
the optional paramaters are non-positional pairs of values.
This commit is contained in:
Raymond Hill 2023-05-24 11:59:17 -04:00
parent bc8bc6a4e6
commit 1d9ee00498
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -177,6 +177,47 @@ function runAtHtmlElement(fn) {
/******************************************************************************/ /******************************************************************************/
builtinScriptlets.push({
name: 'get-extra-args-entries.fn',
fn: getExtraArgsEntries,
});
function getExtraArgsEntries(args, offset) {
return args.slice(offset).reduce((out, v, i, a) => {
if ( (i & 1) === 0 ) {
const rawValue = a[i+1];
const value = /^\d+$/.test(rawValue)
? parseInt(rawValue, 10)
: rawValue;
out.push([ a[i], value ]);
}
return out;
}, []);
}
builtinScriptlets.push({
name: 'get-extra-args-map.fn',
fn: getExtraArgsMap,
dependencies: [
'get-extra-args-entries.fn',
],
});
function getExtraArgsMap(args, offset = 0) {
return new Map(getExtraArgsEntries(args, offset));
}
builtinScriptlets.push({
name: 'get-extra-args.fn',
fn: getExtraArgs,
dependencies: [
'get-extra-args-entries.fn',
],
});
function getExtraArgs(args, offset = 0) {
return Object.fromEntries(getExtraArgsEntries(args, offset));
}
/******************************************************************************/
builtinScriptlets.push({ builtinScriptlets.push({
name: 'abort-current-script-core.fn', name: 'abort-current-script-core.fn',
fn: abortCurrentScriptCore, fn: abortCurrentScriptCore,
@ -714,6 +755,7 @@ builtinScriptlets.push({
aliases: [ 'aeld.js' ], aliases: [ 'aeld.js' ],
fn: addEventListenerDefuser, fn: addEventListenerDefuser,
dependencies: [ dependencies: [
'get-extra-args.fn',
'pattern-to-regex.fn', 'pattern-to-regex.fn',
'run-at.fn', 'run-at.fn',
'safe-self.fn', 'safe-self.fn',
@ -723,20 +765,15 @@ builtinScriptlets.push({
}); });
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120 // https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
function addEventListenerDefuser( function addEventListenerDefuser(
arg1 = '', type = '',
arg2 = '' pattern = ''
) { ) {
const details = typeof arg1 !== 'object' const extraArgs = getExtraArgs(Array.from(arguments), 2);
? { type: arg1, pattern: arg2 }
: arg1;
const { type = '', pattern = '' } = details;
if ( typeof type !== 'string' ) { return; }
if ( typeof pattern !== 'string' ) { return; }
const safe = safeSelf(); const safe = safeSelf();
const reType = patternToRegex(type); const reType = patternToRegex(type);
const rePattern = patternToRegex(pattern); const rePattern = patternToRegex(pattern);
const log = shouldLog(details); const log = shouldLog(extraArgs);
const debug = shouldDebug(details); const debug = shouldDebug(extraArgs);
const trapEddEventListeners = ( ) => { const trapEddEventListeners = ( ) => {
const eventListenerHandler = { const eventListenerHandler = {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
@ -767,7 +804,7 @@ function addEventListenerDefuser(
}; };
runAt(( ) => { runAt(( ) => {
trapEddEventListeners(); trapEddEventListeners();
}, details.runAt); }, extraArgs.runAt);
} }
/******************************************************************************/ /******************************************************************************/
@ -2317,6 +2354,7 @@ builtinScriptlets.push({
fn: sed, fn: sed,
world: 'ISOLATED', world: 'ISOLATED',
dependencies: [ dependencies: [
'get-extra-args.fn',
'pattern-to-regex.fn', 'pattern-to-regex.fn',
'run-at.fn', 'run-at.fn',
'safe-self.fn', 'safe-self.fn',
@ -2329,14 +2367,9 @@ function sed(
) { ) {
const reNodeName = patternToRegex(nodeName, 'i'); const reNodeName = patternToRegex(nodeName, 'i');
const rePattern = patternToRegex(pattern, 'gms'); const rePattern = patternToRegex(pattern, 'gms');
const extraArgs = new Map( const extraArgs = getExtraArgs(Array.from(arguments), 3);
Array.from(arguments).slice(3).reduce((out, v, i, a) => { const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.log || 0;
if ( (i & 1) === 0 ) { out.push([ a[i], a[i+1] || undefined ]); } const reCondition = patternToRegex(extraArgs.condition || '', 'gms');
return out;
}, [])
);
const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.get('log') || 0;
const reCondition = patternToRegex(extraArgs.get('condition') || '', 'gms');
const safe = safeSelf(); const safe = safeSelf();
const stop = (takeRecord = true) => { const stop = (takeRecord = true) => {
if ( takeRecord ) { if ( takeRecord ) {
@ -2347,7 +2380,7 @@ function sed(
safe.uboLog(`sed.js: quitting "${pattern}" => "${replacement}"`); safe.uboLog(`sed.js: quitting "${pattern}" => "${replacement}"`);
} }
}; };
let sedCount = extraArgs.has('sedCount') ? parseInt(extraArgs.get('sedCount')) : 0; let sedCount = extraArgs.sedCount || 0;
const handleNode = node => { const handleNode = node => {
const before = node.textContent; const before = node.textContent;
if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; } if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; }
@ -2389,9 +2422,9 @@ function sed(
safe.uboLog(`sed.js ${count} nodes present before installing mutation observer`); safe.uboLog(`sed.js ${count} nodes present before installing mutation observer`);
} }
} }
if ( extraArgs.has('stay') ) { return; } if ( extraArgs.stay ) { return; }
runAt(( ) => { runAt(( ) => {
const quitAfter = parseInt(extraArgs.get('quitAfter')) || 0; const quitAfter = extraArgs.quitAfter || 0;
if ( quitAfter !== 0 ) { if ( quitAfter !== 0 ) {
setTimeout(( ) => { stop(); }, quitAfter); setTimeout(( ) => { stop(); }, quitAfter);
} else { } else {