rsenderakgmail.com / WFEasyTrading

// ==UserScript==
// @name           WFEasyTrading
// @namespace      sk.seko
// @description    Trading station helper: tweaks Sell All button, adds Buy All button and provides resource availability coloring
// @include        http://*.war-facts.com/viewstations.php*
// @include        http://*.war-facts.com/worldtrade.php*
// @version        1.1
// @grant          none
// ==/UserScript==

// 1.0: initial implementation: coloring + sell all amount adjustment for trade stations
// 1.1: added Build All button for trade stations

// coloring: configuration
var use_coloring = true;
var coloring_able = '#01DFA5';
var coloring_partially = '#F2F5A9';
var coloring_unable = '#F78181';

// trading helper - Sell All button twaek
var use_sell_all = true;

// trading helper - Buy All button
var use_buy_all = true;


// --- actual code: don't touch! ---

function colorize_stations(message, color) {
    var nodes = document.evaluate("//small[contains(text(),'" + message + "')]/..",
                document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    for (var i = 0; i < nodes.snapshotLength; i++) {
        nodes.snapshotItem(i).setAttribute('style', 'background-color:' + color + ';');
    }
}

window.coloring_listener = function () {
    colorize_stations('This station is able to complete your order for', coloring_able)
    //colorize_stations('This station can partially fill your order for', coloring_partially)
    colorize_stations('This station is unable to complete your order', coloring_unable)
}

window.sell_all_listener = function () {
    
    window.checkLimits = function(item) {
        
        function sumCargos(item) {
            var all = ['iron','copper','silver','titanium','gold','uranium','platinum','diamonds','oil','water','food'];
            var sum = 0;
            for (cargo in all) {
                if (item != all[cargo]) {
                    var node = document.getElementById(all[cargo]);
                    if (node) sum += parseFloat(node.value);
                }
            }
            return sum;
        }
        
        var x = document.evaluate("//td/strong[text()='Storage:']/../following-sibling::node()/strong/text()",
                document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        var maxamount = parseFloat(x.textContent.replace(/,/g,'')) - sumCargos(item); 
        var node = document.getElementById(item);
        if (maxamount < node.value) {
            node.value = maxamount;
        }
    }
    
    var nodes = document.evaluate("//input[@value='Sell All']",
                document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    for (var i = 0; i < nodes.snapshotLength; i++) {
        var node = nodes.snapshotItem(i)
        var oldscript = node.getAttribute('onClick');
        var item = oldscript.replace(/^sa/,'').replace(/\(\);/, '')
        node.setAttribute('onClick', oldscript + ';checkLimits("' + item + '");');
    }
}

window.buy_all_listener = function () {
    
    window.buyall = function(item) {
        var node = document.getElementById(item);
        node.value = 999999999999;
        var node = document.getElementById('buy' + item);
        node.click()
    }
    
    var nodes = document.evaluate("//input[@value='Sell All']",
                document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    for (var i = 0; i < nodes.snapshotLength; i++) {
        var node = nodes.snapshotItem(i)
        var oldscript = node.getAttribute('onClick');
        var item = oldscript.replace(/^sa/,'').replace(/\(\);/, '')
        var buyall = document.createElement('input');
        buyall.setAttribute('type', 'button');
        buyall.setAttribute('value', 'Buy All');
        buyall.setAttribute('class', 'small');
        var n = document.evaluate(".//input[@type='text']", node.parentNode.parentNode, null, 
                XPathResult.ORDERED_NODE_ITERATOR_TYPE, null).iterateNext();             
        buyall.setAttribute('onclick', 'buyall("' + n.name + '");');
        node.parentNode.insertBefore(buyall, node.nextSibling); 
    }
}

if (use_coloring && window.location.href.indexOf('/viewstations.php') != -1) { 
    window.addEventListener("load", coloring_listener, false);
}

if (use_sell_all && window.location.href.indexOf('/worldtrade.php') != -1) { 
    window.addEventListener("load", sell_all_listener, false);
}

if (use_buy_all && window.location.href.indexOf('/worldtrade.php') != -1) { 
    window.addEventListener("load", buy_all_listener, false);
}