Scriptlets icon indicating copy to clipboard operation
Scriptlets copied to clipboard

Improve 'prevent-addEventListener' — save reference to 'toString()' method

Open AdamWr opened this issue 2 years ago • 0 comments

If website overrides Function.prototype.toString then prevent-addEventListener might not works. Related to - https://github.com/AdguardTeam/AdguardFilters/issues/143847


Steps to reproduce:

  1. Add this rule:
example.org#%#//scriptlet('prevent-addEventListener', '', 'alert')
  1. Go to - https://example.org/
  2. 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);
});
  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);

AdamWr avatar Feb 25 '23 11:02 AdamWr