User:Killr833/getdefense.js

/* * getdefense.js * * Fetch the defense of an item. * * Derived from RuneScape Wiki's item comparison and * Template:USERNAME used on many wikis

$(document).ready(function {	function parseTemplate(text, tpl) {		tpl = tpl.replace(/[_ ]/g, '[_ ]');		var re = new RegExp('', 'gi');

var data = []; var match;

while (match = re.exec(text)) { var params = match[2].split('|'); var j = 1; var tplData = new Array; for (var k in params) { var t = params[k].split('='); var name = null; var value = null; if (t.length == 1) { name = (j++) + ''; value = t[0]; } else { name = t[0]; value = t[1]; }				tplData[$.trim(name)] = $.trim(value); }			data.push(tplData); }

return data; }

var elems = YAHOO.util.Dom.getElementsByClassName('defense', 'span', document.getElementById('bodyContent'));

if (!elems.length) { return; }

var titles = []; for (var i in elems) { titles.push(elems[i].innerHTML); }

$.ajax({		data: {			'action': 'query',			'prop': 'revisions',			'titles': titles.join('|'),			'rvprop': 'content',			'redirects': '',			'format': 'json'		},		dataType: 'json',		success: function(response) {			var pages = response.query.pages;

for (var i in pages) { var tplData = parseTemplate(pages[i].revisions[0]['*'], 'iteminfobox');

for (var j in elems) { if (elems[j].innerHTML == pages[i].title) { elems[j].innerHTML = tplData[0]['defense']; }				}			}		},		url: wgScriptPath + '/api.php', timeout: 10000 }); });