Skip to content

Instantly share code, notes, and snippets.

@Voltara
Created September 4, 2018 19:37
Show Gist options
  • Save Voltara/f6698d0259a1886962756f2d4e26065f to your computer and use it in GitHub Desktop.
Save Voltara/f6698d0259a1886962756f2d4e26065f to your computer and use it in GitHub Desktop.
Tampermonkey script to work around issues with SpeedSolving forum hotkeys
// ==UserScript==
// @name SpeedSolving Hotkey Fixer
// @namespace https://voltara.org/
// @version 0.1
// @description Work around glitchy SpeedSolving forum hotkeys
// @author Voltara
// @match https://www.speedsolving.com/forum/*
// @grant none
// ==/UserScript==
// Replaces Alt-0 .. Alt-9 with Ctrl-0 .. Ctrl-9 to avoid conflicting with browser tab switching
// Use keypress modifiers instead of keyup modifiers
// Blocks spurious keyup events
(function() {
'use strict';
var modmask = function (e) {
return (e.ctrlKey && 1) | (e.shiftKey && 2) | (e.altKey && 4) | (e.metaKey && 8);
}
var keys = { }, busy = false;
document.addEventListener("keypress", function (e) {
if (e.key < "0" || e.key > "9") {
return;
}
keys[e.key] = modmask(e);
}, true);
document.addEventListener("keyup", function (e) {
if (busy) {
return;
}
if (e.key < "0" || e.key > "9") {
return;
}
var m0 = keys[e.key], m = modmask(e);
keys[e.key] = undefined;
// block spurious keyup
if (m0 == undefined) {
e.stopPropagation();
return;
}
// block altKey
if (m0 & 4) {
e.stopPropagation();
return;
}
// convert ctrlKey to altKey
if (m0 == 1 && (m == 0 || m == 1)) {
var ei = { };
for (var key in e) {
ei[key] = e[key];
}
ei.ctrlKey = false;
ei.altKey = true;
e.stopPropagation();
busy = true;
e.target.dispatchEvent(new KeyboardEvent('keyup', ei));
busy = false;
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment