User:Ilmari Karonen/searchnsmenu.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/searchnsmenu. |
// SEARCH NAMESPACE MENU: <pre><nowiki>
addOnloadHook(function () {
var searchGoButton = document.getElementById('searchGoButton');
var searchInput = document.getElementById('searchInput');
if (!searchGoButton || !searchInput) return;
// Customize project name based on DB name suffix; may need further per-language customization
var siteName = 'Project'; // default name for ns:4
if (/^[a-z]{2,3}(_[_a-z0-9]*)?wiki$/.test(mw.config.get('wgDBname')))
siteName = 'Wikipedia';
else if (/wiktionary$/.test(mw.config.get('wgDBname')))
siteName = 'Wiktionary';
else if (/wiki(books|news|quote|source|versity)$/.test(mw.config.get('wgDBname')))
siteName = mw.config.get('wgDBname').replace(/^.*wiki/, 'Wiki');
// Standard namespaces, common to all MediaWiki wikis
var nameSpaces = {
'-1': '—namespace—', // -1 = NS_SPECIAL, but we use it as a placeholder
'0': '(Main)',
'1': 'Talk',
'2': 'User',
'3': 'User talk',
'4': siteName,
'5': siteName + ' talk',
'6': 'File',
'7': 'File talk',
'8': 'MediaWiki',
'9': 'MediaWiki talk',
'10': 'Template',
'11': 'Template talk',
'12': 'Help',
'13': 'Help talk',
'14': 'Category',
'15': 'Category talk'
};
// Localized default namespaces (per Messages*.php)
if (mw.config.get('wgContentLanguage') == 'fi') {
nameSpaces['-1'] = '—nimiavaruus—';
nameSpaces['0'] = '(Sivut)';
nameSpaces['1'] = 'Keskustelu';
nameSpaces['2'] = 'Käyttäjä';
nameSpaces['3'] = 'Keskustelu käyttäjästä';
// ns:4 and ns:5 may vary, override below as needed
nameSpaces['6'] = 'Kuva';
nameSpaces['7'] = 'Keskustelu kuvasta';
nameSpaces['8'] = 'Järjestelmäviesti';
nameSpaces['9'] = 'Keskustelu järjestelmäviestistä';
nameSpaces['10'] = 'Malline';
nameSpaces['11'] = 'Keskustelu mallineesta';
nameSpaces['12'] = 'Ohje';
nameSpaces['13'] = 'Keskustelu ohjeesta';
nameSpaces['14'] = 'Luokka';
nameSpaces['15'] = 'Keskustelu luokasta';
}
// Site-specific customizations
if (mw.config.get('wgDBname') == 'commonswiki') {
nameSpaces['0'] = '(Gallery)';
nameSpaces['4'] = 'Commons';
nameSpaces['5'] = 'Commons talk';
nameSpaces['100'] = 'Creator';
nameSpaces['101'] = 'Creator talk';
}
else if (mw.config.get('wgDBname') == 'metawiki') {
nameSpaces['4'] = 'Meta';
nameSpaces['5'] = 'Meta talk';
nameSpaces['100'] = 'Hilfe';
nameSpaces['101'] = 'Hilfe Diskussion';
nameSpaces['102'] = 'Aide';
nameSpaces['103'] = 'Discussion Aide';
nameSpaces['104'] = 'Hjælp';
nameSpaces['105'] = 'Hjælp diskussion';
nameSpaces['106'] = 'Helpo';
nameSpaces['107'] = 'Helpa diskuto';
nameSpaces['108'] = 'Hjälp';
nameSpaces['109'] = 'Hjälp diskussion';
nameSpaces['110'] = 'Ayuda';
nameSpaces['111'] = 'Ayuda Discusión';
nameSpaces['112'] = 'Aiuto';
nameSpaces['113'] = 'Discussioni aiuto';
nameSpaces['114'] = 'ヘルプ';
nameSpaces['115'] = 'ヘルプ‐ノート';
nameSpaces['116'] = 'NL Help';
nameSpaces['117'] = 'Overleg help';
nameSpaces['118'] = 'Pomoc';
nameSpaces['119'] = 'Dyskusja pomocy';
nameSpaces['120'] = 'Ajuda';
nameSpaces['121'] = 'Ajuda Discussão';
nameSpaces['122'] = 'CA Ajuda';
nameSpaces['123'] = 'CA Ajuda Discussió';
nameSpaces['124'] = 'Hjelp';
nameSpaces['125'] = 'Hjelp diskusjon';
nameSpaces['126'] = '帮助';
nameSpaces['127'] = '帮助 对话';
nameSpaces['128'] = 'Помощь';
nameSpaces['129'] = 'Помощь Дискуссия';
nameSpaces['130'] = 'Pomoč';
nameSpaces['131'] = 'Pogovor o pomoči';
nameSpaces['132'] = 'مساعدة';
nameSpaces['133'] = 'نقاش المساعدة';
}
if (mw.config.get('wgDBname') == 'mediawikiwiki') {
nameSpaces['100'] = 'Manual';
nameSpaces['101'] = 'Manual talk';
nameSpaces['102'] = 'Extension';
nameSpaces['103'] = 'Extension talk';
}
if (mw.config.get('wgDBname') == 'enwiki') {
nameSpaces['0'] = '(Article)';
nameSpaces['100'] = 'Portal';
nameSpaces['101'] = 'Portal talk';
}
if (mw.config.get('wgDBname') == 'fiwiki') {
nameSpaces['0'] = '(Artikkelit)';
nameSpaces['5'] = 'Keskustelu Wikipediasta';
nameSpaces['100'] = 'Teemasivu';
nameSpaces['101'] = 'Keskustelu teemasivusta';
nameSpaces['102'] = 'Metasivu';
nameSpaces['103'] = 'Keskustelu metasivusta';
}
// Localization ends here, actual code begins
var searchNsHidden = document.createElement('input');
searchNsHidden.id = 'searchNsHidden';
searchNsHidden.type = 'hidden';
searchNsHidden.name = 'defaultns';
searchNsHidden.value = '1';
var searchNsMenu = document.createElement('select');
searchNsMenu.id = 'searchNsMenu';
searchNsMenu.name = 'ns';
var nsOptions = []
for (i in nameSpaces) {
nsOptions[nsOptions.length] = new Option(nameSpaces[i], i);
}
nsOptions.sort(function (a,b) { return a.value - b.value; });
// can't sort searchNsMenu.options, can't assign an array directly to it, must do it like this:
for (var i = 0; i < nsOptions.length; i++) {
searchNsMenu.options[i] = nsOptions[i];
}
addHandler(searchNsMenu, "change", function () {
if (searchNsMenu.selectedIndex > 0) { // first element is placeholder
searchNsHidden.name = 'ns' + searchNsMenu.options[searchNsMenu.selectedIndex].value;
} else {
searchNsHidden.name = 'defaultns'; // dummy value, will be ignored
}
});
// Style menu with CSS to match current skin
appendCSS(
"body.skin-monobook #searchNsMenu { width: 10.9em; margin: 0; font-size: 95%; }\n" +
"body.skin-vector #searchNsMenu { margin:0.4em 0 0; }\n" +
"");
// Adjust placement depending on skin
var skinName = /(^| )skin-([^ ]+)/.exec(document.body.className);
skinName = (skinName ? skinName[2] : "unknown");
var nextElement = (skinName == "vector" ? searchInput : searchGoButton);
// Insert the menu
searchGoButton.parentNode.insertBefore(searchNsHidden, nextElement);
searchGoButton.parentNode.insertBefore(searchNsMenu, nextElement);
searchGoButton.parentNode.insertBefore(document.createTextNode(" "), nextElement);
// Kluge: make the "Go" button prepend the namespace
addHandler(searchGoButton, "click", function () {
if (searchNsMenu.selectedIndex > 1) { // first element is placeholder, second is ns:0
searchInput.value = searchNsMenu.options[searchNsMenu.selectedIndex].text + ":" + searchInput.value;
searchNsMenu.selectedIndex = 0; // reset, just in case
}
});
});
// </nowiki></pre>