Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active December 5, 2022 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatiasq/1ceab24121f5bf4c5a44797868d11430 to your computer and use it in GitHub Desktop.
Save amatiasq/1ceab24121f5bf4c5a44797868d11430 to your computer and use it in GitHub Desktop.
button.click(); // nothing
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
button.dispatchEvent(click) // nothing
// here I discover `event.isTrusted`
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
event.isTrusted = true;
console.log(event.isTrusted);
// false
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
Object.defineProperty(event, 'isTrusted', { value: true });
// Cannot redefine property: isTrusted
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
fake = Object.create(event, { isTrusted: { value: true }});
button.dispatchEvent(fake);
// Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
fake = Object.create(
Object.getPrototypeOf(event),
{ isTrusted: { value: true }}
);
button.dispatchEvent(fake);
// Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
event = document.createEvent ('MouseEvents');
event.initEvent('click', true, true);
fake = new Proxy(event, {
get(target, prop, receiver) {
return prop === 'isTrusted' ? true : Reflect.get(...arguments);
}
});
button.dispatchEvent(fake);
// Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.
@ll4mp
Copy link

ll4mp commented Apr 20, 2022

Did you get it?

@amatiasq
Copy link
Author

No :\

@muodov
Copy link

muodov commented Dec 5, 2022

afaik, you can't fake isTrusted on real Event objects. If you're trying to trick 3rd-party event listeners checking for this, you may try to wrap the listener functions themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment