Skip to content

Commit

Permalink
Add logging ability to trusted-click-element
Browse files Browse the repository at this point in the history
If the vararg `, log, 1` is present, the scriptlet will log to
the console it's execution steps. Works only in dev build.
  • Loading branch information
gorhill committed Oct 16, 2023
1 parent 5a24fad commit f122ce7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3896,6 +3896,7 @@ builtinScriptlets.push({
world: 'ISOLATED',
dependencies: [
'run-at-html-element.fn',
'safe-self.fn',
],
});
function trustedClickElement(
Expand All @@ -3905,6 +3906,12 @@ function trustedClickElement(
) {
if ( extraMatch !== '' ) { return; }

const safe = safeSelf();
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
const uboLog = extraArgs.log !== undefined
? ((...args) => { safe.uboLog(...args); })
: (( ) => { });

const selectorList = selectors.split(/\s*,\s*/)
.filter(s => {
try {
Expand All @@ -3928,15 +3935,22 @@ function trustedClickElement(
};

const next = notFound => {
if ( selectorList.length === 0 ) { return terminate(); }
if ( selectorList.length === 0 ) {
uboLog(`trusted-click-element: Completed`);
return terminate();
}
const tnow = Date.now();
if ( tnow >= tbye ) { return terminate(); }
if ( tnow >= tbye ) {
uboLog(`trusted-click-element: Timed out`);
return terminate();
}
if ( notFound ) { observe(); }
const delay = Math.max(notFound ? tbye - tnow : tnext - tnow, 1);
next.timer = setTimeout(( ) => {
next.timer = undefined;
process();
}, delay);
uboLog(`trusted-click-element: Waiting for ${selectorList[0]}...`);
};
next.stop = ( ) => {
if ( next.timer === undefined ) { return; }
Expand Down Expand Up @@ -3980,6 +3994,7 @@ function trustedClickElement(
selectorList.unshift(selector);
return next(true);
}
uboLog(`trusted-click-element: Clicked ${selector}`);
elem.click();
tnext += clickDelay;
next();
Expand Down

0 comments on commit f122ce7

Please sign in to comment.