[GC] - SDB Removal Improvements

Never try to remove too many items from your SDB again!

// ==UserScript==
// @name        [GC] - SDB Removal Improvements
// @namespace   https://www.grundos.cafe
// @match       https://www.grundos.cafe/safetydeposit/*
// @license    MIT
// @version     1.0
// @author      Cupkait (& shoutout to Josh)
// @description Never try to remove too many items from your SDB again!
// ==/UserScript==

const createInventoryDiv = () => {
    const inventoryDiv = document.createElement('div');
    Object.assign(inventoryDiv.style, {
        position: 'fixed',
        bottom: '10px',
        left: '10px',
        padding: '10px',
        border: '1px solid #ccc',
    });
    document.body.appendChild(inventoryDiv);
    return inventoryDiv;
};

const updateInventoryInfo = async () => {
    try {
        const response = await fetch("/inventory/");
        if (!response.ok) throw new Error(`Network response was not ok: ${response.statusText}`);
        const html = await response.text();
        const inventoryHTML = $(html).find('main').find('p').eq(1).text();
        const currentItemCount = inventoryHTML.match(/You currently hold (.*?) items/)[1];
        const maxItemCount = inventoryHTML.match(/The maximum you should hold is (.*?)\./)[1];
        const availableSpaces = maxItemCount - currentItemCount;
        const remainSpaces = availableSpaces - countRemove;

        if (remainSpaces <= 0) {
            if (remainSpaces === 0) {
                inventoryDiv.innerHTML = 'You cannot select anymore!';
                inventoryDiv.style.background = 'orange';
            } else {
                inventoryDiv.innerHTML = 'You have too many selected!';
                inventoryDiv.style.background = 'red';
            }
        } else {
            inventoryDiv.innerHTML = `You can still remove <strong>${remainSpaces}</strong> more items.`;
            inventoryDiv.style.background = '#fff';
        }
    } catch (error) {
        console.error(error);
    }
};

let countRemove = 0;

const updateCountRemove = () => {
    countRemove = Array.from(document.querySelectorAll('input.form-control.rm'))
        .map(input => {
            const listedQuantity = parseInt(input.getAttribute('data-qty')) || 0;
            const enteredValue = parseInt(input.value) || 0;
            return Math.min(enteredValue, listedQuantity);
        })
        .reduce((sum, value) => sum + value, 0);

    console.log('countRemove:', countRemove);
    updateInventoryInfo();
};

const inventoryDiv = createInventoryDiv();
updateInventoryInfo();

document.querySelectorAll('input.form-control.rm').forEach(input => {
    input.addEventListener('input', updateCountRemove);
});