最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Greasemonkey script to intercept key presses - Stack Overflow

programmeradmin7浏览0评论

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 ); //unment to find more keyCodes
    if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    alert("Gotcha!"); //ment 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.

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 ); //unment to find more keyCodes
    if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    alert("Gotcha!"); //ment 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.

Share Improve this question edited Mar 20, 2017 at 10:18 CommunityBot 11 silver badge asked Nov 5, 2013 at 9:17 Martin J.H.Martin J.H. 2,2053 gold badges24 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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 ); //unment to find more keyCodes
    if (keycodes.indexOf(e.keyCode) != -1 && e.ctrlKey) {
        e.cancelBubble = true;
        e.stopImmediatePropagation();
    // alert("Gotcha!"); //ument to check if it's seeing the bo
    }
    return false;
}, !window.opera);
发布评论

评论列表(0)

  1. 暂无评论