You’ll probably want to edit it to suit your needs, but put keys you want to block handling of in the matchers
array, and add @match
entries for whatever websites you want to run it on. Also you can delete the code after the comment about google drive if you aren’t interested in the conversion it mentions
// ==UserScript==
// @name Stop Keyboard Shortcut Hijacking
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Blocks hijacking of the shortcuts that websites like to hijack
// @author TellowKrinkle
// @match https://floflo.moe/testing/
// @match https://drive.google.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function dispatchKeyboardEvent(options) {
let event = new KeyboardEvent("keydown", options);
document.body.dispatchEvent(event);
}
let matchers = [
(e) => { return e.code == "KeyA" && e.ctrlKey && !e.shiftKey && !e.metaKey && !e.altKey },
(e) => { return e.code == "KeyZ" && !e.ctrlKey && !e.shiftKey && e.metaKey && !e.altKey && e.isTrusted }
];
document.addEventListener("keydown", function(e) {
if (matchers.filter(matcher => matcher(e)).length > 0) {
e.cancelBubble = true;
e.stopImmediatePropagation();
}
// Convert Cmd-Option-Z into Cmd-Z because Google Drive doesn't expose their undo feature through any menu systems
if (e.code == "KeyZ" && !e.ctrlKey && !e.shiftKey && e.metaKey && e.altKey) {
e.cancelBubble = true;
e.stopImmediatePropagation();
dispatchKeyboardEvent({ code: "KeyZ", metaKey: true, keyCode: 90, key: "z", which: 90 })
}
}, true);
})();