Created
September 16, 2019 19:21
Star
You must be signed in to star a gist
Sort builds by popularity on sub-pages of https://www.poebuilds.cc
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
// https://www.poebuilds.cc/ | |
(function() { | |
function createElementFromHTML(htmlString) { | |
var div = document.createElement('div'); | |
div.innerHTML = htmlString.trim(); | |
// Change this to div.childNodes to support multiple top-level nodes | |
return div.firstChild; | |
} | |
// Purge | |
document.querySelectorAll('.build.popular').forEach(node => node.parentNode.removeChild(node)); | |
// Create new container | |
document | |
.querySelector('nav') | |
.after(createElementFromHTML(`<ul class="build popular"><li class="skill"><span class="gem"></span>Most Popular</li></ul>`)); | |
const popularBuilds = document.querySelector('.build.popular'); | |
// Gather builds | |
Array.prototype.slice.call(document.querySelectorAll('.build > .light, .build > .dark')) | |
.map(build => { | |
const hasViews = build.querySelector('[title*="forum views"]'); | |
if (!hasViews) return { views: 0 }; | |
const views = parseInt(hasViews.title.replace(/^.+ \/ ([\d\.]+) forum views$/g, '$1').replace(/\./g, ''), 10) || 0; | |
const title = build.querySelector('.name').innerText; | |
const node = build.cloneNode(true); | |
node.removeAttribute('class'); | |
return { | |
views, | |
title, | |
node, | |
}; | |
}) | |
// Remove builds with "no" views | |
.filter(build => build.views !== 0) | |
// Sort high to low | |
.sort((a, b) => a.views > b.views ? -1 : 1) | |
// Filter duplicates | |
.reduce((acc, current) => { | |
if ((acc.slice(-1)[0] || {}).title !== current.title) acc.push(current); | |
return acc; | |
}, []) | |
// Zebra-stripe and add to Most Popular | |
.forEach((build, index) => { | |
build.node.classList.add(index % 2 === 0 ? 'light' : 'dark'); | |
popularBuilds.appendChild(build.node); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Paste in devtools or add and run as snippet:
Source