Jump to content

User:Csewiki/vector.js

From Wikipedia, the free encyclopedia
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.
/* [[Wikipedia:Google Custom Search]] Gadget */
/* <source lang="javascript"><nowiki> */
var crefDomain = "googlecustomsearch.appspot.com/wikipedia/domain_spec.do?url=" + encodeURIComponent(window.location.href);
var crefUrl = "googlecustomsearch.appspot.com/wikipedia/spec.do?url=" + encodeURIComponent(window.location.href);
 
var tabbedSC;
 
function googleCustomSearchExecute() {
  tabbedSC.execute(document.getElementById('gSearchInput').value);
};
 
function googleCustomSearchClear() {
  tabbedSC.clearAllResults();
};
 
function createSearchResultFooter(div) {
  var bottomBorder = document.createElement('table');
  bottomBorder.className = 'gsc-resultsHeader';
  var borderBody = document.createElement('tbody');
  var bodyRow = document.createElement('tr');
  if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
    var bodyCol = document.createElement('td');
    bodyCol.innerHTML = '&nbsp;';
    bodyRow.appendChild(bodyCol);
  }
  borderBody.appendChild(bodyRow);
  bottomBorder.appendChild(borderBody);
  div.appendChild(bottomBorder);
}
 
var gcscPostSearch = {};
gcscPostSearch.uiAddOn = function() {
  if (!document.getElementById('gsc-close-x')) {
    var closeX = document.createElement('span');
    closeX.className = 'cse-clear-button';
    closeX.setAttribute('id', 'gsc-close-x');
    closeX.onclick = function(){googleCustomSearchClear(); return false;};
    closeX.appendChild(document.createTextNode('x'));
 
    // Add bottom border and x
    var divs = document.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
      if (divs[i].className == 'gsc-resultsRoot gsc-tabData gsc-tabdActive'
          || divs[i].className == 'gsc-resultsRoot gsc-tabData gsc-tabdInactive') {
        createSearchResultFooter(divs[i]);
      } else if (divs[i].className == 'gsc-tabsArea') {
        divs[i].appendChild(closeX);
      }
    }
 
    closeX.style.position = 'relative';
    closeX.style.left = '130px';
    closeX.style.padding = '0px';
    closeX.style.margin = '0px';
  }
};

function CSEonLoad() {
    addOnloadHook( CSEonLoad2 )
}

function CSEonLoad2() {
    var dResults = document.createElement("div");
    dResults.setAttribute("id", "search_control_tabbed");
    dResults.className = 'search-control';
    var bodyContent = document.getElementById("bodyContent");
    if (bodyContent.hasChildNodes()) {
        bodyContent.insertBefore(dResults, bodyContent.firstChild);
    } else {
        bodyContent.appendChild(dResults);
    }
 
    var iQ = document.createElement("input");
    iQ.setAttribute("name", "q");
    iQ.setAttribute("type", "text");
    iQ.setAttribute("size", 20);
    iQ.setAttribute("id", "gSearchInput");
    iQ.onkeydown = function(e) {
      var keyCode = 0;
      if (window && window.event && window.event.keyCode) {
        keyCode = window.event.keyCode;
      } else if (e && e.which) {
        keyCode = e.which;
      } 
      if (keyCode == 13) {
        googleCustomSearchExecute();
        return false;
      }
      return true;
    };
 
    var iSB = document.createElement("input");
    iSB.setAttribute("name", "sa");
    iSB.setAttribute("type", "button");
    iSB.setAttribute("value", "Search");
    iSB.setAttribute("id", "gSearchButton");
    iSB.onclick = googleCustomSearchExecute;
 
    var dClear = document.createElement("span");
    dClear.className = 'cse-clear-button';
    dClear.setAttribute("title", "clear results");
    dClear.onclick = googleCustomSearchClear;
    dClear.appendChild(document.createTextNode("Clear"));
 
    var fSearch = document.createElement("form");
    fSearch.setAttribute("id", "searchbox_cse");
    fSearch.appendChild(iQ);
    fSearch.appendChild(iSB);
    fSearch.appendChild(dClear);
 
    var scriptB = document.createElement("script");
    scriptB.setAttribute("type", "text/javascript");
    scriptB.setAttribute("src", "//www.google.com/coop/cse/brand?form=searchbox_cse");
 
    var dSBox = document.createElement("div");
    dSBox.setAttribute("id","p-search");
    dSBox.appendChild(fSearch);
    dSBox.appendChild(scriptB);
 
    var searchdiv = document.getElementById("p-search");
    var columndiv = searchdiv.parentNode;
 
    //replace the default search box with custom search 
    columndiv.replaceChild(dSBox,searchdiv);
 
    // create a tabbed mode search control
    tabbedSC = new GSearchControl();
 
    // Site Restrict to CSE ID
    var searcher = new GwebSearch();
    searcher.setSiteRestriction({crefUrl : crefDomain}, null, null);
    searcher.setUserDefinedLabel("All Wikipedia pages");
    tabbedSC.addSearcher(searcher);
 
    searcher = new GwebSearch();
    searcher.setSiteRestriction({crefUrl : crefUrl}, "internal", null);
    searcher.setUserDefinedLabel("Linked Wikipedia pages");
    tabbedSC.addSearcher(searcher);
 
    searcher = new GwebSearch();
    searcher.setSiteRestriction({crefUrl : crefUrl}, "external", null);
    searcher.setUserDefinedLabel("Linked non-Wikipedia pages");
    tabbedSC.addSearcher(searcher);
 
    tabbedSC.setResultSetSize(GSearch.LARGE_RESULTSET);
    tabbedSC.setLinkTarget(GSearch.LINK_TARGET_SELF);

 
    tabbedSC.setSearchCompleteCallback(gcscPostSearch, gcscPostSearch.uiAddOn);
 
    // draw in tabbed layout mode
    var drawOptions = new GdrawOptions();
    drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
    tabbedSC.draw(dResults, drawOptions);
 
    // Preload crefs
    function appendCSECrefPreloadImage(parent, url) {
      var preload_base = '//www.google.com/';
      var preload_path = 'cse/tools/ping?cref=';
      var img = document.createElement('img');
      img.src = preload_base + preload_path + encodeURIComponent(url);
      parent.appendChild(img);
    }
    var cse_preloader = document.createElement('div');
    cse_preloader.id = 'cse_cref_preloader';
    cse_preloader.style.display = 'none';
    appendCSECrefPreloadImage(cse_preloader, crefDomain);
    appendCSECrefPreloadImage(cse_preloader, crefUrl);
    document.getElementById('footer').appendChild(cse_preloader);
}
 
/* If it works on this skin, start the Google API...
  *  Autoloads the search module and a callback (CSEonLoad) will be executed when the module is done loading.
  *  The user interface language is also communicated
  */
var skin = mw.config.get('skin');
if( skin == "monobook" || skin == "vector" ) {
  mw.loader.load('//www.google.com/jsapi?autoload=%7B%22modules%22%3A%5B%7B%22name%22%3A%22search%22%2C%22version%22%3A%221%22%2C%22callback%22%3A%22CSEonLoad%22%2C%22language%22%3A%22'+ mw.config.get('wgUserLanguage')+'%22%7D%5D%7D');
  importStylesheetURI('//en.wikipedia.org/w/index.php?title=User:Csewiki/csewiki.css&action=raw&ctype=text/css');
}

/* </source></nowiki> */