Jump to content

User:Ilmari Karonen/fixsearchcheckboxes.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ilmari Karonen (talk | contribs) at 20:01, 31 October 2008 (try cbreak = 2). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.

if (wgCanonicalNamespace == "Special" && 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 = 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 br = document.createElement("br");
        br.style.clear = "both";
        para.parentNode.insertBefore(br, 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);
        }
    });
}