Last active
June 26, 2022 19:32
-
-
Save oomathias/5098d336d39b6d7f672daca90b3f950a to your computer and use it in GitHub Desktop.
lib.rs - sort by downloads
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
// ==UserScript== | |
// @name lib.rs - sort by downloads | |
// @version 0.1 | |
// @description Sort by downloads | |
// @match https://lib.rs/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=lib.rs | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
// Keywords handler | |
let targetSelector = "#category-crates ul.crates-list"; | |
let crates = Array.from(document.querySelectorAll('#category-crates ul.crates-list > li')); | |
// Search handler | |
if(!crates.length) { | |
crates = Array.from(document.querySelectorAll('#results ol > li')); | |
targetSelector = "#results ol" | |
}; | |
// Sort nodes by downloads | |
crates = crates.sort(function(c1,c2){ | |
const c1dlel = c1.getElementsByClassName('downloads')[0]; | |
const c2dlel = c2.getElementsByClassName('downloads')[0]; | |
if(!c1dlel) return 1; | |
if(!c2dlel) return -1; | |
const c1dl = parseInt(c1dlel.getAttribute("title").split()[0]); | |
const c2dl = parseInt(c2dlel.getAttribute("title").split()[0]); | |
return c1dl < c2dl ? 1 : c1dl > c2dl ? -1 : 0; | |
}) | |
// Replace nodes | |
let target = document.querySelector(targetSelector); | |
target.innerHTML = "Sorted by downloads:"; | |
crates.forEach(i => target.appendChild(i)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment