Search code examples
javascriptgoogle-chromegoogle-chrome-extensioncontent-scriptdom-events

How to to initialize keyboard event with given char/keycode in a Chrome extension?


I'm developing a Google Chrome extension which simulates keyboard events on a web-page.

I found that event.initKeyboardEvent() does not work properly because of this webkit bug and I also found some workarounds, e.g. SO Question

However, defining properties on event object is not working because extension's content script has its own "parallel world" so properties defined in content script are not visible to web-page script.

My only and last hope that DOM 4 Event Constructors work in Google Chrome and it would be possible to properly initialize keyboard event through constructor

var event = new KeyboardEvent("keypress", {key: 'U+0041', char: 'a', ... })

Unfortunately, it fails with:

TypeError: illegal constructor  

I was not able to find any documentation on supported event constructors in Chrome. Could anyone point me to some docs/source code?

Any other way to simulate keyboard events in a Google Chrome extension?

(note, TextEvent won't help because many real-world controls listen to keydown/keyup specifically)


Solution

  • I found that chrome debugger protocol v1.1 is the definite answer to simulating key and mouse events from a Google Chrome extension. Part of the protocol is accessible through chrome.debugger API.