Torn company stock order

Automatically calculate stock percent based on sale and enter the order amount to order up to max capasity.

La data de 05-08-2018. Vezi ultima versiune.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Torn company stock order
// @version      0.1.0
// @description  Automatically calculate stock percent based on sale and enter the order amount to order up to max capasity.
// @author       j0hnZen
// @match        https://www.torn.com/*
// @run-at       document-start
// @grant        none
// @namespace    j0hnZen
// ==/UserScript==

// check that script is run on right page
if (window.location.pathname == "/companies.php") {
    window.onload = function() {

        //add CSS for API key entry
        var style_api = document.createElement("style");
        style_api.type = "text/css";
        style_api.innerHTML = `
         #apiKey {
        height: 18px;
        font-size: 1.5em;
        width: 400px;
        font-family: Verdana;
        letter-spacing: .05em;
        margin-left: 5px;
        margin-bottom: 5px;
        padding: 5px;
        }
        `
        document.head.appendChild(style_api);

        //Add observer to triger when the stock tab is selected
        var targetNode = document.getElementById("stock");
        var config = { attributes: true, childList: true, subtree: true };
        var callback = function(mutationsList) {
            for(var mutation of mutationsList) {
                if (mutation.type == 'childList') {
                    if (addUI()){
                        var urlStock = 'https://api.torn.com/company/?selections=stock&key=';
                        var urlStorage = 'https://api.torn.com/company/?selections=detailed&key=';
                        urlStock += localStorage.getItem("_apiKey") ? localStorage.getItem("_apiKey") : '';
                        urlStorage += localStorage.getItem("_apiKey") ? localStorage.getItem("_apiKey") : '';
                        var storage

                        fetch(urlStorage)
                            .then(function(response) {
                            if(response.ok) {
                                return response.json();
                            }
                            throw new Error('Network response was not ok.');
                        })
                            .then(function(myJson) {
                            if (myJson.error){
                                throw new Error(myJson.error.error);
                            }
                            storage = myJson.company_detailed.upgrades.storage_space;
                        });

                        fetch(urlStock)
                            .then(function(response) {
                            if(response.ok) {
                                return response.json();
                            }
                            throw new Error('Network response was not ok.');
                        })
                            .then(function(myJson) {
                            if (myJson.error){
                                throw new Error(myJson.error.error);
                            }
                            var total = 0;
                            for (var key1 in myJson.company_stock) {
                                total += myJson.company_stock[key1].sold_amount;
                            }

                            for (var key in myJson.company_stock) {
                                if (myJson.company_stock.hasOwnProperty(key)) {
                                    var orderPercent = myJson.company_stock[key].sold_amount/total
                                    var orderAmount = 0;

                                    orderAmount = Math.floor((storage * orderPercent) - myJson.company_stock[key].in_stock - myJson.company_stock[key].on_order) > 0 ? Math.floor((250000 * orderPercent) - myJson.company_stock[key].in_stock - myJson.company_stock[key].on_order): 0;

                                    $( ".stock-list.fm-list.t-blue-cont.h" ).find("div:contains("+key+")").parent().find(".quantity").find("input").val(orderAmount);
                                    $( ".stock-list.fm-list.t-blue-cont.h" ).find("div:contains("+key+")").parent().find(".quantity").find("input").trigger('keyup');
                                }
                            }
                        }).catch(function(error) {
                            console.log('There has been a problem with your fetch operation: ', error.message);
                        });
                        break;
                    }
                }
            }
        };
        // Create an observer instance linked to the callback function
        var observer = new MutationObserver(callback);

        // Start observing the target node for configured mutations
        observer.observe(targetNode, config);
    }
}

function addUI (){
    if ( document.getElementsByClassName("StockAutoFill").length == 0 ){
        var div_api = document.createElement("div");
        div_api.className = "StockAutoFill";
        var apiKey = document.createElement("input");
        apiKey.type = "text";
        apiKey.id = "apiKey";
        apiKey.placeholder = "Enter API key here";
        apiKey.value = localStorage.getItem("_apiKey") ? localStorage.getItem("_apiKey") : "";
        div_api.appendChild(apiKey);

        var button_save = document.createElement("button");
        button_save.type = "button";
        button_save.innerText = "Save";
        button_save.id = "btn_save";
        div_api.appendChild(button_save);

        var cw = document.getElementById("stock").getElementsByClassName("stock-list-wrap");
        cw[0].parentNode.insertBefore(div_api, cw[0]);

        document.getElementById("btn_save").addEventListener("click", function() {
            var key = document.getElementById("apiKey");
            localStorage.setItem("_apiKey", key.value);
        });
        return true;
    }
    else{
        return false;
    }
}