User:Suppa chuppa/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 . */

var ewGedbFetching = false; var ewItemIds = []; var markScript = [];

function ewFetchGEDB { ewGedbFetching = true;

$.ajax({		'url': 'http://query.yahooapis.com/v1/public/yql',		'data': {			'q': 'SELECT content FROM html WHERE url="http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=' + ewItemIds[0] + '" AND xpath="//div[@id=\'item_additional\']/p"',			'format': 'json',			'_maxage': '900'		},		'dataType': 'jsonp',		'jsonp': 'callback',		'jsonpCallback': 'ewJSONPCallback',		'method': 'GET',		'timeout': 10000	}); }

function ewJSONPCallback(response) { var itemId = ewItemIds.shift;

if (response.query.results) { var price = $.trim(response.query.results.p).split('\n').pop; $('#ew_main td.ew_item_' + itemId).text(price); } else { var $a = $('').attr('href', '#').click(function {                                                       window.open('http://services.runescape.com/m=itemdb_rs/Shears/viewitem.ws?obj=' + itemId, '_newtab');                                                        }).text('Check price'); $('#ew_main td.ew_item_' + itemId).empty.append($a); }

if (ewItemIds.length) { ewFetchGEDB; } else { ewGedbFetching = false; } }

$(function {	var reLastPrice = /^\|Last=(.*)$/m;	var rePrice = /^\|Price=(.*)$/m;	var reLastDate = /^\|LastDate=(.*)$/m;	var reDate = /^\|Date=(.*)$/m;	var reLowAlch = /^\|LowAlch=(.*)$/m;	var reHighAlch = /^\|HighAlch=(.*)$/m;	var reItemId = /^\|ItemId=\s*([0-9]+)\s*$/m;

var refreshTimer = null; var rcSince = null; var rcIds = [];

var loadingHtml = '';

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

function scriptURL(article) { return wgScript + '?title=' + encodeURIComponent(article.replace(/ /g, '_')); }

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		}); }

function reloadComplete { if ($('#ewAutoRefresh:checked').length) { window.clearTimeout(refreshTimer); refreshTimer = window.setTimeout(reloadData, 10 * 1000); }		$('#ew_update img').hide; $('#ewLastUpdate').text((new Date).toUTCString); }

