Wikia

Memory Alpha

Bp/live search script

Discuss2
37,173pages on
this wiki

< User:Bp

This is a script that updates a drop-down list of articles as you type in an in-page search box. Clicking on an item in the list goes to that page.

Usage Edit

If you don't want the script to run add this line to your User:/monobook.js:

var searchDropDown = 0;

To ignore redirects:

var showRedirects = 0;

Example Edit

If the script is on, you should get something like this as you type:

action.png

Issues Edit

  • Doesn't work in IE, but fails well.

Source Edit

Versions Edit

  • rev 5
    • Automatically uppercase first character.
  • rev 4
    • Now uses query.php to get the data from the server. This is much faster than Special: pages.
  • rev 3
    • A fix to remove the "About Wikia" and "Terms of Use" items
    • Some new styles, and new tools in the box
  • rev 2
    • Added seperate style for redirects
    • Added ability to ignore redirects
    • minor style change
  • rev 1
  • first upload

CSS Edit

.searchList { border: 1px solid black; overflow:hidden; margin-left: 0px !important; margin-top: 2px; padding: 2px; background-color: white; color: black; }

.searchListItem { list-style: none; background: transparent; color:black; cursor:default; }
.searchListItemHighlight { list-style: none; background-color: #4070D0; color:white; cursor:default; }

.searchListItemRedirect { list-style: none; background: transparent; color:#888; cursor:default; }
.searchListItemRedirectHighlight { list-style: none; background-color: #4070D0; color:white; cursor:default; }

.searchListText { list-style: none; background-color: white; color:black; padding: 2px; cursor:default; }
.searchListTools { list-style: none; background-color: #ccc; border-top: 1px dotted black; color:black; padding: 2px; }

a.searchListTools { color: blue; text-decoration: none; border: none; }
a.searchListTools:active { color: red; text-decoration: underline; border: none; }
a.searchListTools:visited { color: purple; text-decoration: none; border: none; }
a.searchListTools:hover { color: red; text-decoration: underline; border: none; }

JS Edit

// 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/wiki/Special:Prefixindex/";
var queryphpLink = "http://memory-alpha.org/query.php?what=allpages&aplimit=10&apnamespace=0&apfrom=";
var loadingHTML = "Loading...";
var searchToolsHTML = "<a class=\"searchListTools\" href=\"/wiki/Special:Allpages\" title=\"All pages\">All pages</a> | <a class=\"searchListTools\" href=\"/wiki/Special:Search\" title=\"Advanced Search\">Advanced Search</a>";

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 "<li class=\"searchListItemRedirect\"" +
				"onmouseover=\"this.className = 'searchListItemRedirectHighlight';\"" +
				"onmouseout=\"this.className = 'searchListItemRedirect';\"" +
				"onclick=\"SubmitText(this.textContent);\"" +
				">" + title + "</li>";
	} else {
		return "<li class=\"searchListItem\"" +
				"onmouseover=\"this.className = 'searchListItemHighlight';\"" +
				"onmouseout=\"this.className = 'searchListItem';\"" +
				"onclick=\"SubmitText(this.textContent);\"" +
				">" + title + "</li>";
	}
}

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("/wiki/") >= 4)) {

			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 += "<li class=\"searchListTools\">" + searchToolsHTML + "</li>";
}

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 "&lt;title&gt;":
				si = si.nextSibling;
				title = si.data;
				break;
			case "&lt;redirect /&gt;":
				isRD = 1;
				break;
			case "&lt;page&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 + "<li class=\"searchListTools\">" + searchToolsHTML + "</li>";
}

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 = "<li class=\"searchListText\">" + loadingHTML + "</li>";
		searchListElement.innerHTML += "<li class=\"searchListTools\">" + searchToolsHTML + "</li>";
		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);

Around Wikia's network

Random Wiki