Disable website keyboard hooks

Stop websites from hijacking keyboard shortcuts.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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 std_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) {
    var keycode_set;
    if (isMac ? e.metaKey : e.ctrlKey) {
        keycode_set = meta_keycodes;
    } else if (e.altKey) {
        keycode_set = alt_keycodes;
    } else {
        keycode_set = std_keycodes;
    }

    if (keycode_set.has(e.keyCode)) {
        e.stopImmediatePropagation();
    }
    return false;
});