Improve 'prevent-addEventListener'
Related to this - https://github.com/AdguardTeam/AdguardFilters/issues/136716
Steps to reproduce
- Add this rule:
dobreprogramy.pl#%#//scriptlet("prevent-addEventListener", "whatever", "test")
Website is broken.
Screenshot

As far as I understand, website is checking Object.getOwnPropertyDescriptor(EventTarget.prototype, 'addEventListener').set.toString() if it includes specific content and if so, then it removes addEventListener.
Example:
if (Object.getOwnPropertyDescriptor(EventTarget.prototype, 'addEventListener').set?.toString().includes('function(){}')) {
delete EventTarget.prototype.addEventListener;
}
If I'm not wrong, as a simple workaround, something like this:
descriptor.set.toString = function () { return undefined; };
could be added here: https://github.com/AdguardTeam/Scriptlets/blob/202f6d855bdb576218b9fc9cabf4a54c82646360/src/scriptlets/prevent-addEventListener.js#L69-L73
mocking toString() is too much for making the prevent-addEventListener work for such specific case. let's keep the issue opened for collecting similar cases
The problem is that this issue probably occurs on all websites from WP group, which is the most popular website in Poland - https://www.similarweb.com/website/wp.pl/#overview
List of websites on which this issue occurs:
https://www.wp.pl/
https://allani.pl/
https://www.homebook.pl/
https://www.extradom.pl/
https://www.wakacje.pl/
https://www.nocowanie.pl/
https://domodi.pl/
https://www.money.pl/
https://www.totalmoney.pl/
https://www.dobreprogramy.pl/
Few subdomains of wp.pl:
https://wiadomosci.wp.pl/
https://tech.wp.pl/
https://kuchnia.wp.pl/
https://gwiazdy.wp.pl/
https://sportowefakty.wp.pl/
It seems that there is another issue related to prevent-addEventListener - https://github.com/AdguardTeam/AdguardFilters/issues/136633#issuecomment-1339564348
It looks like that HandyImage userscript doesn't work correctly - https://github.com/Owyn/HandyImage/raw/master/HandyImage.user.js
Screenshot

As far as I understand, the problem is with this in return nativeAddEventListener.apply(this, [type, listener, ...args]);
Simple workaround would be to use try...catch here:
https://github.com/AdguardTeam/Scriptlets/blob/202f6d855bdb576218b9fc9cabf4a54c82646360/src/scriptlets/prevent-addEventListener.js#L66
and use window instead of this in catch.
Something like:
try {
return nativeAddEventListener.apply(this, [type, listener, ...args]);
} catch (ex) {
return nativeAddEventListener.apply(window, [type, listener, ...args]);
}
but I don't know if it's a good idea.
Or maybe just don't override EventTarget.prototype.addEventListener if it's come from userscripts.