Remove addEventListenerLogger, expand addEventListenerDefuser

The scriptlet addEventListenerLogger has been removed.

The logging of addEventListener() calls can now be done with the
addEventListenerDefuser scriptlet, which now supports the
following named arguments:

"type": the event type to match. Default to '', i.e. match-all.

"pattern": the pattern to match against the handler argument
Default to '', i.e. match-all.

"log": an integer value telling when to log:

- 1: log only when both type and pattern matches, i.e. when a
     call to addEventListener() is defused
- 2: log when either the type or pattern matches
- 3: log all calls to addEventListener()

"debug": an integer value telling when to break into the
 debugger, useful to inspect the debugger's call stack.

- 1: break into the debugger when both type and pattern match,
     so effectively when defusing is taking place.
- 2: break into the debugger when either type or pattern matches.

The usage of named arguments is optional, positional arguments
are still supported as documented. Named arguments is required
to use "log" and/or "debug" arguments.

Obviously, do not use "log" or "debug" in any filter list, these
are investigative tools for filter list authors.

Examples of usage using named arguments:

  wikipedia.org##+js(aeld, { "type": "/mouse/", "pattern": "/.^/", "log": 2 })

Above filter will log calls to addEventListener() which have the
pattern "mouse" in the event type (so "mouseover", "mouseout",
etc.) without defusing any of them (because pattern can't match
anything).

  wikipedia.org##+js(aeld, { "type": "/.^/", "log": 2 })

Above filter will log all calls without defusing any of them
(because type can't match anything)

  wikipedia.org##+js(aeld, { "log": 1 })

Above filter will log and defuse all calls to addEventListener().
This commit is contained in:
Raymond Hill 2023-03-25 12:35:56 -04:00
parent a51130baed
commit 439951824a
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 48 additions and 58 deletions

View File

@ -365,29 +365,34 @@ builtinScriptlets.push({
}); });
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120 // https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
function addEventListenerDefuser( function addEventListenerDefuser(
needle1 = '', arg1 = '',
needle2 = '' arg2 = ''
) { ) {
if ( typeof needle1 !== 'string' ) { return; } const details = typeof arg1 !== 'object'
if ( needle1 === '' ) { ? { type: arg1, pattern: arg2 }
needle1 = '.?'; : arg1;
} else if ( /^\/.+\/$/.test(needle1) ) { let { type = '', pattern = '' } = details;
needle1 = needle1.slice(1,-1); if ( typeof type !== 'string' ) { return; }
if ( typeof pattern !== 'string' ) { return; }
if ( type === '' ) {
type = '^';
} else if ( /^\/.+\/$/.test(type) ) {
type = type.slice(1,-1);
} else { } else {
needle1 = needle1.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); type = type.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
} }
needle1 = new RegExp(needle1); const reType = new RegExp(type);
if ( needle2 === '' ) { if ( pattern === '' ) {
needle2 = '.?'; pattern = '^';
} else if ( /^\/.+\/$/.test(needle2) ) { } else if ( /^\/.+\/$/.test(pattern) ) {
needle2 = needle2.slice(1,-1); pattern = pattern.slice(1,-1);
} else { } else {
needle2 = needle2.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); pattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
} }
needle2 = new RegExp(needle2); const rePattern = new RegExp(pattern);
self.EventTarget.prototype.addEventListener = new Proxy( const logfn = console.log.bind(console);
self.EventTarget.prototype.addEventListener, const proto = self.EventTarget.prototype;
{ proto.addEventListener = new Proxy(proto.addEventListener, {
apply: function(target, thisArg, args) { apply: function(target, thisArg, args) {
let type, handler; let type, handler;
try { try {
@ -395,42 +400,27 @@ function addEventListenerDefuser(
handler = String(args[1]); handler = String(args[1]);
} catch(ex) { } catch(ex) {
} }
const matchesType = reType.test(type);
const matchesHandler = rePattern.test(handler);
const matchesEither = matchesType || matchesHandler;
const matchesBoth = matchesType && matchesHandler;
if (
details.log === 1 && matchesBoth ||
details.log === 2 && matchesEither ||
details.log === 3
) {
logfn(`uBO: addEventListener('${type}', ${handler})`);
}
if ( if (
needle1.test(type) === false || details.debug === 1 && matchesBoth ||
needle2.test(handler) === false details.debug === 2 && matchesEither
) { ) {
return target.apply(thisArg, args); debugger; // jshint ignore:line
} }
if ( matchesBoth ) { return; }
return Reflect.apply(target, thisArg, args);
} }
}
);
}
/// addEventListener-logger.js
builtinScriptlets.push({
name: 'addEventListener-logger.js',
aliases: [ 'aell.js' ],
fn: addEventListenerLogger,
}); });
// https://github.com/uBlockOrigin/uAssets/issues/9123#issuecomment-848255120
function addEventListenerLogger() {
const log = console.log.bind(console);
self.EventTarget.prototype.addEventListener = new Proxy(
self.EventTarget.prototype.addEventListener,
{
apply: function(target, thisArg, args) {
let type, handler;
try {
type = String(args[0]);
handler = String(args[1]);
} catch(ex) {
}
log('uBO: addEventListener("%s", %s)', type, handler);
return target.apply(thisArg, args);
}
}
);
} }