Skip to content

Instantly share code, notes, and snippets.

@KorigamiK
Last active May 15, 2022 05:35
Show Gist options
  • Save KorigamiK/1e74bf5bb84dc24a3f280434ea8a136a to your computer and use it in GitHub Desktop.
Save KorigamiK/1e74bf5bb84dc24a3f280434ea8a136a to your computer and use it in GitHub Desktop.
Snahp.it User Scripts
// ==UserScript==
// @name snahp.eu Forum Sort Topics by Filesize
// @grant none
// @match *://fora.snahp.eu/*
// ==/UserScript==
function sortAllLists(){
Array.from(document.getElementsByClassName('topics')).forEach(sortByFileSize);
return;
}
function sortByFileSize(list){
Array.from(list.getElementsByClassName('row')).sort(compareSize).forEach(li => list.appendChild(li));
return;
}
function compareSize(a,b){
var regex = /\d+(\.\d+)? *(kb|mb|gb|tb)/i;
if (regex.exec(a.getElementsByClassName('topictitle')[0].innerHTML) !== null){
var sizeA = regex.exec(a.getElementsByClassName('topictitle')[0].innerHTML)[0];
sizeA = sizeA.replace(/ *kb/i, ' * 1000');
sizeA = sizeA.replace(/ *mb/i, ' * 1000000');
sizeA = sizeA.replace(/ *gb/i, ' * 1000000000');
sizeA = sizeA.replace(/ *tb/i, ' * 1000000000000');
sizeA = eval(sizeA);
} else
sizeA = Number.MAX_SAFE_INTEGER;
if (regex.exec(b.getElementsByClassName('topictitle')[0].innerHTML) !== null){
var sizeB = regex.exec(b.getElementsByClassName('topictitle')[0].innerHTML)[0];
sizeB = sizeB.replace(/ *kb/i, ' * 1000');
sizeB = sizeB.replace(/ *mb/i, ' * 1000000');
sizeB = sizeB.replace(/ *gb/i, ' * 1000000000');
sizeB = sizeB.replace(/ *tb/i, ' * 1000000000000');
sizeB = eval(sizeB);
} else
sizeB = Number.MAX_SAFE_INTEGER;
return sizeA - sizeB;
}
if (document.getElementsByClassName('topics').length > 0){
var element = document.getElementsByClassName("bar-top");
for (var i = 0; i < element.length; i++) {
element[i].insertAdjacentHTML("beforeend", "<button class='button' id='SortAllTopics" + i + "'>Filesize Sort</button>");
document.getElementById("SortAllTopics" + i).onclick = sortAllLists;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment