Skip to content

Instantly share code, notes, and snippets.

@ryo1kato
Created May 15, 2015 20:28
Show Gist options
  • Save ryo1kato/1bf4f23bb69296c60159 to your computer and use it in GitHub Desktop.
Save ryo1kato/1bf4f23bb69296c60159 to your computer and use it in GitHub Desktop.
anti-keygrabber.user.js for Mac/Emacs user
// ==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;
}
var modifier = isMac ? e.metaKey : e.ctrlKey;
// abort if the proper command/control modifier isn't pressed
if (!modifier) {
return;
}
if (e.ctrlKey) {
switch (e.keyCode) {
case 87: // W - close window
case 84: // T - open tab
case 74: // J - Japanese input switch
case 65: // A
case 66: // B
case 69: // E
case 70: // F
case 72: // H
case 78: // N
case 80: // P
e.stopImmediatePropagation();
return;
}
}
if (!isMac && e.metaKey) {
switch (e.keyCode) {
case 87: // W - close window
case 84: // T - open tab
case 74: // J - Japanese input switch
case 188: // , (comma) - open settings [mac]
case 82: // R - reload tab
e.stopImmediatePropagation();
return;
}
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment