MediaWiki:Common.js/gemwupdate.js

/* Any JavaScript here will be loaded for all users on every page load. */ //

//Parts of this script are copied from User:Quarenon/gemwupdate.js. Credit goes to Quarenon for writing that code.

var button; addOnloadHook(function {	$('#gemw_guide').html(' Update the price of this item: Update price ');	button = document.getElementById('updateGEP'); })

var reqs,priceFailed;

function GEReqsDone(failed) { priceFailed = failed||true; reqs++ if (/*reqs == 2 && */!priceFailed) { alert('Thank you for your submission! The page will now be reloaded.'); document.location.replace(wgScript + '?title=' + encodeURIComponent(wgPageName) + '&action=purge'); } else/* if (reqs == 2)*/ { alert('An error occurred while submitting the edit.'); button.disabled = false; button.innerHTML = 'Update price'; } }

function updateGEPrice { reqs = 0; button.disabled = true; button.innerHTML = 'Updating price...'; $.getJSON('http://anyorigin.com/get?url=http%3A//services.runescape.com/m%3Ditemdb_rs/api/graph/'+$('#GEDBID').html+'.json&callback=?', function(data){		var pricelist = data.contents.daily;		var names = []		for (var name in pricelist) {			names.push(name);		}		var unixnow = names[names.length-1]		var price = pricelist[unixnow];		var curprice = parseInt($('#GEPrice').html);		if (price == curprice) {			alert('The price is already the same as the price on the official GE database.');			button.disabled = false;			button.innerHTML = 'Update price';			return false;		}		callAPI({ 'action': 'query', 'prop': 'info|revisions', 'intoken': 'edit', 'titles': wgPageName, 'rvprop': 'content', 'rvlimit': '1', 'indexpageids': 'true' }, 'GET', function(response) { var page = response.query.pages[response.query.pageids[0]]; var content = page.revisions[0]['*']; var cPrice = content.match(/\|Price\s*=\s*([\d,]+)/)[0].replace(/\|Price\s*=\s*/,''); if (price == parseInt(cPrice.replace(',',''))) { reqs++ alert('The price is already the same as the price on the official GE database.'); button.disabled = false; button.innerHTML = 'Update price'; return false; }			var cDate = content.match(/\|Date\s*=\s*([^\|\n]+)/)[0].replace(/\|Date\s*=\s*/,''); var updated = content.replace(/\|Price\s*=\s*([\d,]+)/, '|Price='+addCommas(price)) .replace(/\|Last\s*=\s*([\d,]+)/, '|Last='+cPrice) .replace(/\|Date\s*=\s*([^\|\n]+)/, '|Date=') .replace(/\|LastDate\s*=\s*([^\|\n]+)/, '|LastDate='+cDate); callAPI({				'minor': 'yes',				'summary': 'Updated GEMW data via script on the exchange page.',				'action': 'edit',				'title': wgPageName,				'basetimestamp': page.revisions[0].timestamp,				'startimestamp': page.starttimestamp,				'token': page.edittoken,				'text': updated			}, 'POST', function(response) {				if (response.edit.result == 'Success') {					GEReqsDone				} else {					GEReqsDone(true);				}			}); });		/* FOR UPDATING THE /DATA PAGES. THIS PART OF THE SCRIPT IS NOT IN USE YET.		callAPI({ 'action': 'query', 'prop': 'info|revisions', 'intoken': 'edit', 'titles': wgPageName+'/Data', 'rvprop': 'content', 'rvlimit': '1', 'indexpageids': 'true' }, 'GET', function(response) { var page = response.query.pages[response.query.pageids[0]]; var content = page.revisions[0]['*']; if (content.match(new RegExp('^'+unixnow+':', 'm'))) { GEReqsDone; return false; } //The Data page already has an entry for that day. ABORT. var updated = content.replace(/\n}}/,'\n'+unixnow+':'+price+'$&'); callAPI({				'minor': 'yes',				'summary': 'Updated GEMW history data via script on the exchange page.',				'action': 'edit',				'title': wgPageName+'/Data',				'basetimestamp': page.revisions[0].timestamp,				'startimestamp': page.starttimestamp,				'token': page.edittoken,				'text': updated			}, 'POST', function(response) {				GEReqsDone;			}); });		*/	}); }