Created
June 21, 2017 13:24
-
-
Save coffee-mug/ad160424354654290d4c3e54053b3969 to your computer and use it in GitHub Desktop.
Script to sort HN links by comments count
This file contains hidden or 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
/** Script to sort HN comments by post date **/ | |
var itemsArray = [], | |
tb = document.querySelectorAll('table.itemlist > tbody > tr'); | |
function sortBycom(a, b) { | |
var left = a[1].querySelector('td.subtext > a:last-child'), | |
right = b[1].querySelector('td.subtext > a:last-child'); | |
// If no comments has been made yet, push them at the end | |
if (left.innerText === "discuss") { | |
return 1; | |
} else if (right.innerText === "discuss") { | |
return -1; | |
} | |
left = parseInt(left.innerText.split(' ')[0], 10); | |
right = parseInt(right.innerText.split(' ')[0], 10); | |
return right - left; | |
}; | |
// Each HN "item" consists of 3 rows : tr.athing - tr - tr.spacer. | |
// Let's collect them 3 by 3. | |
for (var i = 0; i < 90; i+= 3) { | |
itemsArray.push(Array.prototype.slice.call(tb, i, i + 3)) | |
} | |
itemsArray.sort(sortBycom); | |
// Remove tbody children to make space for the sorted one. | |
tb.forEach( e => { | |
var parent = e.parentNode; | |
parent.removeChild(e); | |
}); | |
// Add the list (not looking for performance here) | |
itemsArray.forEach( e => { | |
var parent = document.querySelector('table.itemlist > tbody'); | |
e.forEach( r => { parent.appendChild(r) }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment