User:Alex Smotrov/searchlist.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:Alex Smotrov/searchlist. |
function searchList(){
if (!window.searchListInNewWin) searchListInNewWin = false
var newWinOption
var msgThisWin, msgNewWin = 'NEW window'
var msgSelectTitle = '' //'Choose namespace to search in'
if (!window.searchListNS)
searchListNS = [
['','Find in…'],
[-5,'this window', 'Select whether to open search in a new window'], //value -5 is special
['','External'],
['http://www.google.com/custom?domains=en.wikipedia.org&sitesearch=en.wikipedia.org&q=', 'Google en.wp', 'Google: search English Wikipedia'],
['http://www.google.com/custom?domains=wikipedia.org&sitesearch=wikipedia.org&q=', 'Google wp', 'Google: search Wikipedia'],
['','MediaWiki'],
[-1,'Default search', 'Search in namespace(s) defined in your preferences'], //value -1 is special
[0,'Article'],
[1,'Talk'],
[2,'User'],
[3,'User talk'],
[4,'Wikipedia'],
[5,'Wikipedia talk'],
[6,'Image'],
[7,'Image talk'],
[8,'Mediawiki'],
[9,'Mediawiki talk'],
[10,'Template'],
[11,'Template talk'],
[12,'Help']
]
if (!window.searchListShortNS)
searchListShortNS = {
'u':'User',
'i':'Image',
't':'Template',
'c':'Category',
'me':'MediaWiki'
}
var searchForm = document.getElementById('searchform')
if (!searchForm) return
//create namespace selector hidden input
var searchHidden = document.createElement('input')
searchHidden.type = 'hidden'
searchHidden.value = '1'
searchForm.fulltext.parentNode.appendChild(searchHidden)
//create select
var select = document.createElement('select'), item, opt, text
for (var i=0; i<searchListNS.length; i++){
item = searchListNS[i]
opt = new Option(item[1], item[0])
opt.title = item[2] || ''
if (i == 0)
opt.style.color = '#222222'
else if (opt.value == -5){
newWinOption = opt
msgThisWin = opt.text
}else if (opt.value == ''){
opt.disabled = true
opt.style.color = 'gray'
opt.style.paddingLeft = '10px'
}else
opt.txt = ' ' + opt.txt
select.options[select.options.length] = opt
}
//select.selectedIndex = 0
with (select.style){ margin = '0'; marginTop = '2px'; fontSize = '90%'}
setTarget()
searchForm.fulltext.parentNode.insertBefore(select, searchHidden)
//hide standard Search button if not IE //if (is_opera || is_gecko){
if (navigator.userAgent.indexOf('MSIE') == -1){
searchForm.fulltext.style.display = 'none'
searchForm.go.title = searchForm.go.value
searchForm.go.value = ' → '
searchForm.go.style.width = '30px'
select.style.width = '90px'
}
select.title = msgSelectTitle
searchForm.go.title += ' (' + (is_gecko||is_safari?'Ctrl':'Shift') + ': ' + msgNewWin + ')'
//events
addHandler(searchForm.search, 'keydown', detectShift) //if Enter pressed
addHandler(searchForm.go, 'mouseup', detectShift) //Go clicked
select.onchange = onSelect
//addHandler(select, 'click', detectShift)
addHandler(searchForm, 'submit', shortNS)
return
function detectShift(e){ //open in new win if shift is pressed
e = e || window.event
searchForm.target = (searchListInNewWin || e.shiftKey
|| (is_gecko||is_safari) && e.ctrlKey) ? '_blank' : ''
}
function onSelect(e){
if (this.selectedIndex == 0 || this.options[this.selectedIndex].disabled){
this.selectedIndex = 0
return
}
var val = this.options[this.selectedIndex].value
this.selectedIndex = 0
if (! /^-?\d+$/.test(val)){ //not a number => external search engine
var url = val + encodeURIComponent(searchForm.search.value)
if (searchListInNewWin) window.open(url)
else window.location = url
}else if (val == -5){ //new window switch
searchListInNewWin = !searchListInNewWin
setTarget()
}else if (searchForm.search.value == ''){
searchForm.search.value = '?'
}else { //MediaWiki search
searchHidden.name = (val == -1) ? '' : 'ns' + val
detectShift(e) //doesn't work in FF/Safari anyway
searchForm.fulltext.click()
}
}
function setTarget(){
searchForm.target = searchListInNewWin ? '_blank' : ''
select.style.backgroundColor = searchListInNewWin ? '#F5F5FF' : ''
newWinOption.text = ' (' + (searchListInNewWin ? msgNewWin : msgThisWin) + ')'
}
function shortNS(){ //expands 'u:test' into 'User:test'
var txt = searchForm.search.value, k
if ((k=txt.indexOf(':'))==-1 || txt.substring(0,1) == ' ') return
var pref = txt.substring(0,k).toLowerCase()
if (searchListShortNS[pref])
searchForm.search.value = searchListShortNS[pref] + txt.substring(k)
return true
}
}//searchList func
if (doneOnloadHook)searchList()
else addOnloadHook(searchList)