function reloadData { $('#ew_update img').show; var show = $('#ewShowLast').val; var rcQuery = { 'rclimit': show, 'rcshow': '!bot', 'rcprop': 'user|timestamp|title|ids|sizes|comment|tags', 'action': 'query', 'list': 'recentchanges', 'rcnamespace': '112', 'rctype': 'edit|new', 'format': 'json' };

if (rcSince) { rcQuery['rcend'] = rcSince; }

callAPI(rcQuery, 'GET', function(response) {			var revs = [];			var curDate = '';

response.query.recentchanges.reverse;

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

// Remove duplicates which may occur during autorefresh if ($.inArray(rc.rcid, rcIds) > -1) { continue; }				rcIds.push(rc.rcid);

// Remove any existing rows of the same page in the table... if ($('#ewRecentRevOnly:checked').length) { $('tr.ew_page_' + rc.pageid).remove; }

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'; }

if (rc.tags) { markScript[i] = true; } else { markScript[i] = false; }				var time = rc.timestamp.substring(11, 19); rcSince = rc.timestamp;

$('#ewHeader').after( '' +	' ' + time + ' ' +	' ' +		'' + rc.title.replace(/^Exchange:/, '') + ' ' +		'(edit) ' +               '(diff)' +	' ' +	'' + prefix + (rc.newlen - rc.oldlen) + ' ' +	' Cur ' +	'? ' +	'? ' +	'? ' +	'? ' +	'? ' +	'<td class="ew_high_' + rc.revid + '">? ' + ' <tr class="ew_rev_' + rc.revid + ' ew_page_' + rc.pageid + ' ew_prev">' +	' &raquo; ' +		'talk</a> &bull; ' + 'contribs</a> &bull; ' + 'undo</a> &bull; ' + '</a>' + ' Prev ' + '<td class="ew_user_' + rc.old_revid + '">? ' +	'<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 + '">? ' + ' '				);			}

if (revs.length == 0) { reloadComplete; return; }

callAPI({				'revids': revs.join('|'),				'rvprop': 'ids|user|content',				'rvtoken': 'rollback',				'action': 'query',				'prop': 'revisions',				'rcnamespace': '112'			}, 'GET', function(response) {				reloadComplete;

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); var itemIdMatches = content.match(reItemId);

var mark = !priceMatches || !lastPriceMatches || !lowAlchMatches || !highAlchMatches || !itemIdMatches; if (!mark) { var curPrice = priceMatches[1].replace(/,/g, ''); var lastPrice = lastPriceMatches[1].replace(/,/g, '');

if (curPrice < lastPrice * 0.8 || curPrice > lastPrice * 1.2) { mark = true; }						}

if (mark) { $('#ew_main tr.ew_rev_' + rev.revid).css('backgroundColor', '#8CDD81'); }

if (markScript[i]) { $('#ew_main tr.ew_rev_' + rev.revid).css('backgroundColor', '#9E9'); }

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

$('#ew_main td.ew_user_' + rev.revid).html('' + rev.user + '</a>');

if (itemIdMatches) { var itemId = itemIdMatches[1];

if ($.inArray(itemId, ewItemIds) == -1) { ewItemIds.push(itemId); }

if (!ewGedbFetching) { ewFetchGEDB; }

$('#ew_main td.ew_gedb_' + rev.revid).addClass('ew_item_' + itemId).html(loadingHtml); } else { $('#ew_main td.ew_gedb_' + rev.revid).text('ERROR'); }

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

if ((wgPageName == 'User:Quarenon/Scripts/Exchange_Watch' || wgPageName == 'User:Suppa_chuppa/RecentChangesHybrid' || wgPageName=='User:Thebrains222/Projects/Sandbox4') && (wgAction == 'view' || wgAction == 'purge')) { $('#WikiaRail').remove; $('body').addClass('oasis-one-column');

appendCSS(			'.ew_cur > td, .ew_cur > th { border-top: 2px solid #333 !important; }' +			'/*#ew_msg { padding: 1em; margin: 1px; border: 1px solid black; }*/'		);

$('#ew_options').empty.append( ' ' +	' ' +		' Exchange Watch options ' +		'<input type="checkbox" id="ewRecentRevOnly"/> <label for="ewRecentRevOnly">Display only the most recent revision of each page ' +		'<input type="checkbox" id="ewAutoRefresh" checked="checked" /> <label for="ewAutoRefresh">Auto-refresh table ' +		//'<input type="checkbox" id="ewBotEdits" /> <label for="ewBotEdits">Show bot edits ' +               'Show last <input type="text" id="ewShowLast" maxlength="3" size="1" value="25" /> <label for="ewShowLast"> Exchange edits ' +                '<input type="button" value="Refresh" id="ewRefresh" />' +	' ' + ' '		);

$('#ew_update').html('Last update: ' + loadingHtml);

var $table = $(' ').addClass('wikitable').attr('id', 'ewData').css('width', '100%'); $table.append( '<tr id="ewHeader">' +	' Time ' +	' Title ' +	' Bytes ' +	' Revision ' +	' User ' +	' GEDB ' +	' Price ' +	' Last ' +	' LowAlch ' +	' HighAlch ' + ' '		);

$('#ew_main').empty.append($table);

$('#ewAutoRefresh').click(function {			window.clearTimeout(refreshTimer);			if ($(this).is(':checked')) {				refreshTimer = window.setTimeout(reloadData, 10 * 1000);			}		});

// Reload everything if these are changed. $('#ewRecentRevOnly,#ewBotEdits,#ewRefresh').click(function {			$('#ewHeader').nextAll.remove;			rcIds = [];			rcSince = null;			reloadData;		});

reloadData; } });

/* */