Torn: Gimme Beers, Empty BB, Gas, Bricks

Gimme Beers, Empty blood bags, Gas and Bricks!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Torn: Gimme Beers, Empty BB, Gas, Bricks
// @namespace      lugburz.gimme_beers
// @version        0.1.5
// @description    Gimme Beers, Empty blood bags, Gas and Bricks!
// @author         Lugburz, zstorm [2268511]
// @match          https://www.torn.com/shops.php?step=bitsnbobs*
// @match          https://www.torn.com/shops.php?step=pharmacy*
// @grant          none
// @icon           https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @license         MIT
// ==/UserScript==

//====================UPDATE:===============================
// 0.1.5  Added buttons for Gas and Bricks.
// 0.1.4  Added another buttons at Pharmacy to buy Empty Blood Bag.
// 0.1.3  Derived the script from lugburz.gimme_beers.
//========================================================

(function() {
    'use strict';

    // Configuration for all items (ID: Item ID, Area: shoparea value, Name: button text)
    const ITEMS = {
        'bitsnbobs': [
            { id: 180, area: 103, name: 'Beers', elementId: 'buyBeerBtn' },
            { id: 172, area: 103, name: 'Gas', elementId: 'buyGasBtn' },
            { id: 394, area: 103, name: 'Bricks', elementId: 'buyBrickBtn' }
        ],
        'pharmacy': [
            { id: 731, area: 110, name: 'Empty Blood Bag', elementId: 'buyEBBBtn' }
        ]
    };

    const STATUS_ELEMENT_ID = 'shopResultStatus';

    /**
     * Creates and adds a universal purchase button for a given item configuration.
     * @param {Object} itemConfig - The configuration object for the item.
     */
    function addPurchaseButton(itemConfig) {
        const { id, area, name, elementId } = itemConfig;

        // Only add the button, not a result span
        const buttonHtml = `<button id="${elementId}" style="color: var(--default-blue-color); cursor: pointer; margin-left:5;">${name}</button>`;

        $('div.content-title > h4').append(buttonHtml);

        $(`#${elementId}`).on('click', async () => {
            $(`#${STATUS_ELEMENT_ID}`).text(''); // Clear previous status
            await getAction({
                type: 'post',
                action: 'shops.php',
                data: {
                    step: 'buyShopItem',
                    ID: id,
                    amount: 100,
                    shoparea: area
                },
                success: (str) => {
                    try {
                        const msg = JSON.parse(str);
                        // Update the single shared status element
                        $(`#${STATUS_ELEMENT_ID}`).html(msg.text).css('color', msg.success ? 'green' : 'red');
                    } catch (e) {
                        console.error("Error parsing purchase response:", e, str);
                    }
                }
            });
        });
    }

    // --- Main Execution ---
    const currentUrl = window.location.href;
    const urlParams = new URLSearchParams(currentUrl.split('?')[1]);
    const step = urlParams.get('step');
    const headerElement = $('div.content-title > h4');

    if (step && ITEMS[step] && headerElement.length > 0) {
        // 1. Add all buttons for the current shop page
        ITEMS[step].forEach(item => addPurchaseButton(item));

        // 2. Add the single shared status element after all buttons
        const statusHtml = `<span id="${STATUS_ELEMENT_ID}" style="font-size: 12px; font-weight: 100; margin-left: 10px;"></span>`;
        headerElement.append(statusHtml);
    }
})();