Martin J.H. Martin J.H. - 7 months ago 17
Javascript Question

Greasemonkey script to intercept key presses

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.

Answer

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);