Skip to content

Instantly share code, notes, and snippets.

@Gkjsdll
Last active September 17, 2022 22:17
Show Gist options
  • Save Gkjsdll/0171f93894cb73e31f1ff204afc240ce to your computer and use it in GitHub Desktop.
Save Gkjsdll/0171f93894cb73e31f1ff204afc240ce to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name 5e.tools KB Nav
// @namespace https://gkjsdll.com/
// @version 0.1.1
// @description Add some 5e.tools Keyboard Navigation
// @author Zack (Gkjsdll) Winchell
// @match *://5e.tools/actions*
// @match *://5e.tools/backgrounds*
// @match *://5e.tools/bestiary*
// @match *://5e.tools/charcreationoptions*
// @match *://5e.tools/classes*
// @match *://5e.tools/conditionsdiseases*
// @match *://5e.tools/deities*
// @match *://5e.tools/feats*
// @match *://5e.tools/items*
// @match *://5e.tools/languages*
// @match *://5e.tools/optionalfeatures*
// @match *://5e.tools/psionics*
// @match *://5e.tools/races*
// @match *://5e.tools/recipes*
// @match *://5e.tools/rewards*
// @match *://5e.tools/spells*
// @match *://5e.tools/vehicles*
// @grant none
// ==/UserScript==
(function addKeyboardNavigation() {
function getActiveTab() {
return document.querySelector('.ui-tab__btn-tab-head--active');
}
function getSelectedRow() {
return document.querySelector('.list-multi-selected');
}
function handleArrowDown() {
const selectedRow = getSelectedRow();
const newRow = selectedRow.nextElementSibling ?? selectedRow.parentElement.querySelector(':scope > .lst__row:first-of-type');
newRow.querySelector(':scope > a').click();
newRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
function handleArrowLeft() {
const activeTab = getActiveTab();
const newTab = activeTab.previousElementSibling ??
activeTab.parentElement.querySelector(':scope > button:last-of-type');
newTab.click();
}
function handleArrowRight() {
const activeTab = getActiveTab();
const newTab = (() => {
if (activeTab.nextElementSibling?.tagName === 'BUTTON') {
return activeTab.nextElementSibling;
}
return activeTab.parentElement.querySelector(':scope > button:first-of-type');
})();
newTab.click();
}
function handleArrowUp() {
const selectedRow = getSelectedRow();
const newRow = selectedRow.previousElementSibling ??
selectedRow.parentElement.querySelector(':scope > .lst__row:last-of-type');
newRow.querySelector(':scope > a').click();
newRow.scrollIntoView({ behavior: 'smooth', block: 'center' });;
}
function onKeydown(event) {
switch (event.key) {
case 'ArrowDown': {
handleArrowDown();
break;
}
case 'ArrowLeft': {
handleArrowLeft();
break;
}
case 'ArrowRight': {
handleArrowRight();
break;
}
case 'ArrowUp': {
handleArrowUp();
break;
}
}
}
window.addEventListener('keydown', onKeydown);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment