Replace "Available" and Make Attack-Links Clickable

Replaces "available" with attack links and makes them clickable, skipping anonymous sellers.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Replace "Available" and Make Attack-Links Clickable
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Replaces "available" with attack links and makes them clickable, skipping anonymous sellers.
// @author       Grance [3487987]
// @match        *://www.torn.com/page.php?sid=ItemMarket*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Function to replace "available" with attack links based on extracted IDs
    function replaceAvailableWithAttackLinks() {
        const sellerList = document.querySelector('ul.sellerList___kgAh_'); // Get the <ul> with class 'sellerList___kgAh_'

        if (sellerList) {
            const sellers = sellerList.querySelectorAll('li.rowWrapper___me3Ox'); // Select all listing rows

            sellers.forEach(row => {
                const anonymousDiv = row.querySelector('div.anonymous___P3S5S');
                if (anonymousDiv) {
                    // Skip if the seller is anonymous
                    return;
                }

                const profileLink = row.querySelector('a[href^="/profiles.php?XID="]');
                if (profileLink) {
                    const url = new URL(profileLink.href, 'https://www.torn.com'); // Handle relative URLs
                    const profileID = url.searchParams.get('XID'); // Extract the XID parameter from the href

                    const availableDiv = row.querySelector('div.available___xegv_');
                    if (availableDiv && availableDiv.textContent.includes('available')) {
                        // Extract the quantity from the text (e.g., "16 available")
                        const quantity = availableDiv.textContent.split(' ')[0]; // Get the first part (e.g., "16")
                        availableDiv.textContent = `https://www.torn.com/loader.php?sid=attack&user2ID=${profileID} (${quantity})`;
                    }
                }
            });
        }
    }

    // Function to convert div into a clickable link
    function convertDivToLink() {
        const divs = document.querySelectorAll('div.available___xegv_');

        divs.forEach((div) => {
            if (div.textContent.includes('https://') && !div.dataset.linkConverted) {
                const urlMatch = div.textContent.match(/https:\/\/www\.torn\.com\/loader\.php\?sid=attack&user2ID=\d+/);
                if (urlMatch) {
                    const url = urlMatch[0];
                    const text = div.textContent.replace(url, '').trim();

                    const link = document.createElement('a');
                    link.href = url;
                    link.textContent = `${text} Attack`;
                    link.style.color = 'inherit';
                    link.style.textDecoration = 'none';
                    link.target = '_blank';

                    div.innerHTML = '';
                    div.appendChild(link);
                    div.dataset.linkConverted = 'true';
                }
            }
        });
    }

    // Run the replacement function after the page has fully loaded
    window.addEventListener('load', () => {
        replaceAvailableWithAttackLinks();
        convertDivToLink();
    });

    // Observe for dynamic updates and replace again
    const observer = new MutationObserver(() => {
        replaceAvailableWithAttackLinks();
        convertDivToLink();
    });

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