User:Quarenon/gemwupdate.js

/* */ /** * gemwupdate.js * * Provide a simpler editing interface for GEMW pages. * * 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 . */

if (wgCanonicalNamespace == 'Exchange' && wgAction == 'view') { $(document).ready(function {		var curPrice = parseInt($('#bodyContent>ul li:first').text.match(/Price: (.+) coins/)[1].replace(/,/g, ''));

// http://www.mredkj.com/javascript/numberFormat.html#addcommas function addCommas(nStr) { nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }			return x1 + x2; }

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

function gemwSubmit { var newPrice = $('#gemwNewPrice').val.replace(/,/g, ''); if (newPrice.match(/^[0-9]+$/) == null) { alert('The price entered was not numeric.'); return false; }

if (curPrice * 0.75 > newPrice || curPrice * 1.25 < newPrice) { alert('The new price exceeds the maximum allowed price change.'); return false; }

if (curPrice == newPrice) { alert('The new price is the same as the current price.'); return false; }

curPrice = addCommas(curPrice); alert(curPrice); return false;

$('#gemwSubmit').attr('disabled', 'disabled').val('Loading...');

callAPI({				'action': 'query',				'prop': 'info|revisions',				'intoken': 'edit',				'titles': wgPageName,				'rvprop': 'content'			}, 'GET', function(response) {				var pages = response.query.pages;				var page = null;				for (var i in pages) {					page = pages[i];				}

var content = page.revisions[0]['*']; content = content.replace(/^\|Last=.+$/m, '|Last=' + curPrice); content = content.replace(/^\|Price=.*$/m, '|Price=' + newPrice); var curDate = content.match(/^\|Date=(.*)$/m)[1]; content = content.replace(/^\|LastDate=.+$/m, '|LastDate=' + curDate); content = content.replace(/^\|Date=.+$/m, '|Date=');

callAPI({					'minor': 'yes',					'summary': 'Updated GEMW data via script.',					'action': 'edit',					'title': wgPageName,					'basetimestamp': page.revisions[0].timestamp,					'startimestamp': page.starttimestamp,					'token': page.edittoken,					'text': content				}, 'POST', function(response) {					if (response.edit.result == 'Success') {						alert('Thank you for your submission! The page will now be reloaded.');						document.location.reload;					} else {						alert('An error occurred while updating the data for this item.');					}				}); });

return false; }

$div = $('#bodyContent div:eq(1)').empty; $form = $(' '); $form.submit(gemwSubmit); $form.append($(' ').text('Update the price of this item:')); $p = $(''); $p.append($(' ').attr({'for': 'gemwNewPrice'}).text('New price:')); $p.append($(' ').attr({'id': 'gemwNewPrice', 'type': 'text', 'size': 9})); $p.append($(' ').css({'margin': '0 1em 0 1em'}).attr({'id': 'gemwSubmit', 'type': 'submit'}).val('Update')); $p.append($(' ').attr({'id': 'gemwMsg'})); $form.append($p); $div.append($form); }); }

/* */