Created
July 7, 2022 19:01
-
-
Save q00u/e5fb2bb2444e6f7528253959b4df26a8 to your computer and use it in GitHub Desktop.
This file contains 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 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