Company Stock update

Updates company stock based on given numbers or percentages.

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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


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

/* 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".
*/

// 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, pos = 0, maxStorage = 0;
var url = "https://api.torn.com/company/?selections=stock&key=" + APIkey;
var stockItems = [];
$("#ui-id-8").click(function() {
    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);
                }
            }
        });
    }
});