Scriptlets
Scriptlets copied to clipboard
Improve 'prevent-addEventListener' — save reference to 'toString()' method
If website overrides Function.prototype.toString then prevent-addEventListener might not works.
Related to - https://github.com/AdguardTeam/AdguardFilters/issues/143847
Steps to reproduce:
- Add this rule:
example.org#%#//scriptlet('prevent-addEventListener', '', 'alert')
- Go to - https://example.org/
- In browser console run:
const nativeToString = Function.prototype.toString;
Function.prototype.toString = function (arg) {
return nativeToString.call(this).includes('alert')
? ""
: nativeToString.call(this);
};
document.documentElement.addEventListener('click', () => {
alert(1);
});
- Click somewhere
Alert should be prevented by prevent-addEventListener rule, but it doesn't work.
We probably could just save reference to Function.prototype.toString and use it in listenerToString
https://github.com/AdguardTeam/Scriptlets/blob/b1e0bdaa8bae1ccfa3b2b79a2be82f75175fff3d/src/helpers/add-event-listener-utils.js#L39-L43
Something like:
const nativeToString = Function.prototype.toString;
nativeToString.call(listener);