From f407c28a00338032ff08a03d9f19a2487af814ff Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 22 Aug 2023 10:12:08 -0400 Subject: [PATCH] Re-factor extra args for `set-constant` scriptlet To prepare for better compatibility with AdGuard's own `set-constant` scriptlet. The 3rd position parameter which dictates how to set the value has been converted into a vararg paramater, as follow: ..., as, function ..., as, callback ..., as, resolved ..., as, rejected Similarly, the parameter used to dictate when the scriptlet should become effective is now to be used as a vararg: ..., runAt, load Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/2783 Ideally, AdGuard would support its `stack` parameter as a vararg, to be discussed. --- assets/resources/scriptlets.js | 39 ++++++++++++---------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index 7acf86e19..3c9bc6d87 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -348,25 +348,12 @@ builtinScriptlets.push({ function setConstantCore( trusted = false, - arg1 = '', - arg2 = '', - arg3 = '' + chain = '', + cValue = '' ) { - const details = typeof arg1 !== 'object' - ? { prop: arg1, value: arg2 } - : arg1; - if ( arg3 !== '' ) { - if ( /^\d$/.test(arg3) ) { - details.options = [ arg3 ]; - } else { - details.options = Array.from(arguments).slice(3); - } - } - const { prop: chain = '', value: cValue = '' } = details; - if ( typeof chain !== 'string' ) { return; } if ( chain === '' ) { return; } - const options = details.options || []; const safe = safeSelf(); + const extraArgs = safe.getExtraArgs(Array.from(arguments), 3); function setConstant(chain, cValue) { const trappedProp = (( ) => { const pos = chain.lastIndexOf('.'); @@ -432,14 +419,16 @@ function setConstantCore( } else { return; } - if ( options.includes('asFunction') ) { - cValue = ( ) => cValue; - } else if ( options.includes('asCallback') ) { - cValue = ( ) => (( ) => cValue); - } else if ( options.includes('asResolved') ) { - cValue = Promise.resolve(cValue); - } else if ( options.includes('asRejected') ) { - cValue = Promise.reject(cValue); + if ( extraArgs.as !== undefined ) { + if ( extraArgs.as === 'function' ) { + cValue = ( ) => cValue; + } else if ( extraArgs.as === 'callback' ) { + cValue = ( ) => (( ) => cValue); + } else if ( extraArgs.as === 'resolved' ) { + cValue = Promise.resolve(cValue); + } else if ( extraArgs.as === 'rejected' ) { + cValue = Promise.reject(cValue); + } } let aborted = false; const mustAbort = function(v) { @@ -535,7 +524,7 @@ function setConstantCore( } runAt(( ) => { setConstant(chain, cValue); - }, options); + }, extraArgs.runAt); } /******************************************************************************/