Skip to content

Commit

Permalink
Re-factor extra args for set-constant scriptlet
Browse files Browse the repository at this point in the history
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:
uBlockOrigin/uBlock-issues#2783

Ideally, AdGuard would support its `stack` parameter as a
vararg, to be discussed.
  • Loading branch information
gorhill committed Aug 22, 2023
1 parent 5cf9e53 commit f407c28
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('.');
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -535,7 +524,7 @@ function setConstantCore(
}
runAt(( ) => {
setConstant(chain, cValue);
}, options);
}, extraArgs.runAt);
}

/******************************************************************************/
Expand Down

0 comments on commit f407c28

Please sign in to comment.