User:Ilmari Karonen/fixsearchcheckboxes.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Ilmari Karonen/fixsearchcheckboxes. |
/*
* 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);
}
});
}