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 19:42, 31 October 2008 (okay, time to test it). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 * int((spans.length + 2) / 4);
        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);
        }

        while (para.lastCell) {
            if (para.lastCell.nodeType == 3 && !/\S/.test(para.lastCell.nodeValue))
                para.removeNode(para.lastCell);
            else if (para.lastCell.nodeType == 1 && para.lastCell.tagName.toLowerCase() == "br")
                para.removeNode(para.lastCell);
            else break;
        }

        if (para.nextSibling)
            para.nextSibling.clear = "left";

        for (var i = tables.length; i > 0; i--) {
            var header = document.createElement("th");
            header.setAttribute("colspan", 2);
            header.appendChild(para.cloneNode(true));

            var row0 = document.createElement("tr");
            row0.appendChild(header);
            tables[i].insertBefore(row0, tables[i].firstChild);

            var table = document.createNode("table");
            table.cssFloat = "left";
            table.appendChild(tables[i]);
            para.parentNode.insertBefore(table, para.nextSibling);
        }
    });
}