Torn Extensions - Stock Order

Helps to order stock for your company.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Torn Extensions - Stock Order
// @namespace    TornExtensions
// @version      1.1
// @description  Helps to order stock for your company.
// @author       Mathias [XID 1918010]
// @match        https://www.torn.com/companies.php*
// @grant        none
// ==/UserScript==

(function() {
    //'use strict';
    let APIKey = "YOUR API HERE";
    let targetNode = document.getElementById('stock');
    let config = { childList: true };
    let onItsWay = 0;
    let callback = function(mutationsList, observer) {
        $(".total-price.bold").after("<div class=\"total-stock-container bold\">Total stock:</span> <span class=\"total-stock\"></span></div><br />");
        $(".total-stock-container").css("padding", "10px 10px 0");
        $(".input-money").change(() => {
            calcStock();
        });
        calcStock();
        let API = `https://api.torn.com/company/?selections=stock&key=${APIKey}`;
        fetch(API)
          .then((res) => res.json())
          .then((res) => {
            console.log(API);
            let stockstr = "";
            stockstr += "<br /><br /><h4>Torn Extensions - Stock Order</h4><table><tr><th>Product</th><th>In stock</th><th>Sold yesterday</th></tr>";
            $.each(res.company_stock, (k, v) => {
                onItsWay += v.on_order;
                stockstr += `<tr><td>${k}</td><td>${v.in_stock.toLocaleString("en-US")}(${(v.in_stock + v.on_order).toLocaleString("en-US")})</td><td>${v.sold_amount.toLocaleString("en-US")}</td></tr>`;
            });
            $(".clear-all.t-blue.h.m-left10.c-pointer").after(stockstr + "</table>");
            $("table,th,td").css("border", "1px solid black").css("border-collapse", "collapse");
            $("th,td").css("padding", "5px");
            $(".total-stock").text((parseInt($(".total-stock").text().replace(",", "")) + onItsWay).toLocaleString("en-US"));
        });
    };

    let observer = new MutationObserver(callback);
    observer.observe(targetNode, config);

    function calcStock() {
        $(".total-stock").text((parseInt($(".quantity.bold").text().replace(/,/g, "")) + parseInt($(".stock.bold").text().replace(/,/g, "")) + onItsWay).toLocaleString("en-US"));
    }
})();