Jump to content

User:Tom-/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tom- (talk | contribs) at 23:55, 25 December 2004. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
document.attachEvent("onreadystatechange", function() {
  var deleteItem = document.getElementById("ca-delete");
  
  if (deleteItem)
  {
    var deleteLink = deleteItem.getElementsByTagName("a")[0]
    if (deleteLink)
    {
      deleteLink.accessKey = "";
      setTimeout(function(){ deleteLink.accessKey = ""; }, 500);
    }
  }
 }
);


var typePause = 0;


var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
var searchCache = {};
var searchStr;
var searchEl;
var searchTimeout;
var resultCount;
var resultsEl;

function getResults()
{
  var encStr = escape(searchStr.replace(/ /g, '_'));
  xmlHttp.open("GET", "/wiki/Special:Search?gen=titlematch&ns0=0&limit=10&search=" + encStr, true);
  xmlHttp.onreadystatechange = parseResults;
  xmlHttp.send(null);  
}

function parseResults()
{
  if (xmlHttp.readyState > 3)
  {
    var resultArr = xmlHttp.responseText.replace(/^$/gm, '').split("\n");
    searchCache[searchStr.toLowerCase()] = resultArr;
    showResults(resultArr);
  }
}

function showResults(resultArr)
{
  var returnStr = "";
  var resultsEl = searchEl.suggestList;

  if (resultArr.length < 1)
    resultsEl.style.display = "none";
  else
  {
    resultsEl.innerHTML = "";

    for (var i=0; i < resultArr.length-1; i++)
    {  
      var linkEl = document.createElement("a");
      linkEl.style.display = "block";
      linkEl.style.textDecoration = "none";
      linkEl.style.color = "WindowText";
      linkEl.style.padding = "1px 3px";
      linkEl.href = "http://80.3.244.236/w/" + resultArr[i];
      linkEl.onmouseover = function(e)
      {
        var srcEl = e ? e.target : event.srcElement;
        srcEl.style.backgroundColor = "Highlight";
        srcEl.style.color = "HighlightText";
      }
      linkEl.onmouseout = function(e)
      {
        var srcEl = e ? e.target : event.srcElement;
        srcEl.style.backgroundColor = "Window";
        srcEl.style.color = "WindowText";
      }

      if (resultsEl.curSel == i)
      {
        linkEl.style.backgroundColor = "Highlight";
        linkEl.style.color = "HighlightText";
      }


      var textEl = document.createTextNode(resultArr[i].replace(/_/g, ' '));

      var listItemEl = document.createElement("li");
      listItemEl.style.padding = 0;
      listItemEl.style.margin = 0;
      listItemEl.appendChild(linkEl);
      linkEl.appendChild(textEl);
      resultsEl.appendChild(listItemEl);
    }

    resultsEl.style.display = "block";
    resultsEl.style.top = searchEl.offsetHeight + "px";
    resultsEl.style.left = 0;
    searchEl.parentNode.style.position = "relative";

    searchEl.onblur = function()
    {
      searchEl.suggestList.style.display = "none";
      searchEl.suggestList.parentNode.style.position = "static";
    }
  }

  // set width, top, left
}

function resultType()
{
  searchStr = searchEl.value;
  if (searchTimeout) clearTimeout(searchTimeout);

  if (searchStr != "")
  {
    if (searchCache[searchStr.toLowerCase()])
      showResults(searchCache[searchStr.toLowerCase()])
    else
      searchTimeout = setTimeout(getResults, typePause);
  }  
  else
  {
    searchEl.suggestList.style.display = "none";
  }
}

document.onkeyup = function(e)
{
  var srcEl = e ? e.target : event.srcElement;

  if (srcEl.tagName.toLowerCase() == "input" && srcEl.name == "search" && srcEl.type == "text")
  {
    if (!srcEl.wikiTransform)
    {
      srcEl.autocomplete = "off";
      srcEl.wikiTransform = true;

      var listEl = document.createElement("ul");
      listEl.style.margin = 0;
      listEl.style.padding = 0;
      listEl.style.listStyleType = "none";
      listEl.style.listStyleImage = "none";
      listEl.style.display = "none";
      listEl.style.position = "absolute";
      listEl.style.border = "1px solid #000";
      listEl.style.overflow = "hidden";
      listEl.style.font = "menu";
      listEl.style.color = "WindowText";
      listEl.style.whiteSpace = "nowrap";
      listEl.style.cursor = "default";
      listEl.style.backgroundColor = "window";
      listEl.style.zIndex = 500;
      listEl.curSel = -1;
      srcEl.parentNode.insertBefore(listEl, srcEl.nextSibling);
      srcEl.suggestList = listEl;
      
      /*var posEl = document.createElement("div");
      posEl.style.position = "relative";
      posEl.appendChild(srcEl);
      srcEl.parentNode.replaceChild(posEl, srcEl);*/
    }

    searchEl = srcEl;
    resultType();    
  }
}

document.onkeydown = function(e)
{
  var srcEl = e ? e.target : event.srcElement;
  var keyCode = e ? e.charCode : event.keyCode;

  if (srcEl.tagName.toLowerCase() == "input" && srcEl.name == "search" && srcEl.type == "text")
  {
    switch (keyCode)
    {
      case 40 : // down

        var allLinks = srcEl.suggestList.getElementsByTagName("a");

        if (srcEl.suggestList.curSel < 0)
        {
          srcEl.suggestList.curSel = 0;
          allLinks[0].style.backgroundColor = "Highlight";
          allLinks[0].style.color = "HighlightText";
          break;
        }
        else if (srcEl.suggestList.curSel + 1 < allLinks.length)
        {
          allLinks[srcEl.suggestList.curSel].style.backgroundColor = "";
          allLinks[srcEl.suggestList.curSel].style.color = "";
          allLinks[++srcEl.suggestList.curSel].style.backgroundColor = "Highlight";
          allLinks[srcEl.suggestList.curSel].style.color = "HighlightText";
        }

       
        break;
      case 38 : // up

        var allLinks = srcEl.suggestList.getElementsByTagName("a");

        if (srcEl.suggestList.curSel < 0)
        {
          srcEl.suggestList.curSel = allLinks.length - 1;
          allLinks[allLinks.length - 1].style.backgroundColor = "Highlight";
          allLinks[allLinks.length - 1].style.color = "HighlightText";
          break;
        }
        else if (srcEl.suggestList.curSel - 1 >= 0)
        {
          allLinks[srcEl.suggestList.curSel].style.backgroundColor = "";
          allLinks[--srcEl.suggestList.curSel].style.backgroundColor = "Highlight";
          allLinks[srcEl.suggestList.curSel].style.color = "HighlightText";
        }

       
        break;

      case 13 : // return

        var allLinks = srcEl.suggestList.getElementsByTagName("a");

        if (srcEl.suggestList.curSel > -1)
          srcEl.value = allLinks[srcEl.suggestList.curSel].firstChild.nodeValue;

      case 37 : // left
        break;
      case 39 : // right
        break;
     }
  }
}