Disable website keyboard hooks

Stop websites from hijacking keyboard shortcuts.

As of 26.03.2017. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name           Disable website keyboard hooks
// @description    Stop websites from hijacking keyboard shortcuts.
// @author         Isaac Levy, Jonas Singe
// @run-at         document-start
// @include        *
// @grant          none
// @version        0.0.2
// @namespace      https://github.com/isaacl/keycodeScript
// ==/UserScript==

// Disable these keys when no keyboard modifier is pressed
var keycodes = new Set([
    // Add keycodes as desired
    37, 38, 39, 40 // Arrow Keys.
]);

// Disable these keys when Meta key is pressed.
var meta_keycodes = new Set([
	// Add keycodes as desired
	70 // Ctrl + F
]);

// Disable these keys when Alt key is pressed.
var alt_keycodes = new Set([
	// Add keycodes as desired
	83 // Alt + S
]); 


// Don't change below this line.

var isMac = navigator.platform.indexOf('Mac') >= 0;


document.addEventListener('keydown', function(e) {
    //console.log(e);
    if (isMac ? e.metaKey : e.ctrlKey) {
        if (meta_keycodes.has(e.keyCode)) {
            e.stopImmediatePropagation();
        }
    } else if (e.altKey) {
        if (alt_keycodes.has(e.keyCode)) {
            e.stopImmediatePropagation();
        }
    } else if (keycodes.has(e.keyCode)) {
        e.stopImmediatePropagation();
    }
    return false;
});