Skip to content

Instantly share code, notes, and snippets.

@oomathias
Last active June 26, 2022 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oomathias/5098d336d39b6d7f672daca90b3f950a to your computer and use it in GitHub Desktop.
Save oomathias/5098d336d39b6d7f672daca90b3f950a to your computer and use it in GitHub Desktop.
lib.rs - sort by downloads
// ==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