Greasemonkey脚本拦截按键(Greasemonkey script to intercept key presses)

理论上,以下用户脚本(在Firefox中使用Greasemonkey)应该捕获所有网站上的所有Ctrl+t事件,提醒“Gotcha!”,然后阻止网站看到Ctrl+t事件。

但是,它仅适用于某些网站(Google,Stack Exchange),但不适用于其他网站。 用户脚本不起作用的一个例子是Codecademy(当代码编辑器具有焦点时),其中Ctr+t总是切换光标旁边的两个字符。

我禁用了Flash,所以我认为这是一个可以通过JavaScript解决的问题。 我可以在脚本中更改哪些内容,以便真正阻止事件冒泡到网站脚本?

// ==UserScript== // @name Disable Ctrl T interceptions // @description Stop websites from highjacking keyboard shortcuts // // @run-at document-start // @include * // @grant none // ==/UserScript== // Keycode for 't'. Add more to disable other ctrl+X interceptions keycodes = [84]; document.addEventListener('keydown', function(e) { // alert(e.keyCode ); //uncomment to find more keyCodes if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) { e.cancelBubble = true; e.stopImmediatePropagation(); alert("Gotcha!"); //comment this out for real world usage } return false; });

免责声明 :我最初在超级用户上提出了一个更广泛的问题 。 在试图找到答案时,我偶然发现了这个与脚本相关的具体问题。 我不是故意双重发帖 - 我只是认为问题的这一部分可能更适合Stack Overflow而不是超级用户。

The following Userscript (to be used in Firefox with Greasemonkey) should, in theory, capture all Ctrl+t events on all websites, alert "Gotcha!", and then prevent the website from seeing that Ctrl+t event.

However, it only works on some sites (Google, Stack Exchange), but not on others. One example where the Userscript does not work is Codecademy (when the code editor has focus), where Ctr+t always toggles the two characters next to the cursor.

I disabled Flash, so I think this is a problem that can be solved with JavaScript. What can I change in my script so that it really prevents events to bubble through to the website scripts?

// ==UserScript== // @name Disable Ctrl T interceptions // @description Stop websites from highjacking keyboard shortcuts // // @run-at document-start // @include * // @grant none // ==/UserScript== // Keycode for 't'. Add more to disable other ctrl+X interceptions keycodes = [84]; document.addEventListener('keydown', function(e) { // alert(e.keyCode ); //uncomment to find more keyCodes if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) { e.cancelBubble = true; e.stopImmediatePropagation(); alert("Gotcha!"); //comment this out for real world usage } return false; });

Disclaimer: I originally asked a broader question covering this topic on superuser. While trying to find an answer, I stumbled on this specific, scripting related problem. I did not mean to double-post - I simply think that this part of the question might better fit to Stack Overflow than to Superuser.

最满意答案

我通过从这里找到的另一个用户文件复制两行来修复它。

这改变了document.addEventListener行,更重要的是,改变了最后一行。 在Firefox上, !window.opera计算结果为true 。 这作为addEventListener函数的第三个选项参数传递,该函数将useCapture设置为true 。 这具有以下效果:事件在较早的“捕获阶段”中触发,而不是在“冒泡阶段”中触发,并且防止网站的另一个eventListener“看到”该事件。

这是工作脚本:

// ==UserScript== // @name Disable Ctrl T interceptions // @description Stop websites from highjacking keyboard shortcuts // // @run-at document-start // @include * // @grant none // ==/UserScript== // Keycode for 't'. Add more to disable other ctrl+X interceptions keycodes = [84]; (window.opera ? document.body : document).addEventListener('keydown', function(e) { // alert(e.keyCode ); //uncomment to find more keyCodes if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) { e.cancelBubble = true; e.stopImmediatePropagation(); // alert("Gotcha!"); //ucomment to check if it's seeing the combo } return false; }, !window.opera);

I fixed it by copying two lines from another userscript found here.

This changed the document.addEventListener line and, more importantly, the last line. On Firefox, !window.opera evaluates to true. This is passed as the third option argument to the addEventListener function, which sets useCapture to true. This has the effect that the event is triggered in the earlier "capturing phase", not in the "bubbling phase", and prevents that the other eventListener of the website "sees" the event at all.

Here is the working script:

// ==UserScript== // @name Disable Ctrl T interceptions // @description Stop websites from highjacking keyboard shortcuts // // @run-at document-start // @include * // @grant none // ==/UserScript== // Keycode for 't'. Add more to disable other ctrl+X interceptions keycodes = [84]; (window.opera ? document.body : document).addEventListener('keydown', function(e) { // alert(e.keyCode ); //uncomment to find more keyCodes if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) { e.cancelBubble = true; e.stopImmediatePropagation(); // alert("Gotcha!"); //ucomment to check if it's seeing the combo } return false; }, !window.opera);

更多推荐