User:Quarenon/exchangewatch.js

/* */ /** * exchangewatch.js * * Anti-vandal tool for Exchange pages. Visit User:Quarenon/Scripts/Exchange_Watch to activate. * * 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 . */

$(function {	function articleURL(article) {		return wgArticlePath.replace('$1', encodeURI(article));	}

function callAPI(data, method, callback) { data['format'] = 'json'; $.ajax({			data: data,			dataType: 'json',			url: wgScriptPath + '/api.php',			type: method,			success: function(response) {				if (response.error) {					alert('API error: ' + response.error.info);				} else {					callback(response);				}			},			error: function(xhr, error) { alert('AJAX error: ' + error); },			timeout: 10000 // msec		}); }

var reLastPrice = /^\|Last=(.*)$/m; var rePrice = /^\|Price=(.*)$/m; var reLastDate = /^\|LastDate=(.*)$/m; var reDate = /^\|Date=(.*)$/m; var reLowAlch = /^\|LowAlch=(.*)$/m; var reHighAlch = /^\|HighAlch=(.*)$/m;

if (wgPageName == 'User:Quarenon/Scripts/Exchange_Watch' && (wgAction == 'view' || wgAction == 'purge')) { $('#exchange_watch').html('Loading...'); $('#WikiaRail').remove; $('body').addClass('oasis-one-column'); callAPI({			'rclimit': '25',			'rcshow': '!bot',			'rcprop': 'user|timestamp|title|ids|sizes|comment',			'action': 'query',			'list': 'recentchanges',			'rcnamespace': '112'		}, 'GET', function(response) {			var $table = $(' ').addClass('wikitable').attr('id', 'ewData').css('width', '100%');			$table.append( ' ' +	' Time ' + ' Title ' + ' User ' + ' Bytes ' + ' Revision ' + ' Price ' + ' Last ' + ' LowAlch ' + ' HighAlch ' + ' '			);

var revs = []; var curDate = '';

for (var i in response.query.recentchanges) { var rc = response.query.recentchanges[i];

revs.push(rc.revid); revs.push(rc.old_revid); var prefix = ''; var lenClass = 'even';

if (rc.newlen > rc.oldlen) { prefix = '+'; lenClass = 'pos'; } else if (rc.newlen < rc.oldlen) { lenClass = 'neg'; }

var date = rc.timestamp.substring(0, 10); var time = rc.timestamp.substring(11, 19);

if (date != curDate) { $table.append(' ' + date + ' '); curDate = date; }

$table.append( ' ' +	' ' + time + ' UTC ' +	' ' + rc.title.replace(/^Exchange:/, '') + ' ' +	' ' + rc.user + ' ' +	'' + prefix + (rc.newlen - rc.oldlen) + ' ' +	' Cur ' +	'?? ' +	'?? ' +	'?? ' +	'?? ' + ' ' +	' ' +		'talk &bull; ' +		'contribs &bull; ' +		'undo &bull; ' +		'</a>' + ' Prev ' + '<td class="ew_price_' + rc.old_revid + '">?? ' +	'<td class="ew_last_' + rc.old_revid + '">?? ' +	'<td class="ew_low_' + rc.old_revid + '">?? ' +	'<td class="ew_high_' + rc.old_revid + '">?? ' + ' '				);			}			$('#exchange_watch').empty.append($table);

callAPI({				'revids': revs.join('|'),				'rvprop': 'ids|content',				'rvtoken': 'rollback',				'action': 'query',				'prop': 'revisions',				'rcnamespace': '112'			}, 'GET', function(response) {				for (var pageId in response.query.pages) {					var page = response.query.pages[pageId];					for (var i in page.revisions) {						var rev = page.revisions[i];

var content = rev['*'];

var priceMatches = content.match(rePrice); var lastPriceMatches = content.match(reLastPrice); var lowAlchMatches = content.match(reLowAlch); var highAlchMatches = content.match(reHighAlch);

$('#exchange_watch .ew_price_' + rev.revid).text(priceMatches ? priceMatches[1] : 'ERROR'); $('#exchange_watch .ew_last_' + rev.revid).text(lastPriceMatches ? lastPriceMatches[1] : 'ERROR'); $('#exchange_watch .ew_low_' + rev.revid).text(lowAlchMatches ? lowAlchMatches[1] : 'ERROR'); $('#exchange_watch .ew_high_' + rev.revid).text(highAlchMatches ? highAlchMatches[1] : 'ERROR');

var $rbLink = $('#exchange_watch .ew_rb_' + rev.revid); $rbLink.attr('href', $rbLink.attr('href') + rev.rollbacktoken).text('rollback'); }				}			});		});	} });

/* */