V
V
Vasya Lee2020-09-10 19:48:13
JavaScript
Vasya Lee, 2020-09-10 19:48:13

How to bypass the isTrusted:false property in the key event object?

Hello. How can I do a complete simulation of text input using only events without directly changing the value of el.value ?
I'm trying to run

el.dispatchEvent(new KeyboardEvent('keydown', {'key':'h'}));

unsuccessfully. I also thought maybe you need to run the entire keyboard event loop and tried the following code:
el.dispatchEvent(new KeyboardEvent('keydown', {'key':'h'}));
  el.dispatchEvent(new KeyboardEvent('keypress', {'key':'h'}));
  el.dispatchEvent(new InputEvent('beforeinput', {data:'k',inputType: "insertText", bubbles: true, cancelable: true, composed: true, isTrusted: true}));
  el.dispatchEvent(new KeyboardEvent('keyup', {'key':'h'}));

which was also unsuccessful. I know that the cycle is not complete, but I did not write further when I came across the isTrusted property. It turns out that because of it, the event occurs, but the value of the text field does not change. I read everywhere, tried libraries, but all efforts were in vain.
It is possible to give the debugger a command so that when a keyboard click event occurs, it will add this event to the trusted ones and set it to isTrusted:true , but I can’t figure out how. The official documentation is in English (I don't know it) and describes some rules for extensions.
On one forum I saw that it is done as follows
chrome.debugger.attach(target, "1.2", function() {
    chrome.debugger.sendCommand(target, "Input.dispatchMouseEvent", arguments)
})

I tried to do the same for myself, but
1 it’s not clear what to pass in the arguments (the target is the current window, this is understandable, then the version is, and everything else is a mystery)
2 I don’t even have the chrome.debugger property.
In case anyone is confused, the goal is to simulate keyboard input and populate the value of the input without changing el.value directly. It fails due to the isTrusted:false property.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question