Vault - Max $

Automatically maxes out the deposit input in a vault

// ==UserScript==
// @name         Vault - Max $
// @namespace    Titanic_
// @version      1.3
// @description  Automatically maxes out the deposit input in a vault
// @license      MIT
// @author       Titanic_ [2968477]
// @match        https://www.torn.com/properties.php*
// @grant        window.onurlchange
// ==/UserScript==

// YOU CAN CHANGE THESE
const beepLifeChanged = false // by default, it will not make a sound alert if your life bar changes
const beepMoneyChanged = false // by default, it will not make a sound alert if your wallet amount changes

// DONT TOUCH BELOW
let storedLife = 0;
let storedMoney = 0;
let ran = 0;

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

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

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

function max() {
    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 value = parseInt($inputHidden.value) || 0;
    let amountOnHand = parseInt($("[class^='value_']").attr("data-money"));

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

    var button = $('input[type="submit"].torn-btn.disabled[value="DEPOSIT"][disabled]');
    button.prop('disabled', false).removeClass("disabled");
}

function checkStats() {
    if(!ran) {
        if(!storedMoney) {
            let onHand = parseInt($("[class^='value_']").attr("data-money"))
            onHand = Number(onHand)

            storedMoney = onHand;
            console.log("Updated stored money to", storedMoney);
        }
        if(!storedLife) {
            let lifeBar = $("[class*='bar'][class*='life']");
            let lifeVal = $(lifeBar).find("[class*='bar-value']").text().split("/")[0].trim();
            lifeVal = Number(lifeVal);

            storedLife = lifeVal;
            console.log("Updated stored life to", storedLife);
        }

        ran = 1;
        return;
    }
    console.log("Checking stats");

    let lifeBar = $("[class*='bar'][class*='life']");
    let lifeVal = $(lifeBar).find("[class*='bar-value']").text().split("/")[0].trim();
    lifeVal = Number(lifeVal);

    let onHand = parseInt($("[class^='value_']").attr("data-money"))
    onHand = Number(onHand)

    if(onHand !== storedMoney && beepMoneyChanged) {
        storedMoney = onHand;
        beep();
    }

    if(lifeVal !== storedLife && beepLifeChanged) {
        beep();
        storedLife = lifeVal;
    }
}

var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext);

function beep(duration, frequency, volume, type, callback) {
    var oscillator = audioCtx.createOscillator();
    var gainNode = audioCtx.createGain();

    oscillator.connect(gainNode);
    gainNode.connect(audioCtx.destination);

    if (volume){gainNode.gain.value = volume;}
    if (frequency){oscillator.frequency.value = frequency;}
    if (type){oscillator.type = type;}
    if (callback){oscillator.onended = callback;}

    oscillator.start(audioCtx.currentTime);
    oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
};