User:Stewbasic/tt.js

// copied from User:Tyilo/tt.js /* highlight v3 Highlights arbitrary terms.  MIT license. Johann Burkard   jQuery.fn.highlight = function(pat) { function innerHighlight(node, pat) { var skip = 0; if (node.nodeType == 3) { var pos = node.data.toUpperCase.indexOf(pat); if (pos >= 0) { var spannode = document.createElement('span'); spannode.className = 'highlight'; var middlebit = node.splitText(pos); var endbit = middlebit.splitText(pat.length); var middleclone = middlebit.cloneNode(true); spannode.appendChild(middleclone); middlebit.parentNode.replaceChild(spannode, middlebit); skip = 1; } }  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) { for (var i = 0; i < node.childNodes.length; ++i) { i += innerHighlight(node.childNodes[i], pat); } }  return skip; } return this.each(function { innerHighlight(this, pat.toUpperCase); }); }; jQuery.fn.removeHighlight = function { return this.find("span.highlight").each(function { this.parentNode.firstChild.nodeName;  with (this.parentNode) {   replaceChild(this.firstChild, this);   normalize;  } }).end; }; // Case insensitive :contains jQuery.expr[':'].Contains = function(a,i,m){ return jQuery(a).text.toUpperCase.indexOf(m[3].toUpperCase)>=0; }; $(function {	var $container = $('#tt');	if(wgPageName === 'User:Tyilo/Treasure_Trails' && $container.length > 0)	{		document.styleSheets[0].insertRule('.highlight { background-color: yellow; }', 0);		$('#install').hide;		$(' ').appendTo($container);		var $output = $('#output');		var $query = $('#query');		var response, callback;		$.ajax({ url: 'http://runescape.wikia.com/wiki/Treasure_Trails/Full_guide', success: function(data) { response = $(data); $output.hide; if(callback) { callback; }			},			error: function { $('#loading').html(' Failed to load cache! '); }		});		$query.keydown(function(e) { if(e.which === 13) { search; }		});		$('#submit').click(search);		function search {			callback = function {				$output.show;				$output.html('Loading...');				var query = $query.val;				var $result = $('.wikitable td :Contains("' + query + '")', response.clone).highlight(query);				$output.html('');				$result.each(function { var $this = $(this); var $table = $this.parent.closest('table'); var $header = $table.find('th').parent.closest('tr'); var $newtable; if(!$table.data('included')) { $newtable = $table.clone(true).html($header); $table.data('included', $newtable); } else { $newtable = $table.data('included'); }					$output.append($newtable.append($this.parent.closest('tr'))); });			};			if(response) {				callback;			}		}	} });