Chrome扩展程序:检测Google文档中的按键 [英] Chrome Extension: detecting keypresses in Google Docs

查看:100
本文介绍了Chrome扩展程序:检测Google文档中的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我的朋友和我刚接触javascript,并遇到了一些代码问题. 目前,我们正在尝试开发一个chrome扩展程序,通过检测击键来检测用户在特定的google文档上的工作时间和工作量.

Hey my friends and I new to javascript and are encountering problems with some code. Currently we're trying to make a chrome extension that detects when and how much a user works on a particular google document, by detecting keystrokes.

我们当前的方法涉及创建"keypress"事件监听器.我们将其放入可在任何docs.google网页上运行的content.js文件中.事实是,当您在页面上编辑标题/其他内容时,它可以工作,但是由于某些原因,当用户实际编辑文档时,它不会注册.我们在其他网站上对其进行了尝试,并且可以正常运行,并且无法将其添加到background.js.

Our current method involves creating a 'keypress' event listener. We put it into a content.js file that runs on any docs.google webpage. The thing is, it works when you're on the page editing the title/anything else, but for some reason it doesnt register when the user is actually editing the document. We tried it on other websites and it works, and adding it to background.js doesn't work.

var handler = function (e) { 
    handler.data.push(e);
    console.log("success");
    console.log(handler.data);
}
handler.data = [];
window.addEventListener("keydown", handler);
document.addEventListener("keydown", handler);

所以我们试图更改google docs文档的'iframe'的权限,以便我们可以使用内容脚本,但是它仍然不起作用(这里是代码)

So we tried to change the permissions on the 'iframe' of the google docs document so that we could use content scripts but it still didn't work (here's the code)

var divs = document.getElementsByTagName("iframe");
for(var i = 0; i < divs.length; i++){
divs[i].sandbox = 'allow-scripts'
divs[i].addEventListener('keydown', handler, true);

任何帮助表示赞赏

推荐答案

我在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.

这篇关于Chrome扩展程序:检测Google文档中的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