User:Od1n/MediaWiki:SpecialSearch.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


/** 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 ((wgNamespaceNumber == -1) && (wgCanonicalSpecialPageName == "Search")) {
    var searchEngines = {
        mediawiki: {
            ShortName: "MediaWiki search",
            Template: wgScript+"?search={searchTerms}"
        },
        google: {
            ShortName: "Google",
            Template: "http://www.google.com/search?hl=" + wgUserLanguage + "&q={searchTerms}&as_sitesearch=" + wgServer.replace(/^.*?\/\//, '')
        },
        wikiwix: {
            ShortName: "Wikiwix",
            Template: "http://www.wikiwix.com/index.php?action={searchTerms}&disp=dict"
        },
        live: {
            ShortName: "Bing",
            Template: "http://search.live.com/results.aspx?q={searchTerms}&q1=site:" + wgServer.replace(/^.*?\/\//, '')
        },
        yahoo: {
           ShortName: "Yahoo",
           Template: "http://search.yahoo.com/search?p={searchTerms}&vs=" + wgServer.replace(/^.*?\/\//, '')
       }
    };
    $(externalSearchEngines);
}


function externalSearchEngines() {

    if (typeof SpecialSearchEnhanced2Disabled != 'undefined') return;

    var mwSearchTopTable = document.getElementById('mw-search-top-table');
    if (!mwSearchTopTable) return;

    var firstEngine = "mediawiki";

    var lsearchbox = document.getElementById("searchText");
    var initValue = lsearchbox.value;

    var space = "";

    var choices = newNode('div', {id: "searchengineChoices", style: "clear:left; padding-top:0.3em"})

    for (var id in searchEngines) {
        var engine = searchEngines[id];
        if(engine.ShortName)
        {
            if (space) choices.appendChild(space);
            space = document.createTextNode(" ");

            choices.appendChild( newNode('span', 
                newNode('input', 
                    {type: "radio", 
                     name: "searchengineselect", 
                     value: id, 
                     onFocus: "changeSearchEngine('"+id+"');", 
                     id: "searchengineRadio-"+id}
                )
            ) );

            choices.appendChild( newNode('label', {"for":"searchengineRadio-"+id}, engine.ShortName) )
        }
    }

    choices.appendChild(newNode('input', {id: "searchengineextraparam", type: "hidden"}));
    $(choices).insertAfter(mwSearchTopTable);

    changeSearchEngine(firstEngine, initValue);
}

function changeSearchEngine(selectedId) {

    var currentId = document.getElementById("searchengineChoices").currentChoice;
    if (selectedId == currentId) return;

    document.getElementById("searchengineChoices").currentChoice = selectedId;
    var radio = document.getElementById('searchengineRadio-'  + selectedId);
    radio.checked = "checked";

    var engine = searchEngines[selectedId];
    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 p = l[i].split("=");
        var pValue = p[1];

        if (pValue == "{language}") {
        } else if (pValue == "{searchTerms}") {
            var input;
            if (document.forms["search"]) {
                input = document.getElementById("searchText");
            } else {
                input = document.getElementById("powerSearchText");
            } 

            if (input.tagName.toUpperCase() != "INPUT") {
                input = input.getElementsByTagName("input")[0];
            }

            input.name = p[0];
        } else {
            var input = document.getElementById("searchengineextraparam");

            input.name = p[0];
            input.value = pValue;
        }
    }
}

/*</pre>
==Drop-down language preload menu for [[MediaWiki:Searchmenu-new]]==
<pre>*/

 function addNogoPreloadMenu() {
  var preloadGuide = document.getElementById('preloadGuide');
  if (preloadGuide) {
   preloadGuide.style.display = 'inline-block';
   var menu = "<select style=\"float: left; display: inline-block; margin: 0 0 0.4em 0.5em;\" onChange=\"showPreloads(selectedIndex)\">";
   menu += "<option>English</option>";
   menu += "<option>American Sign Language</option>";
   menu += "<option>Spanish</option>";
   menu += "<option>Swedish</option>";
   menu += "</select>";
   var menuDiv = document.getElementById('entryTemplateMenu');
   menuDiv.innerHTML = menu;
   showPreloads(0);
  }
 }
 $(addNogoPreloadMenu);

 function showPreloads(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';
    }
  }
 }