Skip to content

Instantly share code, notes, and snippets.

@martixy
Last active September 12, 2020 12:39
Show Gist options
  • Save martixy/8d675bc867192a361d9e7584bd4ccbb7 to your computer and use it in GitHub Desktop.
Save martixy/8d675bc867192a361d9e7584bd4ccbb7 to your computer and use it in GitHub Desktop.
d20SRD Redirect and Navigate
// ==UserScript==
// @name d20SRD Redirect and Navigate
// @namespace http://www.d20srd.org/
// @version 0.2
// @description Redirects to the 3.5 site automatically, easy keyboard shortcut navigation
// @author martixy
// @match http://www.d20srd.org/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @grant none
// ==/UserScript==
/*
v0.1 Redirect
v0.2 Added simple navigation, and search focus shortcuts
v0.3 Fix a lil bug where home key triggered in inputs
*/
(function() {
'use strict';
if (window.location == "http://www.d20srd.org/") {
window.location = 'http://www.d20srd.org/index.htm';
}
$(function() {
if (window.location == "http://www.d20srd.org/index.htm") {
let links = {};
let keys = {
'Spells': 's',
'Skills': 'k',
'Monsters': 'm',
'Feats': 't',
'Equipment & Special Materials': 'e',
'Magic Items': 'i',
'Improving Monsters': 'p',
'Combat': 'c',
'Classes': 'l',
'Condition Summary': 'o',
};
$('a').each(function() {
switch(this.textContent) {
case 'Spells':
case 'Skills':
case 'Monsters':
case 'Feats':
case 'Equipment & Special Materials':
case 'Magic Items':
case 'Improving Monsters':
case 'Combat':
case 'Classes':
case 'Condition Summary':
if (!links[this.textContent]) {
links[this.textContent] = this;
this.textContent = `${this.textContent} [${keys[this.textContent]}]`;
}
break;
default:
break;
}
});
let temp = {};
for (let prop in keys) {
temp[keys[prop]] = prop;
}
keys = temp;
$('body').on('keydown', function(ev) {
let focus = $(':focus');
let isInput = focus.length > 0 && focus[0].tagName == 'INPUT';
if (keys[ev.key] && !isInput) {
links[keys[ev.key]].click();
}
});
}
else {
let home = document.querySelector("#path > a:nth-child(1)");
if (home) home.textContent = home.textContent + ' [h]';
$('body').on('keydown', function(ev) {
let focus = $(':focus');
let isInput = focus.length > 0 && focus[0].tagName == 'INPUT';
if (ev.key === 'h' && !isInput) {
window.location = "http://www.d20srd.org/index.htm";
}
});
}
setTimeout(function() {
if ($('#gsc-i-id1').length > 0) {
let el = document.querySelector("body > div:nth-child(9) > h2:nth-child(1)");
if (!el) el = document.querySelector("#searchIndex > b");
if (el) el.textContent = el.textContent + ' [f]';
$('body').on('keyup', function(ev) {
switch(ev.key) {
case 'f':
let el = $('#gsc-i-id1');
if (ev.target !== el.get(0)) {
$('#gsc-i-id1').focus();
}
break;
}
});
}
}, 300);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment