User:Walrus068/gemwupdate.js

// "use strict"; var addCommas = (function {	var re = new RegExp("(\\d+)(\\d{3})");	return function addCommas(n) {		n = n.toString.split(".");		while (re.test(n[0])) {			n[0] = n[0].replace(re, "$1,$2");		}		return n.join(".");	}; }); if (wgCanonicalNamespace === "Exchange" && wgAction === "view") { addEventListener("load", function {		var gemw_guide = document.getElementById("gemw_guide"),			form = document.createElement("form"),			h4 = document.createElement("h4"),			p = document.createElement("p"),			p2 = document.createElement("p"),			label = document.createElement("label"),			input = document.createElement("input"),			submit = document.createElement("input"),

rePrice = new RegExp("^\\s*\\|\\s*Price\\s*=\\s*(.+?)\\s*$", "m"), reLast = new RegExp("^\\s*\\|\\s*Last\\s*=.*?$", "m"), reDate = new RegExp("^\\s*\\|\\s*Date\\s*=\\s*(.+?)\\s*$", "m"), reLastDate = new RegExp("^\\s*\\|\\s*LastDate\\s*=.*?$", "m"), reNewPrice = new RegExp("^([\\d\\.]+)\\s*(k|m|mil|mill)?$", "i"), reCommas = new RegExp(",", "g"),

api = wgScriptPath + "/api.php?format=json&indexpageids=1", title = encodeURIComponent(wgPageName),

showError = function (e) { alert(e); submit.disabled = false; },			onload = function { var j = JSON.parse(this.responseText), e = j.error, edit = j.edit; if (edit && edit.result === "Success") { alert("Thank you for your submission. The page will now be purged."); location.replace(wgScript + "?title=" + title +									"&action=purge"); } else if (e) { showError(e.code + ": " + e.info); }			},			onload1 = function { var n, xhr = new XMLHttpRequest, q = JSON.parse(this.responseText).query, page = q.pages[q.pageids[0]], edittoken = page.edittoken, content = page.revisions[0]["*"],

last_price = content.match(rePrice), last_date = content.match(reDate),

price = input.value.replace(reCommas, ""), matches = price.match(reNewPrice), unit = matches && matches[2];

if (last_price === null) { showError("An error occurred while parsing the previous price."); return false; } else { last_price = last_price[1]; }

if (last_date === null) { showError("An error occurred while parsing the previous date."); } else { last_date = last_date[1]; }

if (matches === null) { showError("The price you entered was not numeric."); return false; }

price = matches[1];

if (unit) { unit = unit.toLowerCase; if (unit === "k") { n = 1000; } else if (unit === "m" || unit === "mil" ||								unit === "mill") { n = 1000000; }					price = price * n;				}

if (Math.floor(price) !== price) { showError("Fractional values are not allowed"); return false; }

price = addCommas(price); if (price === last_price) { showError("The current price is the same as entered; it will not be updated"); return false; }				content = content.replace(reLast, "|Last=" + last_price); content = content.replace(rePrice, "|Price=" + price); content = content.replace(reLastDate, "|LastDate=" + last_date); content = content.replace(reDate, "|Date=");

xhr.onload = onload; xhr.open("POST", api); xhr.setRequestHeader("Content-Type",									"application/x-www-form-urlencoded"); xhr.send(					"action=edit&title=" + title + "&text=" + content +						"&token=" + encodeURIComponent(edittoken) +						"&summary=" + encodeURIComponent("Updated price from " + last_price + " to " + price + " via script on the exchange page.") +						"&basetimestamp=" + page.basetimestamp +						"&starttimestamp=" + page.starttimestamp				); };		h4.innerHTML = "Update the price of this item:"; p.innerHTML = "Please update using the market price (default exchange price) for this item.";

label.innerHTML = "New price:"; label.setAttribute("for", "gemwNewPrice");

input.id = "gemwNewPrice"; input.type = "text"; input.size = "9";

submit.style.marginTop = "0px"; submit.style.marginRight = "1em"; submit.style.marginBottom = "0px"; submit.style.marginLeft = "1em"; submit.id = "gemwSubmit"; submit.type = "submit"; submit.value = "Update"; submit.onclick = function { var xhr = new XMLHttpRequest; xhr.onload = onload1; xhr.open("GET", api + "&action=query&titles=" + title +							"&prop=info|revisions&intoken=edit&rvprop=content&rvlimit=1"); xhr.send; submit.disabled = true; return false; };

p2.appendChild(label); p2.appendChild(input); p2.appendChild(submit); form.appendChild(h4); form.appendChild(p); form.appendChild(p2);

gemw_guide.innerHTML = ""; gemw_guide.appendChild(form); }, false); }