User:Visviva/Bookmarklets/AFDrelists.js

javascript: (function {   const root = document.getElementsByClassName("mw-parser-output")[0];    const ccc = Array.from(root.children);    const sections = [];    var currentSection = ;    var skipSection = false;    var sectionLevel = "h3";    var i = 0;    title = document.title;    if (title.startsWith("Wikipedia:Categories") || title.startsWith("Wikipedia:Redirects") || title.startsWith("Wikipedia:Templates") || title.startsWith("Wikipedia:Miscellany")) {       sectionLevel = "h4";    }    function getCount(elem) {        var count = elem.getElementsByClassName("xfd_relist").length;        return count;    }    function closeSection {        if (currentSection != ) {            if (currentSection.getElementsByClassName("xfd_relist").length != 0) {                sections.push(currentSection);            }        }        currentSection = '';    }    for (i = 0; i < ccc.length; i++) { var elem = ccc[i]; var tag = elem.tagName.toLowerCase; var classList = Array.from(elem.classList); if (tag == sectionLevel) { console.log(elem.textContent); console.log(skipSection); closeSection; currentSection = document.createElement("div"); skipSection = false; }       if (classList.includes("xfd-closed")) { closeSection; skipSection = true; }       if (skipSection) { elem.remove; continue; }       if (currentSection != '' && !skipSection) { currentSection.appendChild(elem); } else { continue; }   }    closeSection; sections.sort((a, b) => getCount(b) - getCount(a)); for (const sec of sections) { root.appendChild(sec); }   const toc = document.getElementById("toc").getElementsByTagName("ul")[0]; const lines = toc.getElementsByTagName("li"); const newLines = Array; const removers = []; for (i = 0; i < lines.length; i++) { var line = lines[i]; var findMe = line.getElementsByTagName("a")[0].href.split("#")[1]; var foundIt = document.getElementById(findMe); if (foundIt == null) { removers.push(line); continue; }       var tocTextElem = line.getElementsByClassName("toctext")[0]; var tocText = tocTextElem.innerHTML; var parentDiv = foundIt.parentElement.parentElement; var count = getCount(parentDiv).toString; var newtext = tocText + " (" + count + " relist";       if (count != 1) {            newtext += "s";        }        newtext += ")"; tocTextElem.textContent = newtext; line.relistCount = parseInt(count); newLines.push(line); }   for (var remover of removers) { remover.remove; }   newLines.sort((a, b) => b.relistCount - a.relistCount); for (i = 0; i < newLines.length; i++) { sortedLine = newLines[i]; tocNumber = sortedLine.getElementsByClassName("tocnumber")[0]; tocNumber.textContent = (i + 1).toString; toc.appendChild(sortedLine); } });