在 Javascript 中捕获按键(Google 文档)
我正在尝试编写一个小greasemonkey 脚本/书签/你有什么Google 文档。我想添加的功能需要一个 keypress/keyup/keydown 事件处理程序(这三个之一)。不幸的是,Javascript 不是我的强项,而且我似乎无法在编辑窗格中捕获(?)按键事件。作为最后的手段,我尝试了以下方法:
javascript:(function(){
els = document.getElementsByTagName("*");
for(i=0;i<els.length;i++){
els[i].onkeypress=function(){alert("hello!");};
els[i].onkeyup=function(){alert("hello2!");};
els[i].onkeydown=function(){alert("hello3!");};
}
})();
但是,这仍然无法捕获编辑窗格中的按键 - 没有烦人的警报(尽管它似乎适用于大多数其他网站......)。我已经检查了 Chrome 和 Firefox(我无法让它在任何一个中工作)。
我在 Firebug 中尝试了“记录事件”(并通过 Firebug 的一个简洁的小扩展,Eventbug 检查了所有注册的事件);这些事件似乎并不是在按键时触发的。
编辑:
为了澄清[Tim],我制作了这个带有一些注释的屏幕截图...
我所说的“编辑窗格”似乎是一堆显示我输入内容的 Javascripted div。
有什么想法吗?谢谢!
I'm trying to write a little greasemonkey script/bookmarklet/what have you for Google Docs. The functionality I'd like to add needs a keypress/keyup/keydown event handler (one of those three). Unfortunately, Javascript isn't my forté, and I can't seem to capture (?) a keypress event to while in the edit pane. As a last resort, I've tried the following:
javascript:(function(){
els = document.getElementsByTagName("*");
for(i=0;i<els.length;i++){
els[i].onkeypress=function(){alert("hello!");};
els[i].onkeyup=function(){alert("hello2!");};
els[i].onkeydown=function(){alert("hello3!");};
}
})();
However, this still doesn't capture keypresses in the editing pane - no annoying alerts (although it seems to work for most other sites...). I've checked in Chrome and Firefox both (I can't get it to work in either one).
I tried "Log Events" in Firebug (and checked out all the registered events via a neat little extension to Firebug, Eventbug); it didn't seem like those events were firing on keypresses.
Edit:
To clarify [Tim], I made this screenshot with some annotations...
The "editing pane" I'm talking about seems to be a bunch of Javascripted-up divs displaying what I type.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Google 文档中进行编辑使用 iframe。您需要将侦听器附加到 iframe 的文档。它似乎对 iframe 做了一些复杂的事情,我还没有完全解决,但以下似乎适用于 Firefox:
Editing in Google Docs uses an iframe. You need to attach a listener to the iframe's document. It seems to do something complicated with the iframe I haven't yet been able to work out fully, but the following seems to work for Firefox:
似乎对我有用...但我使用 jQuery $(document).ready() 来确保在附加任何事件之前加载我的页面。
我认为你可以用裸JavaScript来做到这一点:
顺便说一句,你不能将多个函数附加到事件:
该元素将仅注册最后一个函数(它覆盖前一个函数),在本例中为“alert(”你好3!");"
Seems to work for me... but I'm using jQuery $(document).ready() to make sure my page is loaded before attaching any event.
I think you could do it in bare javascript with :
By the way you can't attach more than one function to the event:
The element will register only the last one (it overrides the previous function), in this case "alert("hello3!");"
您不需要将侦听器附加到页面上的所有元素。将其附加到文档中就足够了。
由于它是 GreaseMonkey 脚本,因此您无需担心 IE,并且可以使用
addEventListener
方法。You do not need to attach the listener to all the elements on the page. To attach it to the document is enough.
Since it is a GreaseMonkey script you do not need to worry about IE and can use the
addEventListener
method.