Skip to content

Instantly share code, notes, and snippets.

@q00u
Created July 7, 2022 19:01
Show Gist options
  • Save q00u/e5fb2bb2444e6f7528253959b4df26a8 to your computer and use it in GitHub Desktop.
Save q00u/e5fb2bb2444e6f7528253959b4df26a8 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Baka-Updates List Chapter Sorter
// @namespace https://gist.github.com/q00u
// @version 0.1
// @description Sorts titles on your lists ignoring leading articles (a, an, the)
// @author Phoenix G
// @match https://www.mangaupdates.com/mylist.html
// @icon https://www.google.com/s2/favicons?sz=64&domain=mangaupdates.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
const removeArticles = (str) => {
const words = str.toLowerCase().split(' ');
if (words.length <= 1) return str;
if (words[0] === 'a' || words[0] === 'an' || words[0] === 'the') {
return words.splice(1).join(' ');
}
return str.toLowerCase();
}
const setSort = (el) => {
const elText = el.children[1]?.children[0]?.innerText;
const elSort = removeArticles(elText);
el.setAttribute('sort', elSort);
//console.log(elSort);
return elSort;
}
// Get list
console.log('Baka-Updates List Sorter');
const listTable = document.getElementById('list_table');
[...listTable.children]
.sort((a, b) => {
let asort = a.getAttribute('sort') || setSort(a);
let bsort = b.getAttribute('sort') || setSort(b);
if (asort > bsort) return 1;
if (asort < bsort) return -1;
return 0;
})
.forEach((node) => listTable.appendChild(node));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment