MediaWiki:Gadget-SpecialSearch.js

// /** Change Special:Search to use a radio menu * * Description: Change Special:Search to use a radio menu, with the default being *              the internal MediaWiki engine * Created by: fr:User:Pmartin */

if ((mw.config.get("wgNamespaceNumber") == -1) && (mw.config.get("wgCanonicalSpecialPageName") == "Search")) { var searchEngines = { mediawiki: { ShortName: "MediaWiki search", Template: mw.config.get("wgScript") + "?search={searchTerms}" },		google: { ShortName: "Google", Template: "https://www.google.com/search?q=site:" + mw.config.get("wgServer").replace(/^.*?\/\//, '') + "&q={searchTerms}" },		live: { ShortName: "Bing", Template: "https://www.bing.com/search?q1=site:" + mw.config.get("wgServer").replace(/^.*?\/\//, '') + "&q={searchTerms}" },		yahoo: { ShortName: "Yahoo", Template: "https://search.yahoo.com/search?p={searchTerms}&vs=" + mw.config.get("wgServer").replace(/^.*?\/\//, '') }	};	$(externalSearchEngines); }

function externalSearchEngines {	var mwSearchTopTable = $('#mw-search-top-table'); if (mwSearchTopTable.length === 0) return;

var firstEngine = "mediawiki"; var choices = $(' ').attr("id", "searchengineChoices").css("clear", "left").css("padding-top", "0.3em");

for (var id in searchEngines) { var engine = searchEngines[id]; if (engine.ShortName) { choices.append(" " );

$(' ')				.attr("for", "searchengineRadio-" + id) .append(					$(' ')					.attr("type", "radio")					.val(id)					.data("engine-name", id)					.on("focus", changeSearchEngine)					.attr("id", "searchengineRadio-" + id)				) .append(" " + engine.ShortName) .appendTo(choices); }	}

$(' ').attr("id", "searchengineextraparam").appendTo(choices); $(choices).insertAfter(mwSearchTopTable);

$("[id^='searchengineRadio-']").click(function {		$("[id^='searchengineRadio-']").not(this).prop('checked', false); 	});

$("#searchengineRadio-" + firstEngine).prop("checked", true); }

function changeSearchEngine {	$("#searchengineextraparam").removeAttr("name"); var radio = $(this).prop("checked", true); var selectedEngineName = radio.data("engine-name"); if (selectedEngineName == $("#searchengineChoices").data("currentChoice")) return;

$("#searchengineChoices").data("currentChoice", selectedEngineName);

var engine = searchEngines[selectedEngineName]; var p = engine.Template.indexOf('?'); var params = engine.Template.substr(p + 1);

var form; if (document.forms["search"]) { form = document.forms["search"]; } else { form = document.getElementById("powersearch"); }	form.setAttribute("action", engine.Template.substr(0, p));

var l = ("" + params).split("&"); for (var i in l) { var pName = l[i].split("=")[0]; var pValue = l[i].split("=")[1];

if (pValue == "{language}") {} else if (pValue == "{searchTerms}") { var input; if (document.forms["search"]) { input = document.getElementById("searchText"); } else { input = document.getElementById("ooui-php-1") || form.querySelectorAll("[type='search']")[0]; }

if (input) { if (input.tagName.toUpperCase != "INPUT") { input = input.getElementsByTagName("input")[0]; }				input.name = pName; }		} else { $("#searchengineextraparam").attr("name", pName).val(pValue); }	}	$(form).find("input:not(#searchengineextraparam):not([type='search']):not([id^='searchengineRadio-']").prop("disabled", selectedEngineName != "mediawiki"); }

// == Drop-down language preload menu for MediaWiki:Searchmenu-new ==

function addNogoPreloadMenu { var preloadGuide = document.getElementById('preloadGuide'); if (preloadGuide) { preloadGuide.style.display = 'inline-block'; var menu = ""; menu += " English "; var languages = document.getElementsByClassName("searchmenu-language-name"); for (var i = 0; i < languages.length; ++i) { var languageName = languages[i].textContent.trim; if (languageName[languageName.length - 1] == ":") { languageName = languageName.substring(0, languageName.length - 1); }			menu += " " + languageName + " "; }		menu += " "; var menuDiv = document.getElementById('entryTemplateMenu'); menuDiv.innerHTML = menu; showPreloads(0); } } $(addNogoPreloadMenu);

window.showPreloads = function (selectedIndex) { var languageOptions = document.getElementById('preloadGuide').getElementsByTagName('table'); for (var i = 0; i < languageOptions.length; i++) { if (languageOptions[i].className == "language") { languageOptions[i].style.display = i == selectedIndex ? 'block' : 'none'; }	} };