Torn - Copy Last Incoming Message (Exact DOM Version)

Copies the most recent incoming chat message based on actual Torn DOM

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn - Copy Last Incoming Message (Exact DOM Version)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Copies the most recent incoming chat message based on actual Torn DOM
// @author       yoyoyossarian
// @license      MIT
// @match        https://www.torn.com/*
// @grant        GM_setClipboard
// ==/UserScript==


(function () {
    "use strict";

    // ---------------------------------------------------------
    // Extract last incoming message EXACTLY from your chat DOM
    // ---------------------------------------------------------
    function getLastIncomingMessage(chatBox) {
        // Any element containing an <a href="/profiles.php?XID=????">
        // is an incoming message container.
        const possible = chatBox.querySelectorAll("div, span");

        let last = null;

        possible.forEach(el => {
            // Incoming messages always have a profile link of the sender
            const profileLink = el.querySelector('a[href*="profiles.php"]');
            if (!profileLink) return;

            // Real message text is ALWAYS in a span.root___Xw4jI inside the container
            const msgText = el.querySelector(".root___Xw4jI");
            if (!msgText) return;

            last = msgText.innerText.trim();
        });

        return last;
    }

    // ---------------------------------------------------------
    // Inject Copy button
    // ---------------------------------------------------------
    function injectCopyButton(chatBox) {
        if (chatBox.querySelector(".copy-last-msg-button")) return;

        // Torn new + old chat input areas
        const inputContainer =
            chatBox.querySelector(".root___WUd1h") ||
            chatBox.querySelector(".chat-box-footer___YK914");

        if (!inputContainer) return;

        // Button container area
        let buttonContainer = chatBox.querySelector(".copy-button-container");
        if (!buttonContainer) {
            buttonContainer = document.createElement("div");
            buttonContainer.className = "copy-button-container";
            buttonContainer.style.display = "flex";
            buttonContainer.style.flexWrap = "wrap";
            inputContainer.insertAdjacentElement("beforebegin", buttonContainer);
        }

        // Create button
        const btn = document.createElement("button");
        btn.innerText = "Copy Msg";
        btn.className = "copy-last-msg-button";
        btn.style.background = "#6c5ce7";
        btn.style.color = "white";
        btn.style.margin = "4px";
        btn.style.padding = "4px 8px";
        btn.style.borderRadius = "5px";
        btn.style.cursor = "pointer";
        btn.style.border = "none";

        btn.addEventListener("click", () => {
            const msg = getLastIncomingMessage(chatBox);
            if (!msg) {
                console.log("[CopyLastMsg] No incoming message found");
                return;
            }
            GM_setClipboard(msg);
            console.log("[CopyLastMsg] COPIED:", msg);
        });

        buttonContainer.appendChild(btn);
    }

    // ---------------------------------------------------------
    // Auto-detect all chat windows (same logic as Enhanced Buttons)
    // ---------------------------------------------------------
    const observer = new MutationObserver(() => {
        const chatBoxes = document.querySelectorAll(
            '.root___FmdS_, .chat-box___mHm01, div[id^="private-"]'
        );
        chatBoxes.forEach(injectCopyButton);
    });

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