Scriptlets icon indicating copy to clipboard operation
Scriptlets copied to clipboard

Improve 'prevent-addEventListener'

Open AdamWr opened this issue 3 years ago • 3 comments

Related to this - https://github.com/AdguardTeam/AdguardFilters/issues/136716

Steps to reproduce

  1. Add this rule:
dobreprogramy.pl#%#//scriptlet("prevent-addEventListener", "whatever", "test")
  1. Go to - https://www.dobreprogramy.pl/kupowales-w-zabce-niektore-zappsy-przepadna-po-31-grudnia,6841327104457248a

Website is broken.

Screenshot

image

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

AdamWr avatar Dec 05 '22 16:12 AdamWr

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

slavaleleka avatar Dec 05 '22 17:12 slavaleleka

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/

AdamWr avatar Dec 05 '22 17:12 AdamWr

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

image


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.

AdamWr avatar Dec 09 '22 08:12 AdamWr