如何在 Chrome 扩展程序中使用给定的字符/键码初始化键盘事件? [英] How to to initialize keyboard event with given char/keycode in a Chrome extension?
问题描述
我正在开发一个 Google Chrome 扩展程序来模拟网页上的键盘事件.
I'm developing a Google Chrome extension which simulates keyboard events on a web-page.
我发现 event.initKeyboardEvent()
无法正常工作,因为 这个 webkit 错误,我还找到了一些解决方法,例如SO 问题
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.
我唯一和最后的希望是 DOM 4 事件构造函数在谷歌浏览器中工作,并且可以通过构造函数正确初始化键盘事件
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', ... })
不幸的是,它失败了:
TypeError: illegal constructor
我无法在 Chrome 中找到有关受支持的事件构造函数的任何文档.有人能指点我一些文档/源代码吗?
I was not able to find any documentation on supported event constructors in Chrome. Could anyone point me to some docs/source code?
还有其他方法可以在 Google Chrome 扩展程序中模拟键盘事件吗?
Any other way to simulate keyboard events in a Google Chrome extension?
(注意,TextEvent 无济于事,因为许多现实世界的控件专门监听 keydown
/keyup
)
(note, TextEvent won't help because many real-world controls listen to keydown
/keyup
specifically)
推荐答案
我发现 chrome 调试器协议 v1.1 是从 Google Chrome 扩展程序模拟按键和鼠标事件的明确答案.部分协议可通过 chrome.debugger API 访问.
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.
这篇关于如何在 Chrome 扩展程序中使用给定的字符/键码初始化键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!