Skip to content

Instantly share code, notes, and snippets.

@fallaciousreasoning
Last active August 21, 2019 02:11
Show Gist options
  • Save fallaciousreasoning/300b545355e257e6de59f1a3f8d70cc4 to your computer and use it in GitHub Desktop.
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.
// ==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