User:Supergeeky1/monobook.js

// Start "live search" script; by User:Bp // findPos function from http://www.quirksmode.org/js/findpos.html // - var searchDropDown = 1; var showRedirects = 1; var uppercaseFirstChar = 1; var allPagesLink = "http://memory-alpha.org/en/wiki/Special:Prefixindex/"; var queryphpLink = "http://memory-alpha.org/en/query.php?what=allpages&aplimit=10&apnamespace=0&apfrom="; var loadingHTML = "Loading..."; var searchToolsHTML = "All pages | Advanced Search";

var qmethod = 1; // 0 = special pages, 1 = query.php

var currentInputElement; var allPagesFrame; var searchListElement; var tid;

function SubmitText(text) { currentInputElement.value = text; currentInputElement.form.submit; }

function findPos(obj) { var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft curtop = obj.offsetTop while (obj = obj.offsetParent) { curleft += obj.offsetLeft curtop += obj.offsetTop }	}	return [curleft,curtop]; }

function makeSearchListItem(title,isRD) { if (isRD) { return "" + title + ""; } else { return "" + title + ""; } }

function allPagesExtract(i,o) { var lc = 0; o.innerHTML = ""; var links = allPagesFrame.contentDocument.getElementsByTagName("a"); for (var i = 0; i < links.length; i++) { var ptn = (links[i].parentNode.tagName) ? links[i].parentNode.tagName.toLowerCase : '';

if ((ptn == "td" || ptn == "div") &&			links[i].title != '' &&			links[i].title == links[i].textContent &&			(links[i].href.indexOf("/en/wiki/") >= 7)) {

if (links[i].parentNode.className == "allpagesredirect") { if (showRedirects) { o.innerHTML += makeSearchListItem(links[i].textContent,1); if (++lc == 10) { break; } }			} else { o.innerHTML += makeSearchListItem(links[i].textContent,0); if (++lc == 10) { break; } }		}	}	o.innerHTML += "" + searchToolsHTML + ""; }

function allPagesExtract_queryphp_xmlfm(i,o) { var pre = allPagesFrame.contentDocument.getElementsByTagName("pre"); if (pre.length < 1) return;

var out = ""; var title = ""; var isRD = 0; var si = pre[0].firstChild; while(si) { var tn = (si.tagName) ? si.tagName.toLowerCase : ''; if (tn == "font") { switch (si.innerHTML) { case "&amp;lt;title&amp;gt;": si = si.nextSibling; title = si.data; break; case "&amp;lt;redirect /&amp;gt;": isRD = 1; break; case "&amp;lt;page&amp;gt;": if (title) { out += makeSearchListItem(title,isRD); } title = ""; isRD = 0; break; }		}		si = si.nextSibling; }	// add last one if (title) { out += makeSearchListItem(title,isRD);	}

o.innerHTML = out + "" + searchToolsHTML + ""; }

function allPagesFrame_onLoad(e) { if (qmethod == 1) { allPagesExtract_queryphp_xmlfm(allPagesFrame, searchListElement); } else { allPagesExtract(allPagesFrame, searchListElement); } }

function hookSearchInput { var fone = 0;

if (!searchDropDown) { return; }

var iboxes = document.getElementsByTagName("input"); for (var i = 0; i < iboxes.length; i++) { if (iboxes[i].className == 'bodySearchIput') { iboxes[i].onkeypress = requestUpdate; iboxes[i].onblur = requestHideSearchList; fone = 1; }	}

if (fone) { allPagesFrame = document.createElement("iframe"); searchListElement = document.createElement("ul");

allPagesFrame.onload = allPagesFrame_onLoad; allPagesFrame.frameBorder = 0; allPagesFrame.width = 0; allPagesFrame.height = 0;

searchListElement.className = "searchList"; searchListElement.style.zIndex = 99; searchListElement.style.display = "none";

document.body.appendChild(allPagesFrame); document.body.appendChild(searchListElement); }

}

function searchUpdate { tid = 0;

if (uppercaseFirstChar) { var s = currentInputElement.value; if (s.length > 0) { if (s.length == 1) { s = s.toUpperCase; } else { s = s.substr(0,1).toUpperCase + s.substr(1,s.length-1); }			currentInputElement.value = s;		} }

if (qmethod == 1) { allPagesFrame.src = queryphpLink + escape(currentInputElement.value) + ((showRedirects) ? "" : "&apfilterredir=nonredirects"); } else { allPagesFrame.src = allPagesLink + escape(currentInputElement.value); } }

function requestUpdate(e) {

//move the box to the correct input element if (e.currentTarget != currentInputElement) { searchListElement.innerHTML = "" + loadingHTML + ""; searchListElement.innerHTML += "" + searchToolsHTML + ""; window.clearTimeout(tid); tid = 0; };

currentInputElement = e.currentTarget;

var ibpos = findPos(currentInputElement);

searchListElement.style.position = "absolute"; searchListElement.style.top = + ibpos[1] + currentInputElement.offsetHeight + "px"; searchListElement.style.left = + ibpos[0] + "px"; searchListElement.style.width = (currentInputElement.offsetWidth + 30) + "px";

if (!tid) { tid = window.setTimeout(searchUpdate, 500); }	showSearchList; }

function requestHideSearchList(e) { window.setTimeout(hideSearchList, 500); }

function hideSearchList { searchListElement.style.display = "none"; }

function showSearchList { searchListElement.style.display = "block"; }

hookEvent("load", hookSearchInput);