User:Writ Keeper/Scripts/SearchNamespace.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:Writ Keeper/Scripts/SearchNamespace. |
// Allows the user to select a namespace to search in from the corner search bar
prefixString =
"<option label='(article)'></option>\n <option>Talk:</option>\n <option label='WP'>Wikipedia:</option>\n <option labl='WP talk:'>Wikipedia talk:</option>\n <option>User:</option>\n <option>User talk:</option>\n<option>Template:</option>\n <option>Template talk:</option>\n <option>Help:</option>\n <option>Special:</option>";
addOnloadHook(addNSSearch);
function addNSSearch()
{
//retrieve existing elements that we're going to use
searchBar = document.getElementById("searchInput");
searchButton = document.getElementById("searchButton");
searchForm = document.getElementById("searchform");
searchDiv = document.getElementById("simpleSearch");
//create new elements that we're going to insert
// prefixDiv = document.createElement("div");
prefixList = document.createElement("select");
hiddenInput = document.createElement("input");
//initialize new elements
// prefixDiv.id = "prefixDiv";
prefixList.id = "prefixList";
prefixList.innerHTML = prefixString;
hiddenInput.id = "hiddenInput";
hiddenInput.name = "search";
hiddenInput.type = "hidden";
//insert new elements
searchDiv.insertBefore(hiddenInput, searchBar);
searchForm.insertBefore(prefixList, searchDiv);
//modify old ones
document.getElementById
searchForm.addEventListener("onsubmit", createSearchTerm, false); //set onsubmit handler to concatenate selected prefix and entered search term
searchBar.removeAttribute("name"); //since we're no longer using the search bar to submit the search terms directly
searchDiv.style.display = "inline-block";
prefixList.style.marginTop = "0.65em";
}
//onsubmit handler; concatenates prefixList and searchBar and inserts into hidden input prior to submission
function createSearchTerm()
{
//retrieve used elements
hiddenInput = document.getElementById("hiddenInput");
prefixList = document.getElementById("prefixList");
searchBar = document.getElementById("searchInput");
//do the work
hiddenInput.value = "" + prefixList.value + searchBar.value;
}