Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Docs simulate keyboard

I need to simulate keyboard in google docs with using JavaScript to be able print or erase characters on cursor position.
Unfortunately solutions with simulating keypress event didn't work for me. I tried with and without jQuery.
After some investigation I detected that Google Docs have virtual keyboard. Clicks on virtual keys calls this function:

C.MOa = function(a) {
  this.dispatchEvent(new Q(Td, {keyCode: a}))
};

Where Td is a string "action" and Q some Event class.
What is the correct way to send this event with java script? Is there other ways to simulate keyboard in Google Docs?

like image 895
Sergey Kuryanov Avatar asked Dec 04 '14 09:12

Sergey Kuryanov


1 Answers

Paste the following code in console of google docs.

const input = document.querySelector(".docs-texteventtarget-iframe").contentDocument.activeElement;
    
// Insert the character in the document and trigger the save API call
const eventObj = document.createEvent("Event");
eventObj.initEvent("keypress", true, true);
eventObj.keyCode = 105;
input.dispatchEvent(eventObj);

You will see the character "i" inserting on the document.

like image 83
Prateek Jain Avatar answered Sep 24 '22 23:09

Prateek Jain