且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Chrome扩展程序:检测Google文档中的按键

更新时间:2021-07-21 09:26:50

我在g-docs或g-sheets上看不到用于主要内容的iframe,但是如果您坚持可以使用,使其自动在所有iframe中运行.

I don't see iframes used for the main content on g-docs or g-sheets, but if you insist you can use "all_frames": true and "match_about_blank": true in manifest.json content script declaration to make it run in all iframes automatically.

另一个想法是在网站看到事件之前捕获事件:声明"run_at":"document_start "用于manifest.json中的内容脚本,并将true用于 useCapture 参数:document.addEventListener("keydown", handler, true);-在构建页面DOM之前,应在内容脚本的主代码中执行此行,以注册侦听器,请勿将其放在某些loadDOMContentLoaded回调.

Another idea is to capture events before the site sees them: declare "run_at": "document_start" for your content script in manifest.json and use true for useCapture parameter of addEventListener: document.addEventListener("keydown", handler, true); - this line should be executed in the main code of your content script to register the listener before the page DOM is built, don't put it inside some load or DOMContentLoaded callback.