User:R1ck3t/monaco.js

/*** BEGIN EZ-HISCORES SCRIPT ***/

/* For help with this script, visit User:Quarenon/EzHiscoresGuide */

/* The notification reminder time, in hours. */ var ezHiscoresReminder = 12;

/* Use 1 to enable the update log, or 0 to turn it off. */ var ezHiscoresLog = 0; //(disabled)

/* Your RuneScape player name. */ var ezHiscoresPlayer = 'R1ck3t';

/* The page where your stats are stored. */ var ezHiscoresPage = 'User:Ricky28269/HiScoreData';

/** * ezhiscores.js * (copy from 06 July 2009) * * Add a reminder box for updating a user's hiscore data, and adds a mechanism for updating it * with one click. * * TODO LIST * - Wrap functions in a class for namespace separation. * - Better JSON sanitation. * - Better handling when Ajax takes too long to load. * - Support multiple players. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */

/** Checks last update/dismiss cookie and creates the notification box. */ addOnloadHook(function {	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 = '4px'; div.style.marginBottom = '15px'; div.id = 'ezHiscoresMsg'; div.name = 'ezHiscoresMsg';

// Append notification to top of article var container = null; if (skin == 'monaco') { container = document.getElementById('article'); } else { container = document.getElementById('content'); }		container.insertBefore(div, container.firstChild);

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

if (!lastUpdate) { notifyStr += 'may 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 ';

if (days) { notifyStr += days + ' day' + ((days > 1) ? 's' : ''); } else if (hours) { notifyStr += hours + ' hour' + ((hours > 1) ? 's' : ''); } else if (minutes) { notifyStr += minutes + ' minute' + ((minutes > 1) ? 's' : ''); } else { notifyStr += seconds + ' second' + ((seconds > 1) ? 's' : ''); }

notifyStr += ' ago.'; }

notifyStr += ' Update data'; notifyStr += ' &middot; Dismiss'; ezHiscoresMsg(notifyStr); }	});

/** Hide the notification box and set a cookie to keep it hidden for the rest of the browser session. */ function ezHiscoresDismiss { document.getElementById('ezHiscoresMsg').style.display = 'none'; ezHiscoresSetCookie('dismiss', 'yes', null); }

/** Loads an external script to fetch the hiscore data. */ function ezHiscoresLoad { ezHiscoresMsg('Loading hiscores for player ' + ezHiscoresPlayer + '...'); var log = (typeof(ezHiscoresLog) != 'undefined' && ezHiscoresLog) ? '1' : '0';	var username = encodeURIComponent(wgUserName); var page = encodeURIComponent(ezHiscoresPage); importScriptURI('http://rickyc.org/ez_hiscores.php'); }

/** Fetch the edit token prior to editing the hiscore data page. */ 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); }); }

/** Edit the hiscore page. */ function ezHiscoresEditPage(baseTS, startTS, editToken) { ezHiscoresMsg('Editing ' + ezHiscoresPage + '...');

ezHiscoresCallAPI({		'minor': 'yes',		'summary': 'Updated via EzHiscores',		'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); }	}); }

/** Set a cookie */ 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=/'; }

/** Get a cookie */ 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'; }

/** Set notification box content. */ function ezHiscoresMsg(msg) { document.getElementById('ezHiscoresMsg').innerHTML = msg; }

/** Call MediaWiki API using AJAX. */ 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); } }

var playerURI = encodeURIComponent(ezHiscoresPlayer);

/*** END EZ-HISCORES SCRIPT ***/