It turns out that there are drawbacks to using evemu-play to emulate
key presses (it's racy).
Going back to the element.dispatchEvent approach, I am now wondering
if and how it can be made to work. I was planning to use
chrome.tabs.ExecuteScript, but according to the docs here:
https://developer.chrome.com/extensions/content_scripts#execution-environment
the API available to content scripts seems quite limited.
More concretely, the injection would be performed by a promise created as shown:
new Promise((resolve, reject) => {
chrome.tabs.executeScript(1732467094, {code: "
{
var kbEvent = document.createEvent('KeyboardEvent');
var initKind = typeof kbEvent.initKeyboardEvent !== 'undefined' ?
'initKeyboardEvent' : 'initKeyEvent';
kbEvent[initKind]('keydown',
true,
true,
window,
false,
false,
false,
false,
42, // keycode
0);
document.dispatchEvent(keyboardEvent);
}"}, () => { resolve() }) });
Am I delusional?