Torn Trade Page Chat Button

Adds an 'Open Chat' button on trade pages that works with Chat 3.0 and Torn PDA

当前为 2025-07-15 提交的版本,查看 最新版本

// ==UserScript==
// @name         Torn Trade Page Chat Button
// @namespace    https://www.torn.com/
// @version      1.1
// @description  Adds an 'Open Chat' button on trade pages that works with Chat 3.0 and Torn PDA
// @author       KillerCleat [2842410]
// @match        https://www.torn.com/trade.php*
// @match        https://www.torn.com/pda.php*
// @grant        none
// @license      MIT
// @locale       en-US
// ==/UserScript==

/**
 * Notes & Requirements:
 * – Author: KillerCleat [2842410]
 * – Description: Injects an 'Open Chat' button into the Trade Log header on PC and PDA trade pages.
 * – Works with Torn Chat 3.0 (PC) and PDA chat interface.
 */

(function() {
    'use strict';

    /** Utility: Run callback when DOM is ready */
    function onReady(fn) {
        if (document.readyState !== 'loading') {
            fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }

    onReady(function() {
        // Determine if we're on PC trade or PDA trade page
        const isTradePC = location.pathname.includes('trade.php');
        const isTradePDA = location.pathname.includes('pda.php') && /[?&](trade|Trade)/.test(location.search);
        if (!isTradePC && !isTradePDA) return;

        // Find header with 'Trade Log'
        const headings = document.querySelectorAll('div[role="heading"][aria-level="5"]');
        let header = null;
        headings.forEach(h => {
            if (h.textContent.trim().startsWith('Trade Log')) {
                header = h;
            }
        });
        if (!header) return;

        // Extract the trade partner's user ID
        const userLink = document.querySelector('a[href*="user.php?ID="]');
        if (!userLink) return;
        const href = userLink.getAttribute('href');
        const params = new URLSearchParams(href.split('?')[1]);
        const userId = params.get('ID');
        if (!userId) return;

        // Create the 'Open Chat' button
        const btn = document.createElement('span');
        btn.className = 'tt-open-chat';
        btn.textContent = 'Open Chat';
        btn.style.cursor = 'pointer';
        btn.style.marginLeft = '10px';

        btn.addEventListener('click', function() {
            let chatUrl;
            if (isTradePDA) {
                chatUrl = '/pda.php?chat=' + userId;
            } else {
                chatUrl = '/chat.php?user2=' + userId;
            }
            window.open(chatUrl, '_blank', 'width=400,height=600');
        });

        // Insert button, avoiding duplicates
        let container = header.querySelector('div');
        if (!container) {
            container = document.createElement('div');
            header.appendChild(container);
        }
        if (!container.querySelector('.tt-open-chat')) {
            container.appendChild(btn);
        }
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址