1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

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.
This commit is contained in:
Raymond Hill 2023-08-22 10:12:08 -04:00
parent 5cf9e5382d
commit f407c28a00
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -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);
}
/******************************************************************************/