Javascript Simulating Input in Google Docs "Find In Document" Prompt

20 views Asked by At

I am attempting to programatically search a Google Doc using Javascript. I am able to open the "Find in Document" prompt by sending a ctrl+f key event to the .docs-texteventtarget-iframe iframe.

const keyEvent = document.createEvent('Event');
keyEvent.initEvent("keydown", true, true);
keyEvent.key = "f";
keyEvent.keyCode = 70;
keyEvent.ctrlKey=true;
document.querySelector('.docs-texteventtarget-iframe').contentDocument.activeElement.dispatchEvent(keyEvent);

However, I'm having trouble actually simulating key events to the find input box.The above approach does not work.

Directly setting the value of the .docs-findinput-input does not work. It fills the input box but does not actually trigger the search.

document.getElementsByClassName("docs-findinput-input")[0].value="Test";

I have also tried sending key events to the input but nothing happens.

const keyEvent = document.createEvent('Event');
keyEvent.initEvent("keydown", true, true);
keyEvent.key = "f";
keyEvent.keyCode = 70;
document.getElementsByClassName("docs-findinput-input")[0].dispatchEvent(keyEvent);
0

There are 0 answers