[GC] - SDB Removal Improvements

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

目前為 2023-12-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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);
});