Jump to content

User:Ilmari Karonen/betterwatchlist.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Make expandable watchlist entries in "show all applicable changes" mode look more like normal ones

if (wgCanonicalNamespace == "Special" && wgCanonicalSpecialPageName == "Watchlist") {
    addOnloadHook(function () {
        var i = 0;
        var toggleButton;
        while (toggleButton = document.getElementById("RCM" + (++i))) {

            var mainTable = toggleButton;
            while (mainTable && mainTable.tagName.toLowerCase() != "table")
                mainTable = mainTable.parentNode;
            if (!mainTable) continue; 
            var mainTD = mainTable.getElementsByTagName("td")[1];  // second column
            if (!mainTD) continue;

            var subDiv = document.getElementById("RCI" + i);
            if (!subDiv) continue;
            var subTR = subDiv.getElementsByTagName("tr")[0];  // first row
            if (!subTR) continue;
            var subTD = subTR.getElementsByTagName("td")[1];  // second column of first row
            if (!subTD) continue;

            var mainLinks = mainTD.getElementsByTagName("a");
            var mainSpans = mainTD.getElementsByTagName("span");
            var subLinks = subTD.getElementsByTagName("a");
            var subSpans = subTD.getElementsByTagName("span");
            if (!mainLinks || !mainSpans || !subLinks || !subSpans) continue;

            var totalBytes = mainSpans[0];
            var moreEditors = mainSpans[1];
            var allChanges = mainLinks[1];
            var histLink = mainLinks[2];
            var diffLink = subLinks[1];
            if (!totalBytes || !moreEditors || !allChanges || !histLink || !diffLink) continue;

            // TODO: check classes etc. to be sure we got what we wanted

            while (diffLink.nextSibling)
                moreEditors.parentNode.insertBefore(diffLink.nextSibling, moreEditors);

            moreEditors.parentNode.insertBefore(document.createElement("br"), moreEditors);
            moreEditors.parentNode.insertBefore(allChanges, moreEditors);
            moreEditors.parentNode.insertBefore(document.createTextNode(" . . "), moreEditors);
            moreEditors.parentNode.insertBefore(totalBytes, moreEditors);
            moreEditors.parentNode.insertBefore(document.createTextNode(" . . "), moreEditors);

            // remove some spacers (should really check that these are what we expect them to be):
            histLink.parentNode.removeChild(histLink.nextSibling);
            histLink.parentNode.removeChild(histLink.prevSibling);
            histLink.parentNode.removeChild(histLink.prevSibling);

            histLink.parentNode.insertBefore(diffLink, histlink);
            histLink.parentNode.insertBefore(document.createTextNode("; "), histlink);
            histLink.innerHTML = "hist";

            subTR.parentNode.removeChild(subTR);
        }
    });
}