// ==UserScript==
// @name SB Item Market Magic Extended
// @namespace http://aperson150.yahoo.com
// @description Upgrades the Item Market in several ways.
// @author Dextarrrrr, Azander
// @version 1.0.0
// @include *spybattle.com/*itemmarket.php*
// @include *spybattle.com/*inventory.php
// @include *spybattle.com/*equip.php*
// @include *spybattle.com/*sellitem.php*
// @include *spybattle.com/*putonmarket.php*
// @include *spybattle.com/*itemguide.php*
// ==/UserScript==
function xpath(query) {
return document.evaluate(query, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}
function trim(str) {
//Thanks to: http://blog.stevenlevithan.com/archives/faster-trim-javascript
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
function rangeText(stored) {
return 'Standard Sell: ' + stored[0] + ' - Current market range: $' + stored[1] + ' to $' + stored[2];
}
function showHoverBox(event, item) {
var body = document.getElementsByTagName('body')[0];
var hover = document.createElement('div');
hover.setAttribute('id', 'priceInfo');
hover.setAttribute('style', 'position:absolute; top:' + parseInt((event.pageY * 1)+1,10) + 'px; left:' + parseInt((event.pageX*1+1)) + 'px; background-color:black; font-size:1.5em; padding:0.2em;');
hover.innerHTML = rangeText(info[item]);
body.appendChild(hover);
}
function removeHoverBox() {
document.getElementsByTagName('body')[0].removeChild(document.getElementById("priceInfo"));
}
function addHoverJavascript(info) {
var body = document.getElementsByTagName("body")[0];
var script = document.createElement('script');
script.setAttribute('type','text/javascript');
var infoText = "var info = " + info.toSource();
//Add the assignment text and functions to the document
script.innerHTML = infoText + "\n\n" + showHoverBox + "\n\n" + removeHoverBox + "\n\n" + rangeText;
body.appendChild(script);
}
function addValues(tbody, values) {
//Loop through the values, displaying them on the screen.
for (var rarity in values) {
var td = document.createElement('td');
var tr = document.createElement('tr');
//Attach spacer
tr.appendChild(td);
td = document.createElement('td');
td.innerHTML = rarity;
tr.appendChild(td);
td = document.createElement('td');
//Attach spacer
tr.appendChild(td);
var value;
if (values[rarity][1]) {
//Format for cash display
value = String(values[rarity][0]);
var rgx = /(\d+)(\d{3})/;
while (rgx.test(value)) {
value = value.replace(rgx, "$1" + "," + "$2");
}
value = "$" + value;
} else {
//Format for points display
value = String(values[rarity][0]) + " point";
if (values[rarity][0] != 1)
value += "s";
}
td = document.createElement('td');
td.innerHTML = value;
tr.appendChild(td);
tbody.appendChild(tr);
}
}
if (/itemmarket.php/.test(window.location.href) || /putonmarket.php\?id=[\d]+&put=true/.test(window.location.href)) {
var market = xpath("//td[@class='contentcontent']");
if(market.snapshotLength > 0) {
var info = new Array();
var marketel = market.snapshotItem(market.snapshotLength - 1);
//Get the filter buttons HTML
var pos = marketel.innerHTML.search("
") + 8;
var filters = marketel.innerHTML.substring(0, pos);
var items = marketel.innerHTML.substring(pos).split("
");
var lastitem = "";
var output = "", thisitem = new Array(), thisoutput = new Array();
var lowest = new Array();
var highest = new Array();
var result;
var reItemId = /itemWindowFunction\(([0-9]+)\);"[^>]*?>([^<]+)<\/a>/i;
var rePrice = /\]\s+-\s+\$([0-9,]+)/i;
for(i in items) {
var item = trim(items[i]);
result = item.match(reItemId);
if(result != null) {
var itemid = parseInt(result[1], 10);
var thisprice = item.match(rePrice);
//Need to keep the string version for displaying and the integer version for comparing (to find a new high or low)
priceStr = thisprice[1];
price = parseInt(priceStr.replace(/,/g, ""), 10);
if(typeof(lowest[itemid]) == "undefined") {
lowest[itemid] = [price, priceStr];
highest[itemid] = [price, priceStr];
thisoutput[itemid] = "";
}
else {
if (price >= highest[itemid][0]) {
highest[itemid] = [price, priceStr];
}
if (price <= lowest[itemid][0]) {
lowest[itemid] = [price, priceStr];
}
}
thisitem[itemid] = result[2];
thisoutput[itemid] += item + "
";
}
}
for(i in thisitem) {
//Get the stored values so that the "Sell" value can be stored back correctly
var stored = GM_getValue(i, "$0" + lowest[i][1] + "" + highest[i][1]).split("");
//Store the current low and high (overwriting the previous low and high)
GM_setValue(i, stored[0] + "" + lowest[i][1] + "" + highest[i][1]);
//Set the stored array with the new data
stored[1] = lowest[i][1];
stored[2] = highest[i][1];
info[i] = stored.slice();
output += '