Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Last active May 26, 2024 17:50
Show Gist options
  • Save rodneyrehm/5213304 to your computer and use it in GitHub Desktop.
Save rodneyrehm/5213304 to your computer and use it in GitHub Desktop.
GreaseMonkey: Prevent Web Applications From Grabbing Certain HotKeys
// ==UserScript==
// @name anti key-grabber
// @description Prevent web apps from capturing and muting vital keyboard shortcuts
// @grant none
// @version 1.1
// ==/UserScript==
(function(){
var isMac = unsafeWindow.navigator.oscpu.toLowerCase().contains("mac os x");
unsafeWindow.document.addEventListener('keydown', function(e) {
if (e.keyCode === 116) {
// F5 should never be captured
e.stopImmediatePropagation();
return;
}
// Mac uses the Command key, identified as metaKey
// Windows and Linux use the Control key, identified as ctrlKey
var modifier = isMac ? e.metaKey : e.ctrlKey;
// abort if the proper command/control modifier isn't pressed
if (!modifier) {
return;
}
switch (e.keyCode) {
case 87: // W - close window
case 84: // T - open tab
case 76: // L - focus awesome bar
case 74: // J - open downloads panel
e.stopImmediatePropagation();
return;
}
// s'more mac love
if (!isMac) {
return;
}
switch (e.keyCode) {
case 188: // , (comma) - open settings [mac]
case 82: // R - reload tab
e.stopImmediatePropagation();
return;
}
}, true);
})();
@Hund
Copy link

Hund commented Oct 31, 2015

Is it possible to add support for the Escape key? I cant leave input mode in Vimperator on sites like Facebook without closing every conversation first.

@protrolium
Copy link

thanks @rodneyrehm. I modified this script slightly by adding case 190: // . period and case 191 // '/' but it doesn't seem to work; Google still hijacks these key presses. I am using Firefox 43.0.1 with Vimperator/Greasemonkey.

@VVelox
Copy link

VVelox commented Jul 30, 2018

Hmm.... This not appear to be working in 60.0.2.

@lindsayad
Copy link

I'm experiencing the same issue right here on github where I want to disable github's built in behavior of ctrl-b inserting bold formatting, which prevents me from simply moving point like I'm used to with my emacs keybindings.

@mikehaertl
Copy link

For those like me who want to use this with TamperMonkey and the above solution doesn't work, I found a sledgehammer method: Just disable addEventListener('keydown', ...) for certain sites (inspired by this genious answer on SO):

// ==UserScript==
// @name anti keydown-event
// @description Prevent registering of keydown events
// @grant none
// @version 1.0
// @match  https://www.example.com/bitbucket/*
// @run-at document-start
// ==/UserScript==
(function(){
    'use strict';
    const orig = EventTarget.prototype.addEventListener;
    EventTarget.prototype.addEventListener = function(...args) {
        if (args[0] == 'keydown') {
            return;
        }
        return orig.apply(this, args);
    };
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment