Skip to content

Instantly share code, notes, and snippets.

@loxaxs
Created November 6, 2018 17:12
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 loxaxs/c07f85ecc9373e2f566324f6127c87e0 to your computer and use it in GitHub Desktop.
Save loxaxs/c07f85ecc9373e2f566324f6127c87e0 to your computer and use it in GitHub Desktop.
A JS script for Derpibooru which orders any tag list of the page by tag count. It's supposed to be used in the developer console (F12 or Ctrl+Shift+I/J).
/* A function to change the page display */
function appendCss(css) {
var styleElem = document.createElement('style');
styleElem.textContent = css;
document.head.appendChild(styleElem);
}
/* A function to extract the tag-count of a tag element */
function extractCount(tagElem) {
return parseInt(tagElem.innerText.replace(/^.* \((\d+)\) \+$/, "$1"));
}
/* For all tag list of the page */
for (let tagList of document.querySelectorAll(".tag-list")) {
let tagV = Array.from(tagList.children);
/* The below line computes the new ordre */
tagV.sort((tagA, tagB) => extractCount(tagB) - extractCount(tagA));
/* For all element of the now ordered list */
for (let el of tagV) {
/* Move it to the end of the list */
tagList.appendChild(el);
}
}
/* The below code changes the display of the tags from horizontal to vertical */
appendCss(`
.tag-list > * {
display: table;
}
`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment