Torn Price RRP Auto Input

Automatically sets the price in the item listing page with a button click on Torn.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn Price RRP Auto Input
// @namespace    http://tampermonkey.net/
// @version      2.6
// @description  Automatically sets the price in the item listing page with a button click on Torn.com
// @author       Gemini 2.0 https://gemini.google.com/ and OctaviusRW https://www.torn.com/profiles.php?XID=3421627
// @match        https://www.torn.com/page.php?sid=ItemMarket*
// @match        https://*.torn.com/page.php?sid=ItemMarket*
// @license      GNU GPLv3 
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addButtonsToPriceElements() {
        const priceElements = document.querySelectorAll('div[class*="price___"]');

        priceElements.forEach(priceElement => {
            if (priceElement.querySelector('.tampermonkey-price-button')) return;

            const button = document.createElement('button');
            button.textContent = '$'; // Shorter button text
            button.classList.add('tampermonkey-price-button');

            button.addEventListener('click', () => {
                const priceText = priceElement.textContent.trim();
                const priceMatch = priceText.match(/[\$£€]?[\s]*([\d,.]+)/i);

                if (priceMatch) {
                    const price = priceMatch[1].trim();
                    const rowElement = priceElement.closest('div[class*="itemRow___"]');

                    if (rowElement) {
                        const inputMoney = rowElement.querySelector('div.input-money-group input[data-money="Infinity"]');

                        if (inputMoney) {
                            inputMoney.value = price;
                            inputMoney.dispatchEvent(new Event('input', { 'bubbles': true, 'cancelable': true }));
                            inputMoney.dispatchEvent(new Event('change', { 'bubbles': true }));
                            inputMoney.dispatchEvent(new Event('blur'));
                        } else {
                            alert("Could not find the price input in this row.");
                        }
                    } else {
                        alert("Could not find the product row. Check the HTML.");
                    }
                } else {
                    alert("Could not find price.");
                }
            });

            priceElement.appendChild(button);
        });
    }

    addButtonsToPriceElements();
    const observer = new MutationObserver(mutations => {
        let nodesAdded = false;
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length > 0) {
                nodesAdded = true;
            }
        });
        if (nodesAdded) {
            addButtonsToPriceElements();
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

    const style = document.createElement('style');
    style.textContent = `
        .tampermonkey-price-button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 2px 5px; /* Smaller padding */
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 12px; /* Smaller font */
            cursor: pointer;
            border-radius: 3px; /* Smaller border radius */
            margin-left: 3px; /* Reduced margin */
            line-height: 1; /* Added line-height to control vertical spacing */
        }
        .tampermonkey-price-button:hover {
            background-color: #45a049;
        }
    `;
    document.head.appendChild(style);

    console.log("Set Price to Input script running.");
})();