Last active
December 9, 2022 18:44
-
-
Save po-miyasaka/24301ab9a2a0c09c34c77876c5423b48 to your computer and use it in GitHub Desktop.
Bookmarklet: Making Keyboard-shortcuts for an arbitrary website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: | |
const targets = document.getElementsByClassName('css-8ushbk'); | |
const keyBinds = [ | |
{ | |
key: 'ArrowRight', | |
defaultFunction: () => { targets[2].click() }, | |
commandFunction: () => {}, | |
optionFunction: () => {}, | |
shiftFunction: () => {} | |
}, | |
{ | |
key: 'ArrowLeft', | |
defaultFunction: () => { targets[1].click() }, | |
commandFunction: () => {}, | |
optionFunction: () => {}, | |
shiftFunction: () => {} | |
}, | |
{ | |
key: 'ArrowUp', | |
defaultFunction: () => { targets[0].click() }, | |
commandFunction: () => {}, | |
optionFunction: () => {}, | |
shiftFunction: () => {} | |
}, | |
{ | |
key: 'ArrowDown', | |
defaultFunction: () => { }, | |
commandFunction: () => {}, | |
optionFunction: () => {}, | |
shiftFunction: () => {} | |
}, | |
]; | |
document.addEventListener('keydown', (event) => { | |
console.log(event.key); | |
const keyData = keyBinds.filter( (keyBind) => { return keyBind.key === event.key })[0]; | |
if (keyData == null) { | |
return | |
} | |
if (event.altKey) { | |
keyData.optionFunction() | |
} | |
else if (event.shiftKey) { | |
keyData.shiftFunction() | |
} | |
else if (event.metaKey) { | |
keyData.commandFunction() | |
} | |
else { | |
keyData.defaultFunction() | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment