1

This is a similar question to this post, although that question doesn't have a marked answer and isn't very specific.

I am simulating a click using a mouseEvent:

element.dispatchEvent(new MouseEvent("click", {...}));

and I have a listener:

element.addEventListener("click", function() {
    console.log(event);
});

Is is possible for me to simulate/spoof isTrusted=true to that listener?

Dante
  • 436
  • 6
  • 17
  • First thing I'd try would be to try to assign to the `isTrusted` property of the event and see what happens? – CertainPerformance Dec 03 '18 at 09:04
  • 1
    How much you could trust to a trust indicator if it was programmable ..? – Teemu Dec 03 '18 at 09:09
  • @CertainPerformance According to [this link](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) it is read-only, which explains why when I tried doing this the listener still showed the event as `isTrusted=false`. I am only really asking this as the comment on the other post mentioned that it is possible to simulate this to a listener. – Dante Dec 03 '18 at 09:11
  • @Teemu So are you saying that it would be impossible to simulate this to an event listener? – Dante Dec 03 '18 at 09:13
  • 1
    Something like that. You can ofcourse attach the event to an exisiting function (instead of the anonymous) and make a regular call to that function without the real or dispatched event, when you can pass what ever you want. – Teemu Dec 03 '18 at 09:18
  • @Teemu I'll give this a try. I appreciate your help! – Dante Dec 03 '18 at 09:21
  • Doesn't seem to be tweakable from my fiddling. Can't assign to non-configurable property, and I'd thought you might've been able to use `Object.create` and create a new object inheriting from `MouseEvent` with an `isTrusted` property, but that doesn't work either, `Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'.` – CertainPerformance Dec 03 '18 at 09:22

0 Answers0