Company Stock update

Updates company stock based on given numbers or percentages.

当前为 2018-05-04 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Company Stock update
// @namespace    http://tampermonkey.net/
// @version      1.7.4
// @description  Updates company stock based on given numbers or percentages.
// @author       LordBusiness
// @match        https://www.torn.com/companies.php
// @run-at       document-idle
// @grant        none
// ==/UserScript==


// You need to add the prices you want in order.
var isPercentage = true;
var APIkey = "KPf3Spnv";
var PriceArray = ["8%", "16%", "16%", "8%", "8%", "8%", "8%", "24%"];

/* Examples
var isPercentage = false;
var APIkey = "";
var PriceArray = ["560", "4500", "9000", "500", "1000"];

var isPercentage = true;
var APIkey = "UGdo6gia";
var PriceArray = ["20%", "25%", "5%", "11%", "9%", "13%", "14%"];


Note: * If you use percentages, make sure all of them added are below 100.
      * Make sure Price Array has the exact number of things to order. If you don't need to order, leave it as "0".
*/
var runWhen = setInterval(function() {
    if (window.jQuery) {
        clearInterval(runWhen);
        runOnlyNow();
    }
}, 1000);
function runOnlyNow() {
    // a minimal jQuery library for reacting to innerHTML changes
    (function($) {
        $.fn.change = function(cb, e) {
            e = e || { subtree:true, childList:true, characterData:true };
            $(this).each(function() {
                function callback(changes) { cb.call(node, changes, this); }
                var node = this;
                (new MutationObserver(callback)).observe(node, e);
            });
        };
    })(jQuery);

    var storageURL = "https://api.torn.com/company/?selections=detailed&key=" + APIkey;

    var flag = 0, flag2 = 0, pos = 0, maxStorage = 0;
    var url = "https://api.torn.com/company/?selections=stock&key=" + APIkey;
    var stockItems = [];

    $("#ui-id-8").click(updateStock);
    $("#manage-tabs").change(function() {
        if($("#manage-tabs option:selected").val() == "stock") {
            updateStock();
        }
    });
    function updateStock() {
        if(flag2 == 0) {
            flag2 = 1;
            if(isPercentage) {
                $.getJSON( storageURL )
                    .done(function( json ) {
                    maxStorage = parseInt(json.company_detailed.upgrades.storage_space);
                    var sumPercent = 0;
                    for(var i=0;i<PriceArray.length;i++) {
                        PriceArray[i] = PriceArray[i].replace('%', '');
                        sumPercent += parseInt(PriceArray[i]);
                        PriceArray[i] = parseInt(PriceArray[i]) * maxStorage / 100;
                        console.log(sumPercent);
                    }
                    console.log(sumPercent, PriceArray);
                    $.getJSON( url )
                        .done(function( json ) {
                        for (var key in json.company_stock) {
                            console.log(1, key);
                            if(pos == 0) {
                                stockItems = [key];
                                pos = 1;
                                console.log(2, stockItems);
                            }
                            else {
                                for(var i=0; i < stockItems.length; i++) {
                                    var stockItemelem = parseInt(json.company_stock[stockItems[i]].rrp);
                                    var currentelem = parseInt(json.company_stock[key].rrp);
                                    if(stockItemelem >= currentelem) {
                                        if((stockItemelem == currentelem) && (String(key).localeCompare(stockItems[i]) == 1))
                                            if(++i == stockItems.length) {
                                                stockItems.push(key);
                                                break;
                                            }
                                        stockItems.splice(i, 0, key);
                                        console.log(3, stockItems);
                                        break;
                                    }
                                    else if(i == stockItems.length-1) {
                                        stockItems.push(key);
                                        break;
                                    }
                                }
                            }
                        }
                        for(var j=0; j<stockItems.length; j++) {
                            PriceArray[j] = PriceArray[j] - parseInt(json.company_stock[stockItems[j]].in_stock) - parseInt(json.company_stock[stockItems[j]].on_order);
                        }
                        console.log(PriceArray);
                        if($(".stock-list-wrap").length > 0) {
                            try {
                                var k = 0;
                                $(".quantity > input").each(function() {
                                    if(PriceArray[k] > 0) {
                                        $(this).focus();
                                        $(this).val(PriceArray[k]);
                                        $(this).keydown();
                                        $(this).keypress();
                                        $(this).keyup();
                                        $(this).blur();
                                    }
                                    k++;
                                });
                                k = 0;
                            }
                            catch(err) {
                                console.log("Err:" + err);
                            }
                        }
                    })
                        .fail(function( jqxhr, textStatus, error ) {
                        console.log( "Request Failed: " + textStatus + ", " + error );
                    });
                })
                    .fail(function( jqxhr, textStatus, error ) {
                    console.log( "Request Failed: " + textStatus + ", " + error );
                });
            }
            else {
                $('#stock').change(function(changes, observer) {
                    if(($(".stock-list-wrap").length > 0) && (flag === 0)) {
                        flag = 1;
                        console.log("p");
                        try {
                            var i = 0;
                            $(".quantity > input").each(function() {
                                if(PriceArray[i] > 0) {
                                    $(this).focus();
                                    $(this).val(PriceArray[i]);
                                    $(this).keydown();
                                    $(this).keypress();
                                    $(this).keyup();
                                    $(this).blur();
                                }
                                i++;
                            });
                            i = 0;
                        }
                        catch(err) {
                            console.log("Err:" + err);
                        }
                    }
                });
            }
        }
    }
}