Skip to content

Commit 060f9d6

Browse files
committed
Add elements vararg to prevent-addEventListener scriptlet
If present, `elements` vararg must be a valid CSS selector, which will be used to apply the scriptlet to only elements matching the selector. Related issue: uBlockOrigin/uBlock-issues#3061 Example of usage: [...]##+js(aeld, click, return"undefined", elements, a.indirect)
1 parent a9eb963 commit 060f9d6

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

assets/resources/scriptlets.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,23 @@ function addEventListenerDefuser(
14761476
const rePattern = safe.patternToRegex(pattern);
14771477
const log = shouldLog(extraArgs);
14781478
const debug = shouldDebug(extraArgs);
1479+
const targetElements = extraArgs.elements || undefined;
1480+
const shouldPrevent = (thisArg, type, handler) => {
1481+
const matchesInstance = targetElements === undefined ||
1482+
Array.from(document.querySelectorAll(targetElements)).includes(thisArg);
1483+
if ( matchesInstance === false ) { return false; }
1484+
const matchesType = safe.RegExp_test.call(reType, type);
1485+
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
1486+
const matchesEither = matchesType || matchesHandler;
1487+
const matchesBoth = matchesType && matchesHandler;
1488+
if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
1489+
safe.uboLog(`addEventListener('${type}', ${handler})`);
1490+
}
1491+
if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
1492+
debugger; // jshint ignore:line
1493+
}
1494+
return matchesBoth;
1495+
};
14791496
const trapEddEventListeners = ( ) => {
14801497
const eventListenerHandler = {
14811498
apply: function(target, thisArg, args) {
@@ -1487,17 +1504,7 @@ function addEventListenerDefuser(
14871504
: String(args[1]);
14881505
} catch(ex) {
14891506
}
1490-
const matchesType = safe.RegExp_test.call(reType, type);
1491-
const matchesHandler = safe.RegExp_test.call(rePattern, handler);
1492-
const matchesEither = matchesType || matchesHandler;
1493-
const matchesBoth = matchesType && matchesHandler;
1494-
if ( log === 1 && matchesBoth || log === 2 && matchesEither || log === 3 ) {
1495-
safe.uboLog(`addEventListener('${type}', ${handler})`);
1496-
}
1497-
if ( debug === 1 && matchesBoth || debug === 2 && matchesEither ) {
1498-
debugger; // jshint ignore:line
1499-
}
1500-
if ( matchesBoth ) { return; }
1507+
if ( shouldPrevent(thisArg, type, handler) ) { return; }
15011508
return Reflect.apply(target, thisArg, args);
15021509
},
15031510
get(target, prop, receiver) {

0 commit comments

Comments
 (0)