User:Magnus Manske/googlesearch.js
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
addOnloadHook ( addgooglesearch ) ;
function addgooglesearch () {
var sb = document.getElementById('searchform') ;
if ( skin == "standard" ) {
sb = document.getElementById('topbar') ;
sb = sb.getElementsByTagName('form')[0] ;
}
var d = sb.getElementsByTagName('div') ;
if ( d.length > 0 ) d = d[0] ;
else d = sb ; // Other skins
var googlebutton = document.createElement('input') ;
googlebutton.id = 'googlesearchbutton' ;
googlebutton.className = 'searchButton' ;
googlebutton.type = 'button' ;
googlebutton.onclick = rungooglesearch ;
googlebutton.value = 'Google search' ;
googlebutton.name = 'googlesearch' ;
d.appendChild ( googlebutton ) ;
}
function rungooglesearch () {
var s ;
if ( skin == "standard" ) {
s = document.getElementById('topbar') ;
s = s.getElementsByTagName('form')[0] ;
s = s.getElementsByTagName('input')[0] ;
s = s.value ;
} else s = document.getElementById('searchInput').value ;
s += " site:" + mw.config.get('wgServer') ;
s = encodeURIComponent ( s ) ;
s = "http://www.google.co.uk/search?ie=utf-8&oe=utf-8&q=" + s ;
location.href = s ;
}