在用户脚本中覆盖文档 keydown 侦听器(来自 Closure)?
我正在尝试编写一个用户脚本来重新定义网络应用程序的键绑定。使用 Chrome DOM 检查器,我发现在捕获阶段,键处理程序被定义为文档对象上的 keydown 事件侦听器。据我了解事件模型,文档对象上的任何捕获阶段侦听器都将在其他侦听器之前运行,因此让我自己的侦听器在默认侦听器之前运行的唯一方法是在 Web 应用程序初始化之前安装它。
运行
document.addEventListener("keydown", function(e){
alert("foo");
e.stopPropagation();
e.stopImmediatePropagation();
}, true);
因此,我使用 @run-at document-start
编写了一个用户脚本,以便在 Web 应用程序安装自己的处理程序之前 。根据 DOM 检查器的说法,我的处理程序确实是第一个,而且确实当我在页面的某些部分按下某个键时,我会收到警报。但在我试图“修复”的富文本编辑器中,警报完全丢失。
我还尝试添加自己的替换 Node.prototype.addEventHandler 来拦截注册有问题处理程序的调用,但似乎 Closure 并不适合它(或者还有其他一些计时问题),因为当页面加载完成时,它会返回到本机函数而不是我的替换函数。而且本机 addEventHandler
显然使得无法检查处理程序,因此据我所知,我无法访问 Web 应用程序的处理程序来修改或删除它。
所以现在我已经没有想法了 - 其他人会如何改变这个密钥处理程序?
I'm trying to write a userscript to redefine the keybindings of a webapp. Using the Chrome DOM inspector I've found that the key handler is defined as a keydown event listener on the document object, during the capture phase. As I understand the event model, any capture-phase listeners on the document object will run before any others, so the only way to get my own listener to run before the default is to install it before the webapp initializes.
So I wrote a userscript with @run-at document-start
to run
document.addEventListener("keydown", function(e){
alert("foo");
e.stopPropagation();
e.stopImmediatePropagation();
}, true);
before the webapp installs its own handlers. According to the DOM inspector, my handler is indeed first, and indeed when I hit a key in certain parts of the page, I get the alert. But within the rich text editor I'm trying to "fix", the alert is entirely missing.
I also tried adding my own replacement Node.prototype.addEventHandler
to intercept the call that registers the offending handler, but it seems that Closure doesn't fall for it (or else there's some other timing issues), since by the time the page is done loading, it's back to a native function rather than my replacement. And the native addEventHandler
apparently makes it impossible to inspect the handlers, so as far as I can tell there's just no way I can get at the webapp's handler to modify or remove it.
So now I've run out of ideas - how would anyone else go about changing this keyhandler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论