brqqq / Calc money to send

// ==UserScript==
// @name         Calc money to send
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  Calculates how much money to send to each account for stocking
// @author       Burak
// @match        *://mob-robot.xyz/resources/stocklists*
// @grant        none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @require https://gist.githubusercontent.com/Brqqq/3f3a2823bb866dcb7e0aa9b81aaa0589/raw/8d12067a580f351c0b514c51364139a9fac8438b/waitForKeyElements.js
// @updateURL https://openuserjs.org/meta/brqqq/Calc_money_to_send.meta.js
// @downloadURL https://openuserjs.org/install/brqqq/Calc_money_to_send.user.js
// @copyright 2021, Burak (https://openuserjs.org/users/brqqq)
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  const start = () => {
    const startButton = document.createElement("button");

    let bulletPrice = 0;

    const onclick = () => {
      bulletPrice = +prompt("What is the bullet price");
      if (Number.isNaN(bulletPrice)) {
        alert("That price is not a number");
        return;
      }

      startButton.textContent = `Bullet price € ${bulletPrice.toLocaleString()} (refresh to change)`;
      startButton.disabled = true;

      const newTh = document.createElement("th");
      const thText = document.createTextNode("Money to send")
      newTh.appendChild(thText);
      newTh.className = "text-left"
      document.querySelectorAll("thead tr")[0].insertBefore(newTh, document.querySelectorAll("thead tr")[0].children[6]);

      const trs = Array.from(document.querySelectorAll("tr")).slice(1)

      for (let tr of trs) {
        const neededBullets = (+tr.children[2].innerText.replace(/[^0-9]/g, "")) * 1000;
        const newTd = document.createElement("td");
        const tdText = document.createTextNode("-");
        newTd.appendChild(tdText);
        tr.insertBefore(newTd, tr.children[6]);

        if (neededBullets == 0) {
          continue;
        }

        const cash = +tr.children[5].innerText.replace(/[^0-9]/g, "");
        const currentBullets = +tr.children[7].innerText.replace(/[^0-9]/g, "");

        const bulletsCost = (neededBullets - currentBullets) * bulletPrice;

        const moneyToSend = Math.trunc((bulletsCost - cash) / 0.9);
        const roundedMoneyToSend = Math.max(0, Math.ceil(moneyToSend / 1000000) * 1000000);

        tdText.textContent = "€ " + roundedMoneyToSend.toLocaleString();

      }
    }

    startButton.className = "btn btn-default btn-primary inline-flex items-center relative";
    startButton.style["margin-bottom"] = "12px";
    startButton.onclick = onclick;
    const buttonContent = document.createElement("div");
    buttonContent.innerText = "Calculate money to send";
    startButton.appendChild(buttonContent);

    const tableDiv = document.querySelector("#nova > div > div.content > div.px-view.py-view.mx-auto > div.relative > div.card");
    tableDiv.parentNode.insertBefore(startButton, tableDiv);
  }

  waitForKeyElements("thead tr", start);

})();