Last active
August 24, 2021 22:42
-
-
Save EduardoPazz/8bc2301195922c1ff1fbe5b61d24c428 to your computer and use it in GitHub Desktop.
Toggle node class based on URL script
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
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