Skip to content

Instantly share code, notes, and snippets.

@rkuhn
Last active June 4, 2024 08:54
Show Gist options
  • Save rkuhn/c15b7b913da3eee29241bacac277ab32 to your computer and use it in GitHub Desktop.
Save rkuhn/c15b7b913da3eee29241bacac277ab32 to your computer and use it in GitHub Desktop.
GreaseMonkey script to prevent websites from messing with my keyboard shortcuts

To install in Vivaldi, open the Extensions page, enable developer mode, and then drag & drop the file into the page.

// ==UserScript==
// @name forbid rebinding my keys
// @version 0.1
// @description Get off my lawn!
// @author Roland Kuhn
// @match http://*/*
// @match https://*/*
// ==/UserScript==
(function() {
'use strict';
const SHIFT = 1
const CTRL = 2
const ALT = 4
const META = 8
function prevent(ev) {
const mod =
(ev.shiftKey ? SHIFT : 0) |
(ev.ctrlKey ? CTRL : 0) |
(ev.altKey ? ALT : 0) |
(ev.metaKey ? META : 0)
if (ev.code == 'KeyM' && mod == (SHIFT | META)) { return true }
if ((ev.code == 'ArrowLeft' || ev.code == 'ArrowRight') && mod == (ALT | META)) { return true }
if (ev.code == 'KeyF' && mod == (SHIFT | META)) { return true }
if (ev.code == 'KeyR' && mod == META) { return true }
if (ev.code == 'KeyW' && mod == META) { return true }
if (ev.code >= 'Digit0' && ev.code <= 'Digit9' && mod == META) { return true }
if (ev.code == 'KeyJ' && mod == (ALT | META)) { return true }
if (ev.code == 'KeyI' && mod == (ALT | META)) { return true }
if ((ev.code == 'BracketLeft' || ev.code == 'BracketRight') && mod == META) { return true }
return false
}
window.addEventListener('keydown', (ev) => {
if (prevent(ev)) {
ev.stopImmediatePropagation()
}
}, {capture: true})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment