Last active
August 5, 2024 05:08
-
-
Save ubershmekel/9ae7d845404cf8343e91ea0cd20f92fb to your computer and use it in GitHub Desktop.
Paste the following into a bookmark. When you're on a youtube channel's video page, you can now sort that page by view count. This new version works with the 2022 UI refresh that added rows to the mix.
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('#metadata-line'); | |
} | |
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