在 Javascript 中捕获按键(Google 文档)

发布于 2024-10-06 16:59:15 字数 894 浏览 2 评论 0原文

我正在尝试编写一个小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],我制作了这个带有一些注释的屏幕截图... screenshot

我所说的“编辑窗格”似乎是一堆显示我输入内容的 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...
screenshot

The "editing pane" I'm talking about seems to be a bunch of Javascripted-up divs displaying what I type.

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2024-10-13 16:59:15

在 Google 文档中进行编辑使用 iframe。您需要将侦听器附加到 iframe 的文档。它似乎对 iframe 做了一些复杂的事情,我还没有完全解决,但以下似乎适用于 Firefox:

var iframe = document.getElementsByTagName("iframe")[0];
if (iframe) {
    iframe.contentDocument.addEventListener("keypress", function(evt) {
        console.log("iframe keypress: " + evt.which);
    }, false);
}

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:

var iframe = document.getElementsByTagName("iframe")[0];
if (iframe) {
    iframe.contentDocument.addEventListener("keypress", function(evt) {
        console.log("iframe keypress: " + evt.which);
    }, false);
}
苹果你个爱泡泡 2024-10-13 16:59:15

似乎对我有用...但我使用 jQuery $(document).ready() 来确保在附加任何事件之前加载我的页面。
我认为你可以用裸JavaScript来做到这一点:

window.onload = function()
{
  els = document.getElementsByTagName("*");
  for(i=0;i<els.length;i++) {
    els[i].onkeypress=function(){alert("hello!");};
  }
}

顺便说一句,你不能将多个函数附加到事件:

for(i=0;i<els.length;i++){
    els[i].onkeypress=function(){alert("hello!");};
    els[i].onkeypress=function(){alert("hello2!");};
    els[i].onkeypress=function(){alert("hello3!");};
}

该元素将仅注册最后一个函数(它覆盖前一个函数),在本例中为“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 :

window.onload = function()
{
  els = document.getElementsByTagName("*");
  for(i=0;i<els.length;i++) {
    els[i].onkeypress=function(){alert("hello!");};
  }
}

By the way you can't attach more than one function to the event:

for(i=0;i<els.length;i++){
    els[i].onkeypress=function(){alert("hello!");};
    els[i].onkeypress=function(){alert("hello2!");};
    els[i].onkeypress=function(){alert("hello3!");};
}

The element will register only the last one (it overrides the previous function), in this case "alert("hello3!");"

染火枫林 2024-10-13 16:59:15

您不需要将侦听器附加到页面上的所有元素。将其附加到文档中就足够了。

window.addEventListener('load', function() {
    document.addEventListener('keypress', function() { alert("hello!"); });
}

由于它是 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.

window.addEventListener('load', function() {
    document.addEventListener('keypress', function() { alert("hello!"); });
}

Since it is a GreaseMonkey script you do not need to worry about IE and can use the addEventListener method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文