Jump to content

User:Ilmari Karonen/fixsearchcheckboxes.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.
/*
 * Put the namespace checkboxes at the bottom of the search page in a nice table
 * This is not a very robust script.  It is only intended as a proof-of-concept test,
 * and makes quite a lot of assumptions about the current markup.
 *
 * To try out this new layout, either add the line:
 *   importScript("User:Ilmari_Karonen/fixsearchcheckboxes.js");
 * to your /monobook.js (or equivalent page for other skins), or simply go to a search
 * page and cut-and-paste the URL:
 *   javascript:void(importScript("User:Ilmari_Karonen/fixsearchcheckboxes.js"));
 * to the address bar and hit enter.
 */

if (mw.config.get('wgCanonicalNamespace') == "Special" && mw.config.get('wgCanonicalSpecialPageName') == "Search") {
    addOnloadHook(function () {
        var mainNsCheckbox = document.getElementById("mw-search-ns0");
        if (!mainNsCheckbox) return;
        var span = mainNsCheckbox.parentNode;
        if (!span || span.tagName.toLowerCase() != "span") return;
        var para = span.parentNode;
        if (!para || para.tagName.toLowerCase() != "p") return;

        var spans = [];
        while (span) {
            if (span.nodeType == 1 && span.tagName.toLowerCase() == "span")
                spans[spans.length] = span;
            span = span.nextSibling;
        }
        // FIXME: this assumes that namespaces always come in pairs
        var cbreak = 6; // 2 * Math.ceil(spans.length / 6);
        if (cbreak == 0) cbreak = spans.length;  // just in case

        var tables = [];
        var table;
        for (var i = 0; i < spans.length; i ++) {
            if (i % cbreak == 0)
                table = tables[tables.length] = document.createDocumentFragment();
            if (i % 2 == 0)
                table.appendChild(document.createElement("tr"));
            var cell = document.createElement("td");
            cell.appendChild(spans[i]);
            table.lastChild.appendChild(cell);
        }

        var hr = document.createElement("hr");
        hr.style.clear = "both";
        para.parentNode.insertBefore(hr, para.nextSibling);

        for (var i = tables.length - 1; i >= 0; i--) {
            var table = document.createElement("table");
            table.style.cssFloat = "left";
            table.style.margin = "0 2em 1em 0";
            table.appendChild(tables[i]);
            para.parentNode.insertBefore(table, para.nextSibling);
        }
    });
}