Skip to content

Commit

Permalink
Add ability to execute aeld scriptlet at a later time
Browse files Browse the repository at this point in the history
As per discussion with filter list maintainers.

THis requires to use JSON notation for parameter passing:

- "runAt":  "end" = execute scriptlet at `DOMContentLoaded` event
- "runAt": "idle" = execute scriptlet at `load` event
  • Loading branch information
gorhill committed Apr 27, 2023
1 parent 7cba521 commit 3c12173
Showing 1 changed file with 73 additions and 37 deletions.
110 changes: 73 additions & 37 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ function shouldLog(details) {
return scriptletGlobals.has('canDebug') && details.log;
}

/******************************************************************************/

builtinScriptlets.push({
name: 'run-at.fn',
fn: runAt,
});
function runAt(fn, when) {
const intFromReadyState = state => {
return ({
loading: 1,
interactive: 2,
end: 2,
complete: 3,
idle: 3,
})[`${state}`] || 0;
};
const runAt = intFromReadyState(when);
if ( intFromReadyState(document.readyState) >= runAt ) {
fn(); return;
}
const options = { capture: true };
const onStateChange = ( ) => {
if ( intFromReadyState(document.readyState) < runAt ) { return; }
fn();
document.removeEventListener('readystatechange', onStateChange, options);
};
document.addEventListener('readystatechange', onStateChange, options);
}

/*******************************************************************************
Injectable scriptlets
Expand Down Expand Up @@ -432,6 +461,7 @@ builtinScriptlets.push({
fn: addEventListenerDefuser,
dependencies: [
'pattern-to-regex.fn',
'run-at.fn',
'safe-self.fn',
'should-debug.fn',
'should-log.fn',
Expand All @@ -453,29 +483,45 @@ function addEventListenerDefuser(
const rePattern = patternToRegex(pattern);
const log = shouldLog(details);
const debug = shouldDebug(details);
const proto = self.EventTarget.prototype;
proto.addEventListener = new Proxy(proto.addEventListener, {
apply: function(target, thisArg, args) {
let type, handler;
try {
type = String(args[0]);
handler = String(args[1]);
} catch(ex) {
}
const matchesType = safe.RegExp_test.call(reType, type);
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
const matchesEither = matchesType || matchesHandler;
const matchesBoth = matchesType && matchesHandler;
if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
safe.uboLog(`addEventListener('${type}', ${handler})`);
}
if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
debugger; // jshint ignore:line
const trapEddEventListeners = ( ) => {
const eventListenerHandler = {
apply: function(target, thisArg, args) {
let type, handler;
try {
type = String(args[0]);
handler = String(args[1]);
} catch(ex) {
}
const matchesType = safe.RegExp_test.call(reType, type);
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
const matchesEither = matchesType || matchesHandler;
const matchesBoth = matchesType && matchesHandler;
if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
safe.uboLog(`addEventListener('${type}', ${handler})`);
}
if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
debugger; // jshint ignore:line
}
if ( matchesBoth ) { return; }
return Reflect.apply(target, thisArg, args);
}
if ( matchesBoth ) { return; }
return Reflect.apply(target, thisArg, args);
}
});
};
self.EventTarget.prototype.addEventListener = new Proxy(
self.EventTarget.prototype.addEventListener,
eventListenerHandler
);
self.document.addEventListener = new Proxy(
self.document.addEventListener,
eventListenerHandler
);
self.addEventListener = new Proxy(
self.addEventListener,
eventListenerHandler
);
};
runAt(( ) => {
trapEddEventListeners();
}, details.runAt);
}

/******************************************************************************/
Expand Down Expand Up @@ -979,6 +1025,9 @@ builtinScriptlets.push({
name: 'set-constant.js',
aliases: [ 'set.js' ],
fn: setConstant,
dependencies: [
'run-at.fn',
],
});
function setConstant(
arg1 = '',
Expand Down Expand Up @@ -1144,22 +1193,9 @@ function setConstant(
};
trapChain(window, chain);
}
const runAt = details.runAt;
if ( runAt === 0 ) {
setConstant(chain, cValue); return;
}
const docReadyState = ( ) => {
return ({ loading: 1, interactive: 2, complete: 3, })[document.readyState] || 0;
};
if ( docReadyState() >= runAt ) {
setConstant(chain, cValue); return;
}
const onReadyStateChange = ( ) => {
if ( docReadyState() < runAt ) { return; }
runAt(( ) => {
setConstant(chain, cValue);
document.removeEventListener('readystatechange', onReadyStateChange);
};
document.addEventListener('readystatechange', onReadyStateChange);
}, details.runAt);
}

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

0 comments on commit 3c12173

Please sign in to comment.