MediaWiki:Common.js/search.js: Difference between revisions
(replacing deprecated global with mw.config variable) |
No edit summary |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
* Description: Change Special:Search to use a drop-down menu, with the default being | * Description: Change Special:Search to use a drop-down menu, with the default being | ||
* the internal MediaWiki engine | * the internal MediaWiki engine | ||
* | * Copied from https://en.wikipedia.org/wiki/MediaWiki:Gadget-externalsearch.js | ||
*/ | */ | ||
| Line 10: | Line 10: | ||
var opt = document.createElement('option'); | var opt = document.createElement('option'); | ||
opt.appendChild(document.createTextNode(site)); | opt.appendChild(document.createTextNode(site)); | ||
searchEngines.push([action, mainQ, addQ, addV]); | window.searchEngines.push([action, mainQ, addQ, addV]); | ||
return opt; | return opt; | ||
} | } | ||
var topline = document. | |||
topline.style.color = "blue"; | var topline = document.getElementsByClassName('mw-body-subheader'); | ||
topline.innerText = "Not finding what you're looking for? Select | topline[0].style.color = "blue"; | ||
if (document.forms | topline[0].innerText = "Not finding what you're looking for? Select a different search engine from the dropdown menu below and try again."; | ||
var searchForm = document.forms | |||
if (document.forms | if (document.forms.powersearch) | ||
var searchForm = document.forms | var searchForm = document.forms.powersearch; | ||
if (document.forms.search) | |||
var searchForm = document.forms.search; | |||
if (searchForm.lsearchbox) { | if (searchForm.lsearchbox) { | ||
| Line 29: | Line 31: | ||
selectBox.id = 'searchEngine'; | selectBox.id = 'searchEngine'; | ||
searchForm.onsubmit = function() { | searchForm.onsubmit = function() { | ||
var optSelected = searchEngines[document.getElementById('searchEngine').selectedIndex]; | var optSelected = window.searchEngines[document.getElementById('searchEngine').selectedIndex]; | ||
searchForm.action = optSelected[0]; | searchForm.action = optSelected[0]; | ||
searchBox.name = optSelected[1]; | searchBox.name = optSelected[1]; | ||
| Line 35: | Line 37: | ||
searchForm.title.name = optSelected[2]; | searchForm.title.name = optSelected[2]; | ||
} | } | ||
selectBox.appendChild(createOption('MediaWiki Search', mw.config.get(' | selectBox.appendChild(createOption('MediaWiki Search', mw.config.get( 'wgScript' ), 'search', 'title', 'Special:Search')); | ||
selectBox.appendChild(createOption('Google', ' | selectBox.appendChild(createOption('Google', 'https://www.google.com/search', 'q', 'sitesearch', 'wiki.oni2.net')); | ||
selectBox.appendChild(createOption('Yahoo!', ' | selectBox.appendChild(createOption('Yahoo!', 'https://search.yahoo.com/search', 'p', 'vs', 'wiki.oni2.net')); | ||
selectBox.appendChild(createOption('Bing', ' | selectBox.appendChild(createOption('Bing', 'https://www.bing.com/search', 'q', 'q1', 'site:https://wiki.oni2.net')); | ||
searchBox.style.marginLeft = '0px'; | searchBox.style.marginLeft = '0px'; | ||
if (document.getElementById('loadStatus')) { | if (document.getElementById('loadStatus')) { | ||
| Line 44: | Line 46: | ||
} else { | } else { | ||
var lStat = searchForm.title; | var lStat = searchForm.title; | ||
if( typeof lStat == 'object' && typeof lStat.length === 'number' ) lStat = lStat[0]; | if( typeof lStat === 'object' && typeof lStat.length === 'number' ) lStat = lStat[0]; | ||
} | } | ||
lStat.parentNode.insertBefore(selectBox, lStat); | lStat.parentNode.insertBefore(selectBox, lStat); | ||
} | } | ||
window.searchEngines = []; | |||
$(SpecialSearchEnhanced); | // Script is specific to Special:Search | ||
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search'){ | |||
$(SpecialSearchEnhanced); | |||
} | |||
Latest revision as of 03:41, 28 December 2025
/** Change Special:Search to use a drop-down menu *******************************************************
*
* Description: Change Special:Search to use a drop-down menu, with the default being
* the internal MediaWiki engine
* Copied from https://en.wikipedia.org/wiki/MediaWiki:Gadget-externalsearch.js
*/
function SpecialSearchEnhanced() {
var createOption = function(site, action, mainQ, addQ, addV) {
var opt = document.createElement('option');
opt.appendChild(document.createTextNode(site));
window.searchEngines.push([action, mainQ, addQ, addV]);
return opt;
}
var topline = document.getElementsByClassName('mw-body-subheader');
topline[0].style.color = "blue";
topline[0].innerText = "Not finding what you're looking for? Select a different search engine from the dropdown menu below and try again.";
if (document.forms.powersearch)
var searchForm = document.forms.powersearch;
if (document.forms.search)
var searchForm = document.forms.search;
if (searchForm.lsearchbox) {
var searchBox = searchForm.lsearchbox;
} else {
var searchBox = searchForm.search;
}
var selectBox = document.createElement('select');
selectBox.id = 'searchEngine';
searchForm.onsubmit = function() {
var optSelected = window.searchEngines[document.getElementById('searchEngine').selectedIndex];
searchForm.action = optSelected[0];
searchBox.name = optSelected[1];
searchForm.title.value = optSelected[3];
searchForm.title.name = optSelected[2];
}
selectBox.appendChild(createOption('MediaWiki Search', mw.config.get( 'wgScript' ), 'search', 'title', 'Special:Search'));
selectBox.appendChild(createOption('Google', 'https://www.google.com/search', 'q', 'sitesearch', 'wiki.oni2.net'));
selectBox.appendChild(createOption('Yahoo!', 'https://search.yahoo.com/search', 'p', 'vs', 'wiki.oni2.net'));
selectBox.appendChild(createOption('Bing', 'https://www.bing.com/search', 'q', 'q1', 'site:https://wiki.oni2.net'));
searchBox.style.marginLeft = '0px';
if (document.getElementById('loadStatus')) {
var lStat = document.getElementById('loadStatus');
} else {
var lStat = searchForm.title;
if( typeof lStat === 'object' && typeof lStat.length === 'number' ) lStat = lStat[0];
}
lStat.parentNode.insertBefore(selectBox, lStat);
}
window.searchEngines = [];
// Script is specific to Special:Search
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search'){
$(SpecialSearchEnhanced);
}