Skip to content

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

Description

@AdamWr

If website overrides Function.prototype.toString then prevent-addEventListener might not works.
Related to - AdguardTeam/AdguardFilters#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

export const listenerToString = (listener) => {
return typeof listener === 'function'
? listener.toString()
: listener.handleEvent.toString();
};

Something like:

const nativeToString = Function.prototype.toString;
nativeToString.call(listener);

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions