Improve prevent-addEventListener scriptlet

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/3360
This commit is contained in:
Raymond Hill 2025-02-24 12:47:54 -05:00
parent 8a6b12a319
commit 9c26a07b53
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -1281,30 +1281,32 @@ function addEventListenerDefuser(
}
return matchesBoth;
};
runAt(( ) => {
proxyApplyFn('EventTarget.prototype.addEventListener', function(context) {
const { callArgs, thisArg } = context;
let t, h;
try {
t = String(callArgs[0]);
if ( typeof callArgs[1] === 'function' ) {
h = String(safe.Function_toString(callArgs[1]));
} else if ( typeof callArgs[1] === 'object' && callArgs[1] !== null ) {
if ( typeof callArgs[1].handleEvent === 'function' ) {
h = String(safe.Function_toString(callArgs[1].handleEvent));
}
} else {
h = String(callArgs[1]);
const proxyFn = function(context) {
const { callArgs, thisArg } = context;
let t, h;
try {
t = String(callArgs[0]);
if ( typeof callArgs[1] === 'function' ) {
h = String(safe.Function_toString(callArgs[1]));
} else if ( typeof callArgs[1] === 'object' && callArgs[1] !== null ) {
if ( typeof callArgs[1].handleEvent === 'function' ) {
h = String(safe.Function_toString(callArgs[1].handleEvent));
}
} catch {
} else {
h = String(callArgs[1]);
}
if ( type === '' && pattern === '' ) {
safe.uboLog(logPrefix, `Called: ${t}\n${h}\n${elementDetails(thisArg)}`);
} else if ( shouldPrevent(thisArg, t, h) ) {
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
}
return context.reflect();
});
} catch {
}
if ( type === '' && pattern === '' ) {
safe.uboLog(logPrefix, `Called: ${t}\n${h}\n${elementDetails(thisArg)}`);
} else if ( shouldPrevent(thisArg, t, h) ) {
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
}
return context.reflect();
};
runAt(( ) => {
proxyApplyFn('EventTarget.prototype.addEventListener', proxyFn);
proxyApplyFn('document.addEventListener', proxyFn);
}, extraArgs.runAt);
}