Last active
April 5, 2023 02:08
-
-
Save ubershmekel/dd03a3ba3cabc7fb4e46af6b36f8e034 to your computer and use it in GitHub Desktop.
On a video page, run this in the console, or click the bookmarklet, to have the comments sorted by their upvotes
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
javascript: function findNodes(el) { | |
return el.querySelectorAll('#vote-count-middle'); | |
} | |
function isDigit(str) { | |
return str.length === 1 && str.match(/[0-9]/i); | |
} | |
function metricVal(el) { | |
let modifiers = { | |
K: 1e3, | |
M: 1e6, | |
B: 1e9, | |
}; | |
let text = el?.textContent?.trim(); | |
if (!text) { | |
console.log('empty text', el); | |
return NaN; | |
} else { | |
console.log('score found', text); | |
} | |
const match = text.match(/\d+(\.\d+)?[KMB]?/); | |
if (!match) { | |
console.log("bad parse " + text); | |
return NaN; | |
} | |
const numText = match[0]; | |
let number; | |
if (isDigit(numText[numText.length - 1])) { | |
number = +numText; | |
} else { | |
number = +numText.slice(0, -1); | |
const modChar = match[0].slice(-1); | |
number = number * modifiers[modChar]; | |
} | |
return number; | |
} | |
function compare(el1, el2) { | |
let val1 = metricVal(findNodes(el1)[0]); | |
let val2 = metricVal(findNodes(el2)[0]); | |
if (val1 < val2) return 1; | |
if (val1 > val2) return -1; | |
return 0; | |
} | |
function nodeParent(node) { | |
if (node instanceof DocumentFragment) { | |
return node.host; | |
} else { | |
return node.parentNode; | |
} | |
} | |
function findCommonParent(el1, el2) { | |
const seen = new Set(); | |
let next = el1; | |
while (next) { | |
seen.add(next); | |
next = nodeParent(next); | |
} | |
next = el2; | |
while (next) { | |
if (seen.has(next)) { | |
return next; | |
} else { | |
next = nodeParent(next); | |
} | |
} | |
return null; | |
} | |
function main() { | |
let nodes = findNodes(document); | |
let topNode = findCommonParent(nodes[0], nodes[1]); | |
[...topNode.children].sort(compare).forEach(node => topNode.appendChild(node)) | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment