Torn Profile to TornPal Button

Adds a button to Torn profiles to view the user on TornPal

// ==UserScript==
// @name         Torn Profile to TornPal Button
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Adds a button to Torn profiles to view the user on TornPal
// @author       You
// @match        https://www.torn.com/profiles.php*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to add the TornPal button
    function addTornPalButton() {
        // Wait for the profile buttons to load
        const buttonsContainer = document.querySelector('.buttons-list');
        if (!buttonsContainer) {
            setTimeout(addTornPalButton, 500);
            return;
        }

        // Check if our button already exists to prevent duplicates
        if (document.querySelector('.profile-button-tornpal')) {
            return; // Button already exists, don't add another one
        }

        // Extract user ID from the profile
        const userInfoElement = document.querySelector("#profileroot > div > div > div > div:nth-child(6) > div.basic-information.profile-left-wrapper.left > div > div.cont.bottom-round > div > ul > li:nth-child(1) > div.user-info-value");
        if (!userInfoElement) {
            console.log("User ID element not found");
            return;
        }

        // Extract the user ID from the text (format: "Username [ID]")
        const userIdMatch = userInfoElement.textContent.match(/\[(\d+)\]/);
        if (!userIdMatch) {
            console.log("Could not extract user ID from text");
            return;
        }

        const userId = userIdMatch[1];

        // Create the new button
        const tornPalButton = document.createElement('a');
        tornPalButton.href = `https://tornpal.com/profile/${userId}`;
        tornPalButton.className = 'profile-button profile-button-tornpal active';
        tornPalButton.setAttribute('aria-label', 'View on TornPal');
        tornPalButton.setAttribute('data-is-tooltip-opened', 'false');
        tornPalButton.id = 'tornpal-profile-button'; // Add an ID for easier identification

        // Style the button to match other buttons but with text instead of SVG
        tornPalButton.style.display = 'flex';
        tornPalButton.style.alignItems = 'center';
        tornPalButton.style.justifyContent = 'center';
        tornPalButton.style.width = '46px';
        tornPalButton.style.height = '46px';
        tornPalButton.style.fontSize = '14px';
        tornPalButton.style.fontWeight = 'bold';
        tornPalButton.style.color = "rgba(255, 255, 255, 0.4)";
        tornPalButton.style.textDecoration = 'none';
        tornPalButton.style.textAlign = "center";
        tornPalButton.style.borderRadius = '4px';

        // Add text content
        tornPalButton.innerHTML = "Torn <br> Pal";

        // Add the button to the container
        buttonsContainer.appendChild(tornPalButton);
    }

    // Use a single event listener for page load
    window.addEventListener('load', function() {
        // Add a small delay to ensure the profile has loaded
        setTimeout(addTornPalButton, 1000);
    });
})();