Skip to content

Instantly share code, notes, and snippets.

@EduardoPazz
Last active August 24, 2021 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EduardoPazz/8bc2301195922c1ff1fbe5b61d24c428 to your computer and use it in GitHub Desktop.
Save EduardoPazz/8bc2301195922c1ff1fbe5b61d24c428 to your computer and use it in GitHub Desktop.
Toggle node class based on URL script
function highlight() {
const currentURL = window.location.href;
if (!currentURL.includes("categorias")) {
return;
}
// Indexes
const K = 0; // Stands for Key
const V = 1; // Stands for Value
const categoryMapping = [
['ultra-som', 'Ultra-som'],
['hot-plate', 'Hot Plate'],
['spin', 'Spin'],
['vibracao-linear', 'Vibração Linear'],
['especial', 'Especial']
]
const category = categoryMapping.find(cat => currentURL.endsWith(`${cat[K]}/`));
/*
* This is pretty unlikely to be true, but we'll keep here for redundancy.
* As long as we can assert the actual URL contains 'categorias', it must contains
* some of the categories keywords listed above
*/
if (category === undefined) {
return;
}
categoryButtons = Array.from(document.querySelectorAll("div.product-subcategories.responsive.grid div.archive-listing.classic-grid h3.product-name"));
categoryButtons
.find(button => button.innerText === category[V])
.classList.toggle('active');
}
highlight();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment