Skip to content

Commit

Permalink
Improve prevent-addEventListener scriptlet
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 24, 2025
1 parent 8a6b12a commit 9c26a07
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/js/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
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)}`);
} else {
h = String(callArgs[1]);
}
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);
}

Expand Down

0 comments on commit 9c26a07

Please sign in to comment.