Vault Withdraw Buttons

Adds buttons to remove at million $ intervals to the vault page to make it easier to manage money in ghost trades.

// ==UserScript==
// @name         Vault Withdraw Buttons
// @namespace    Titanic_
// @version      1.3
// @description  Adds buttons to remove at million $ intervals to the vault page to make it easier to manage money in ghost trades.
// @license      MIT
// @author       Titanic_
// @match        https://www.torn.com/properties.php*
// @grant        none
// ==/UserScript==

let playMoney = 10000000; // Set this for magic button

function addElements() {
    let div;
    let parent = document.createElement("div");
    parent.id = "custom-buttons";

    addButton(parent, "-1m", 1000000);
    addButton(parent, "-5m", 5000000);
    addButton(parent, "-10m", 10000000);
    addPasteButton(parent);
    addMagicButton(parent);

    div = document.querySelectorAll("div.clear")[3];

    if (div && !document.getElementById("custom-buttons")) {
        div.parentNode.insertBefore(parent, div.nextSibling);
    }
}

function addButton(parent, label, amount) {
    let btn = document.createElement("input");
    btn.value = label;
    btn.type = "button";
    btn.classList.add("torn-btn");

    btn.addEventListener("click", () => {
        let $inputVisible = document.querySelector("form.left > div.torn-divider > div.input-money-group > input.input-money");
        let $inputHidden = document.querySelectorAll("form.left > div.torn-divider > div.input-money-group > input.input-money")[1];
        let value = parseInt($inputHidden.value);

        if(isNaN(value)) { value = 0 }
        $inputVisible.value = amount + value;
        $inputVisible.dispatchEvent(new Event("input", { bubbles: true }));
    });

    parent.appendChild(btn);
}

function addPasteButton(parent) {
    let btn = document.createElement("input");
    btn.value = "Paste";
    btn.type = "button";
    btn.classList.add("torn-btn");

    btn.addEventListener("click", () => {
        let $inputVisible = document.querySelector("form.left > div.torn-divider > div.input-money-group > input.input-money");
        let $inputHidden = document.querySelectorAll("form.left > div.torn-divider > div.input-money-group > input.input-money")[1];
        navigator.clipboard.readText().then((clipboardValue) => {
            clipboardValue = clipboardValue.replace(/[, $]/g, '');

            if (!isNaN(clipboardValue) && parseInt(clipboardValue)) {
                $inputVisible.value = parseInt(clipboardValue);
                $inputVisible.dispatchEvent(new Event("input", { bubbles: true }));
            } else {
                alert("Not a number");
            }
        });
    });

    parent.appendChild(btn);
}

function addMagicButton(parent) {
    let btn = document.createElement("input");
    btn.value = "Magic";
    btn.type = "button";
    btn.classList.add("torn-btn");

    btn.addEventListener("click", () => {
        let $inputVisible = document.querySelector("form.right > div.torn-divider > div.input-money-group > input.input-money");
        let $inputHidden = document.querySelectorAll("form.right > div.torn-divider > div.input-money-group > input.input-money")[1];
        let $widthrawVisible = document.querySelector("form.left > div.torn-divider > div.input-money-group > input.input-money");
        let $withdrawHidden = document.querySelectorAll("form.left > div.torn-divider > div.input-money-group > input.input-money")[1];

        let amountOnHand = $("[class^='value_']").attr("data-money");
        if(amountOnHand < playMoney) {
            $widthrawVisible.value = (playMoney - amountOnHand);
            $widthrawVisible.dispatchEvent(new Event("input", { bubbles: true }));
        } else if(playMoney < amountOnHand) {
            $inputVisible.value = (amountOnHand - playMoney);
            $inputVisible.dispatchEvent(new Event("input", { bubbles: true }));
        }
    });

    parent.appendChild(btn);
}

function inputCheck() {
    setTimeout(function() {
        if (document.querySelector('.input-money-group > .input-money')) {
            addElements();
        }
    }, 300);
}

const observer = new MutationObserver(() => {
    if (window.location.href.includes("tab=vault")) {
        inputCheck();
    }
});

observer.observe(document.body, { childList: true, subtree: true });

if (window.location.href.includes("tab=vault")) {
    inputCheck();
}

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

addGlobalStyle('#custom-buttons { justify-content: center; display: flex; gap: 2px; padding-top: 2.5px; padding-bottom: 2.5px; }');
addGlobalStyle('#custom-buttons > input:nth-of-type(5) { background: #6a0dad; color: #ffffff; border: 1.5px solid #8a2be2; border-radius: 12px; font-size: 16px; box-shadow: 0 0 10px rgba(138, 43, 226, 0.5), 0 0 20px rgba(138, 43, 226, 0.5), 0 0 30px rgba(138, 43, 226, 0.5); text-shadow: 0 0 5px rgba(255, 255, 255, 0.5); transition: transform 0.3s ease, box-shadow 0.3s ease; }');
addGlobalStyle('#custom-buttons > input:nth-of-type(5):hover { transform: scale(1.1); box-shadow: 0 0 20px rgba(138, 43, 226, 0.7), 0 0 30px rgba(138, 43, 226, 0.7), 0 0 40px rgba(138, 43, 226, 0.7); }');
addGlobalStyle('@media only screen and (max-width: 600px) { #custom-buttons { justify-content: space-between; } }');