Torn Profile Link Formatter

Adds a 'Copy' button next to a user's name for easy sharing.

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

// ==UserScript==
// @name         Torn Profile Link Formatter
// @namespace    GNSC4 [268863]
// @version      1.0.2
// @description  Adds a 'Copy' button next to a user's name for easy sharing.
// @author       GNSC4 [268863]
// @match        https://www.torn.com/profiles.php?XID=*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    /**
     * This function runs once the page has fully loaded to ensure all elements are available.
     */
    window.addEventListener('load', () => {
        // --- 1. Extract User Information ---
        const urlParams = new URLSearchParams(window.location.search);
        const userId = urlParams.get('XID');
        if (!userId) {
            console.error("Torn Profile Link Formatter: Could not find User ID in the URL.");
            return;
        }

        // Target the main name header element on the profile page.
        const nameElement = document.querySelector('#skip-to-content');
        if (!nameElement) {
            console.error("Torn Profile Link Formatter: Could not find the name element '#skip-to-content' on the page.");
            return;
        }
        // The user's name is displayed as "Name [ID]". We split by " [" to get just the name.
        const userName = nameElement.textContent.split(' [')[0].trim();


        // --- 2. Construct the Formatted String for Clipboard ---
        const profileUrl = `https://www.torn.com/profiles.php?XID=${userId}`;
        const attackUrl = `https://www.torn.com/loader2.php?sid=getInAttack&user2ID=${userId}`;
        const formattedString = `${userName} - <a href="${profileUrl}">Profile</a> - <a href="${attackUrl}">Attack</a>`;


        // --- 3. Create and Style the 'Copy' Button ---
        const copyButton = document.createElement('a');
        copyButton.href = "#";
        copyButton.innerHTML = '<span>Copy</span>';

        // Style the link to look like a dark-mode button.
        copyButton.style.backgroundColor = '#333333';
        copyButton.style.color = '#DDDDDD';
        copyButton.style.border = '1px solid #555555';
        copyButton.style.borderRadius = '5px';
        copyButton.style.padding = '3px 8px';
        copyButton.style.marginLeft = '10px';
        copyButton.style.textDecoration = 'none';
        copyButton.style.display = 'inline-block';
        copyButton.style.verticalAlign = 'middle';
        copyButton.style.fontSize = '12px';
        copyButton.style.lineHeight = '1.5';
        copyButton.style.fontWeight = 'bold';


        // --- 4. Add Click Functionality to the Button ---
        copyButton.addEventListener('click', (e) => {
            e.preventDefault();
            copyToClipboard(formattedString);

            // Provide visual feedback to the user that the copy was successful.
            const originalText = copyButton.innerHTML;
            const originalBgColor = copyButton.style.backgroundColor;
            copyButton.innerHTML = '<span>Copied!</span>';
            copyButton.style.backgroundColor = '#2a633a'; // Darker success green

            // Revert the button back to its original state after 2 seconds.
            setTimeout(() => {
                copyButton.innerHTML = originalText;
                copyButton.style.backgroundColor = originalBgColor;
            }, 2000);
        });


        // --- 5. Add the New Button to the Page ---
        // We insert the button immediately after the name header element.
        nameElement.insertAdjacentElement('afterend', copyButton);
    });

    /**
     * Copies the given text to the user's clipboard.
     * This function creates a temporary textarea element, adds the text to it,
     * selects the text, and uses the document's 'copy' command.
     * @param {string} text - The text to be copied to the clipboard.
     */
    function copyToClipboard(text) {
        const tempTextarea = document.createElement('textarea');
        tempTextarea.style.position = 'absolute';
        tempTextarea.style.left = '-9999px'; // Move it off-screen
        tempTextarea.value = text;
        document.body.appendChild(tempTextarea);
        tempTextarea.select();
        document.execCommand('copy');
        document.body.removeChild(tempTextarea);
    }
})();