Last active
August 21, 2019 02:11
-
-
Save fallaciousreasoning/300b545355e257e6de59f1a3f8d70cc4 to your computer and use it in GitHub Desktop.
A helper script for the crag for showing/hiding trad routes. This adds a button "Show/Hide Trad Routes" to the start of the Routes table.
This file contains 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
// ==UserScript== | |
// @name The Crag, Only Sport | |
// @description A script for filtering trad climbs on the crag. | |
// @locale en | |
// @namespace Violentmonkey Scripts | |
// @match https://www.thecrag.com/* | |
// @grant none | |
// ==/UserScript== | |
const storeValue = (key, value) => localStorage.setItem(key, value); | |
const getValue = (key) => { | |
const json = localStorage.getItem(key); | |
if (!json) return; | |
return JSON.parse(json); | |
} | |
let showingTrad = getValue('showingTrad'); | |
const addTradFilterButton = () => { | |
const group = document.querySelector(".node-listview .btn-group-group"); | |
const button = document.createElement("small"); | |
button.className = "btn-group inline" | |
const updateTradButton = () => { | |
button.innerHTML = ` | |
<a class="btn btn-mini"> | |
<i class="icon-${showingTrad ? "minus" : "plus"}-sign"></i> | |
${showingTrad ? "Hide" : "Show"} Trad Routes | |
</a>`; | |
}; | |
button.addEventListener('click', () => { | |
showingTrad = !showingTrad; | |
setTradRouteVisibility(showingTrad); | |
updateTradButton(); | |
storeValue('showingTrad', showingTrad); | |
}); | |
updateTradButton(); | |
group.appendChild(button); | |
} | |
const getTradRoutes = () => { | |
return Array.from(document.querySelectorAll('.title .tags.trad')).map(tag => tag.parentNode.parentNode.parentNode.parentNode.parentNode); | |
} | |
const setTradRouteVisibility = (visible) => { | |
getTradRoutes().forEach(r => r.setAttribute("style", visible ? "" : "display: none;")) | |
} | |
setTradRouteVisibility(showingTrad); | |
addTradFilterButton(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment