如何检测 Gmail 中的键盘事件 [英] How can I detect keyboard events in Gmail

查看:17
本文介绍了如何检测 Gmail 中的键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个浏览器扩展,需要将处理程序附加到所有页面上的 keyupkeydown 事件.我可以使用以下内容脚本代码使其正常工作.

I am writing a browser extension that needs to attach handlers to the keyup and keydown events on all pages. I can get it working pretty well with the following content script code.

document.addEventListener("keydown",keyDown, true);      
document.addEventListener("keyup", keyUp, true);

但我无法在 Gmail 中使用它.具体来说,在撰写新电子邮件的正文时,我无法使其正常工作.它适用于我测试过的其他任何地方.我认为问题是因为 Gmail 在所有键盘事件上调用 stopPropagation 但很难调试它们的最小化代码.我认为将第三个参数设置为 true 会导致在 CAPTURE_PHASE 期间捕获事件,但这不起作用.

I can't get this to work in Gmail though. Specifically I can't get it to work when composing the body of an new email. It will work everywhere else I have tested. I think the problem is because Gmail is calling stopPropagation on all keyboard events but it is difficult to debug their minimized code. I thought that setting the 3rd parameter to true would cause the event to be captured during the CAPTURE_PHASE but this isn't working.

如何在使用 Google Chrome 内容脚本在 Gmail 中编写新正文时捕获 keyupkeydown 事件?

How can I capture keyup and keydown events while composing a new body in Gmail with a Google Chrome content script?

通过将 "all_frames": true, 添加到我的清单,我已经确保我的内容脚本被注入到 DOM 的所有 iframe 中.我什至尝试使用以下代码:

I've ensured that my content scripts are being injected into all iframes of the DOM by adding "all_frames": true, to my manifest. I have even tried using the following code:

document.addEventListener("DOMNodeInserted", function (event) {
     if(event.type === "DOMNodeInserted") {
        if(event.srcElement.nodeName === "IFRAME") {
        console.log(event.srcElement.nodeName + " iframe detected");
        event.srcElement.addEventListener("keydown", function(kevent) {
            document.dispatchEvent(kevent);
            }, true);
        event.srcElement.addEventListener("keyup", function(kevent) {
            document.dispatchEvent(kevent);
            }, true);
        
    }
}
},true);

这仍然不能解决 Gmail 的问题.

This still doesn't fix the issue with Gmail.

推荐答案

您的代码不起作用,因为 event.srcElement 引用了