Skip to content

Commit 1d9ee00

Browse files
committed
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.
1 parent bc8bc6a commit 1d9ee00

1 file changed

Lines changed: 55 additions & 22 deletions

File tree

assets/resources/scriptlets.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,47 @@ function runAtHtmlElement(fn) {
177177

178178
/******************************************************************************/
179179

180+
builtinScriptlets.push({
181+
name: 'get-extra-args-entries.fn',
182+
fn: getExtraArgsEntries,
183+
});
184+
function getExtraArgsEntries(args, offset) {
185+
return args.slice(offset).reduce((out, v, i, a) => {
186+
if ( (i & 1) === 0 ) {
187+
const rawValue = a[i+1];
188+
const value = /^\d+$/.test(rawValue)
189+
? parseInt(rawValue, 10)
190+
: rawValue;
191+
out.push([ a[i], value ]);
192+
}
193+
return out;
194+
}, []);
195+
}
196+
197+
builtinScriptlets.push({
198+
name: 'get-extra-args-map.fn',
199+
fn: getExtraArgsMap,
200+
dependencies: [
201+
'get-extra-args-entries.fn',
202+
],
203+
});
204+
function getExtraArgsMap(args, offset = 0) {
205+
return new Map(getExtraArgsEntries(args, offset));
206+
}
207+
208+
builtinScriptlets.push({
209+
name: 'get-extra-args.fn',
210+
fn: getExtraArgs,
211+
dependencies: [
212+
'get-extra-args-entries.fn',
213+
],
214+
});
215+
function getExtraArgs(args, offset = 0) {
216+
return Object.fromEntries(getExtraArgsEntries(args, offset));
217+
}
218+
219+
/******************************************************************************/
220+
180221
builtinScriptlets.push({
181222
name: 'abort-current-script-core.fn',
182223
fn: abortCurrentScriptCore,
@@ -714,6 +755,7 @@ builtinScriptlets.push({
714755
aliases: [ 'aeld.js' ],
715756
fn: addEventListenerDefuser,
716757
dependencies: [
758+
'get-extra-args.fn',
717759
'pattern-to-regex.fn',
718760
'run-at.fn',
719761
'safe-self.fn',
@@ -723,20 +765,15 @@ builtinScriptlets.push({
723765
});
724766
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
725767
function addEventListenerDefuser(
726-
arg1 = '',
727-
arg2 = ''
768+
type = '',
769+
pattern = ''
728770
) {
729-
const details = typeof arg1 !== 'object'
730-
? { type: arg1, pattern: arg2 }
731-
: arg1;
732-
const { type = '', pattern = '' } = details;
733-
if ( typeof type !== 'string' ) { return; }
734-
if ( typeof pattern !== 'string' ) { return; }
771+
const extraArgs = getExtraArgs(Array.from(arguments), 2);
735772
const safe = safeSelf();
736773
const reType = patternToRegex(type);
737774
const rePattern = patternToRegex(pattern);
738-
const log = shouldLog(details);
739-
const debug = shouldDebug(details);
775+
const log = shouldLog(extraArgs);
776+
const debug = shouldDebug(extraArgs);
740777
const trapEddEventListeners = ( ) => {
741778
const eventListenerHandler = {
742779
apply: function(target, thisArg, args) {
@@ -767,7 +804,7 @@ function addEventListenerDefuser(
767804
};
768805
runAt(( ) => {
769806
trapEddEventListeners();
770-
}, details.runAt);
807+
}, extraArgs.runAt);
771808
}
772809

773810
/******************************************************************************/
@@ -2317,6 +2354,7 @@ builtinScriptlets.push({
23172354
fn: sed,
23182355
world: 'ISOLATED',
23192356
dependencies: [
2357+
'get-extra-args.fn',
23202358
'pattern-to-regex.fn',
23212359
'run-at.fn',
23222360
'safe-self.fn',
@@ -2329,14 +2367,9 @@ function sed(
23292367
) {
23302368
const reNodeName = patternToRegex(nodeName, 'i');
23312369
const rePattern = patternToRegex(pattern, 'gms');
2332-
const extraArgs = new Map(
2333-
Array.from(arguments).slice(3).reduce((out, v, i, a) => {
2334-
if ( (i & 1) === 0 ) { out.push([ a[i], a[i+1] || undefined ]); }
2335-
return out;
2336-
}, [])
2337-
);
2338-
const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.get('log') || 0;
2339-
const reCondition = patternToRegex(extraArgs.get('condition') || '', 'gms');
2370+
const extraArgs = getExtraArgs(Array.from(arguments), 3);
2371+
const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.log || 0;
2372+
const reCondition = patternToRegex(extraArgs.condition || '', 'gms');
23402373
const safe = safeSelf();
23412374
const stop = (takeRecord = true) => {
23422375
if ( takeRecord ) {
@@ -2347,7 +2380,7 @@ function sed(
23472380
safe.uboLog(`sed.js: quitting "${pattern}" => "${replacement}"`);
23482381
}
23492382
};
2350-
let sedCount = extraArgs.has('sedCount') ? parseInt(extraArgs.get('sedCount')) : 0;
2383+
let sedCount = extraArgs.sedCount || 0;
23512384
const handleNode = node => {
23522385
const before = node.textContent;
23532386
if ( safe.RegExp_test.call(rePattern, before) === false ) { return true; }
@@ -2389,9 +2422,9 @@ function sed(
23892422
safe.uboLog(`sed.js ${count} nodes present before installing mutation observer`);
23902423
}
23912424
}
2392-
if ( extraArgs.has('stay') ) { return; }
2425+
if ( extraArgs.stay ) { return; }
23932426
runAt(( ) => {
2394-
const quitAfter = parseInt(extraArgs.get('quitAfter')) || 0;
2427+
const quitAfter = extraArgs.quitAfter || 0;
23952428
if ( quitAfter !== 0 ) {
23962429
setTimeout(( ) => { stop(); }, quitAfter);
23972430
} else {

0 commit comments

Comments
 (0)