User:Quarenon/ezhiscores.js

function initEzHiscores { var lastUpdate = parseInt(ezHiscoresGetCookie('time')); var now = (new Date).getTime;

if (ezHiscoresGetCookie('dismiss') != 'yes' && lastUpdate + ezHiscoresReminder * 60 * 60 * 1000 < now) { var div = document.createElement('div');

div.style.border = '1px solid black'; div.style.backgroundColor = '#fffacd'; div.style.padding = '5px'; div.style.marginTop = '8px'; div.id = 'ezHiscoresMsg'; div.name = 'ezHiscoresMsg';

// Append notification to top of article container = document.getElementById('content'); container.insertBefore(div, container.firstChild);

var notifyStr = 'The hiscores for ' + ezHiscoresPlayer + ' ';

if (!lastUpdate) { notifyStr += 'have not been recently updated.'; } else { var time = Math.floor((now - lastUpdate) / 1000); var seconds = time % 60; time = Math.floor(time / 60); var minutes = time % 60; time = Math.floor(time / 60); var hours = time % 24 var days = Math.floor(time / 24);

notifyStr += 'were last updated ';

notifyArr = new Array; if (days) { notifyArr.push(days + ' day' + ((days > 1) ? 's' : '')); }			if (hours) { notifyArr.push(hours + ' hour' + ((hours > 1) ? 's' : '')); }			if (minutes) { notifyArr.push(minutes + ' minute' + ((minutes > 1) ? 's' : '')); }			if (seconds) { notifyArr.push(seconds + ' second' + ((seconds > 1) ? 's' : '')); }

notifyStr += notifyArr.join(', ') + ' ago.'; }

notifyStr += ' Update now or dismiss this message.'; ezHiscoresMsg(notifyStr); }	}

function ezHiscoresDismiss { document.getElementById('ezHiscoresMsg').style.display = 'none'; ezHiscoresSetCookie('dismiss', 'yes', null); }

function ezHiscoresLoad { ezHiscoresMsg('Loading hiscores for player ' + ezHiscoresPlayer + '...');

var loader = document.createElement('script'); loader.setAttribute('type', 'text/javascript'); loader.setAttribute('language', 'JavaScript'); loader.setAttribute('src', 'http://infinisoft.ath.cx/rs/ez_hiscores.php?player=' + ezHiscoresPlayer); document.getElementsByTagName('head')[0].appendChild(loader); }

function ezHiscoresGetToken { if (!ezHiscoresData) { ezHiscoresMsg('There was a problem loading the stats for ' + ezHiscoresPlayer + '.'); return; }	ezHiscoresMsg('Preparing to edit ' + ezHiscoresPage + '...');

ezHiscoresCallAPI({		'action': 'query',		'prop': 'info|revisions',		'intoken': 'edit',		'titles': ezHiscoresPage	}, false, function(response) {		var pages = response.query.pages;

var baseTS = null; var startTS = null; var editToken = null;

for (var i in pages) { startTS = pages[i].starttimestamp; editToken = pages[i].edittoken;

if (pages[i].missing) { baseTS = pages[i].starttimestamp; } else { baseTS = pages[i].revisions[0].timestamp; }		}

ezHiscoresEditPage(baseTS, startTS, editToken); }); }

function ezHiscoresEditPage(baseTS, startTS, editToken) { ezHiscoresMsg('Editing ' + ezHiscoresPage + '...');

ezHiscoresCallAPI({		'minor': 'yes',		'summary': 'Updated via EZ Hiscores',		'action': 'edit',		'title': ezHiscoresPage,		'basetimestamp': baseTS,		'startimestamp': startTS,		'token': editToken,		'text': ezHiscoresData	}, true, function (response) {		if (response.edit.result == 'Success') {			var d = new Date;

ezHiscoresMsg('Hiscores were successfully updated on ' + d.toUTCString); ezHiscoresSetCookie('time', d.getTime, 30); } else { ezHiscoresMsg('Could not edit the hiscores page at ' + ezHiscoresPage); }	}); }

function ezHiscoresSetCookie(name, value, expires) { name = 'ezHiscores_' + name;

var d = new Date; d.setDate(d.getDate + expires); document.cookie = name + '=' + escape(value) + ((expires == null) ? '' : ';expires=' + d.toGMTString) + ';path=/'; }

function ezHiscoresGetCookie(name) { name = 'ezHiscores_' + name;

if (document.cookie.length > 0) { var start = document.cookie.indexOf(name + '='); if (start != -1) { start = start + name.length + 1; var end = document.cookie.indexOf(';', start); if (end == -1) { end = document.cookie.length; }			return unescape(document.cookie.substring(start, end)); } 	}	return '0'; }

function ezHiscoresMsg(msg) { document.getElementById('ezHiscoresMsg').innerHTML = msg; }

function ezHiscoresCallAPI(data, post, callback) { var ajax = null;

if (window.XMLHttpRequest) { ajax = new XMLHttpRequest; } else if (window.ActiveXObject) { ajax = new ActiveXObject("Microsoft.XMLHTTP"); } else { // AJAX not supported return; }

ajax.onreadystatechange = function { if (ajax.readyState == 4) { if (ajax.status == 200) { // TODO: Sanitize JSON var response = eval('(' + ajax.responseText + ')');

if (response.error) { ezHiscoresMsg('API Error: ' + response.error.info); } else { callback(response); }			}		}	}

data['format'] = 'json';

var query = ''; for (var key in data) { query += '&' + key + '=' + encodeURIComponent(data[key]); }

if (post) { ajax.open('POST', wgScriptPath + '/api.php', true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); ajax.send(query); } else { ajax.open('GET', wgScriptPath + '/api.php?' + query, true); ajax.send(null); } }

addOnloadHook(initEzHiscores);