mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +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:
parent
5cf9e5382d
commit
f407c28a00
@ -348,25 +348,12 @@ builtinScriptlets.push({
|
|||||||
|
|
||||||
function setConstantCore(
|
function setConstantCore(
|
||||||
trusted = false,
|
trusted = false,
|
||||||
arg1 = '',
|
chain = '',
|
||||||
arg2 = '',
|
cValue = ''
|
||||||
arg3 = ''
|
|
||||||
) {
|
) {
|
||||||
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; }
|
if ( chain === '' ) { return; }
|
||||||
const options = details.options || [];
|
|
||||||
const safe = safeSelf();
|
const safe = safeSelf();
|
||||||
|
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
|
||||||
function setConstant(chain, cValue) {
|
function setConstant(chain, cValue) {
|
||||||
const trappedProp = (( ) => {
|
const trappedProp = (( ) => {
|
||||||
const pos = chain.lastIndexOf('.');
|
const pos = chain.lastIndexOf('.');
|
||||||
@ -432,14 +419,16 @@ function setConstantCore(
|
|||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( options.includes('asFunction') ) {
|
if ( extraArgs.as !== undefined ) {
|
||||||
cValue = ( ) => cValue;
|
if ( extraArgs.as === 'function' ) {
|
||||||
} else if ( options.includes('asCallback') ) {
|
cValue = ( ) => cValue;
|
||||||
cValue = ( ) => (( ) => cValue);
|
} else if ( extraArgs.as === 'callback' ) {
|
||||||
} else if ( options.includes('asResolved') ) {
|
cValue = ( ) => (( ) => cValue);
|
||||||
cValue = Promise.resolve(cValue);
|
} else if ( extraArgs.as === 'resolved' ) {
|
||||||
} else if ( options.includes('asRejected') ) {
|
cValue = Promise.resolve(cValue);
|
||||||
cValue = Promise.reject(cValue);
|
} else if ( extraArgs.as === 'rejected' ) {
|
||||||
|
cValue = Promise.reject(cValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let aborted = false;
|
let aborted = false;
|
||||||
const mustAbort = function(v) {
|
const mustAbort = function(v) {
|
||||||
@ -535,7 +524,7 @@ function setConstantCore(
|
|||||||
}
|
}
|
||||||
runAt(( ) => {
|
runAt(( ) => {
|
||||||
setConstant(chain, cValue);
|
setConstant(chain, cValue);
|
||||||
}, options);
|
}, extraArgs.runAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
Loading…
Reference in New Issue
Block a user