Skip to content

Commit 9c26a07

Browse files
committed
Improve prevent-addEventListener scriptlet
Related issue: uBlockOrigin/uBlock-issues#3360
1 parent 8a6b12a commit 9c26a07

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/js/resources/scriptlets.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,30 +1281,32 @@ function addEventListenerDefuser(
12811281
}
12821282
return matchesBoth;
12831283
};
1284-
runAt(( ) => {
1285-
proxyApplyFn('EventTarget.prototype.addEventListener', function(context) {
1286-
const { callArgs, thisArg } = context;
1287-
let t, h;
1288-
try {
1289-
t = String(callArgs[0]);
1290-
if ( typeof callArgs[1] === 'function' ) {
1291-
h = String(safe.Function_toString(callArgs[1]));
1292-
} else if ( typeof callArgs[1] === 'object' && callArgs[1] !== null ) {
1293-
if ( typeof callArgs[1].handleEvent === 'function' ) {
1294-
h = String(safe.Function_toString(callArgs[1].handleEvent));
1295-
}
1296-
} else {
1297-
h = String(callArgs[1]);
1284+
const proxyFn = function(context) {
1285+
const { callArgs, thisArg } = context;
1286+
let t, h;
1287+
try {
1288+
t = String(callArgs[0]);
1289+
if ( typeof callArgs[1] === 'function' ) {
1290+
h = String(safe.Function_toString(callArgs[1]));
1291+
} else if ( typeof callArgs[1] === 'object' && callArgs[1] !== null ) {
1292+
if ( typeof callArgs[1].handleEvent === 'function' ) {
1293+
h = String(safe.Function_toString(callArgs[1].handleEvent));
12981294
}
1299-
} catch {
1300-
}
1301-
if ( type === '' && pattern === '' ) {
1302-
safe.uboLog(logPrefix, `Called: ${t}\n${h}\n${elementDetails(thisArg)}`);
1303-
} else if ( shouldPrevent(thisArg, t, h) ) {
1304-
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
1295+
} else {
1296+
h = String(callArgs[1]);
13051297
}
1306-
return context.reflect();
1307-
});
1298+
} catch {
1299+
}
1300+
if ( type === '' && pattern === '' ) {
1301+
safe.uboLog(logPrefix, `Called: ${t}\n${h}\n${elementDetails(thisArg)}`);
1302+
} else if ( shouldPrevent(thisArg, t, h) ) {
1303+
return safe.uboLog(logPrefix, `Prevented: ${t}\n${h}\n${elementDetails(thisArg)}`);
1304+
}
1305+
return context.reflect();
1306+
};
1307+
runAt(( ) => {
1308+
proxyApplyFn('EventTarget.prototype.addEventListener', proxyFn);
1309+
proxyApplyFn('document.addEventListener', proxyFn);
13081310
}, extraArgs.runAt);
13091311
}
13101312

0 commit comments

Comments
 (0)