Nomad's Quick Revive

Button to quickly request a revive from the Nomad Faction

目前為 2024-12-01 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Nomad's Quick Revive
// @namespace    http://tampermonkey.net/
// @version      1.0.2
// @description  Button to quickly request a revive from the Nomad Faction
// @author       LilyWaterbug
// @match        https://www.torn.com/*
// @grant        GM_xmlhttpRequest
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const observer = new MutationObserver(() => {
        const targetContainer = document.querySelector('.toggle-content___BJ9Q9 .content___GVtZ_');

        if (targetContainer) {
            if (document.getElementById('quick-revive-button')) return;

            const button = document.createElement('button');
            button.id = 'quick-revive-button';
            button.innerText = 'Nomad Nurse';

            // Estilos del botón
            button.style.margin = '10px auto';
            button.style.padding = '2.5px 15px';
            button.style.display = 'block';
            button.style.fontSize = '14px';
            button.style.backgroundColor = '#D32F2F'; // Color principal
            button.style.color = '#FFFFFF'; // Texto blanco
            button.style.border = 'none'; // Sin bordes directos
            button.style.borderRadius = '5px';
            button.style.cursor = 'pointer';
            button.style.fontWeight = 'bold';
            button.style.webkitTextStroke = '0.2px #FFFFFF';
            button.style.textAlign = 'center';

            // Outline doble
            button.style.boxShadow = `
                0 0 0 2px #f2f2f2,
                0 0 0 4px #D32F2F
            `;

            button.addEventListener('click', () => {
                // Obtener el nombre del usuario
                const nameElement = document.querySelector('.menu-info-row___YG31c .menu-value___gLaLR');
                const userLink = nameElement?.getAttribute('href');
                const userName = nameElement?.textContent;

                // Extraer el ID del href
                const userIdMatch = userLink?.match(/XID=(\d+)/);
                const userId = userIdMatch ? userIdMatch[1] : null;

                if (!userName || !userId) {
                    alert('Error: Unable to extract user information.');
                    return;
                }

                // Formatear el nombre del usuario
                const formattedUser = `${userName} [${userId}]`;

                // Llamada a la API de Torn
                const apiUrl = `https://api.torn.com/user/${userId}?selections=&key=oklXUrfSR6e3U9mB`;

                GM_xmlhttpRequest({
                    method: "GET",
                    url: apiUrl,
                    onload: function (response) {
                        if (response.status === 200) {
                            const apiData = JSON.parse(response.responseText);

                            // Extraer datos de API
                            const status = apiData.status?.description || "N/A";
                            const statusState = apiData.status?.state || "N/A";
                            const revivable = apiData.revivable || 0;
                            const faction = apiData.faction?.faction_name || "No Faction";
                            const factionId = apiData.faction?.faction_id || null;
                            const factionUrl = factionId
                                ? `https://www.torn.com/factions.php?step=profile&ID=${factionId}#/`
                                : "No Faction URL";
                            const details = apiData.status?.details || "No Details";

                            // Verificar si el estado no es "Hospital"
                            if (statusState !== "Hospital") {
                                alert("You're not hospitalized right now.");
                                return;
                            }

                            // Verificar si el usuario no tiene revives encendidos
                            if (revivable === 0) {
                                alert("Turn on your revives!");
                                return;
                            }

                            // Preparar datos para el webhook
                            const data = {
                                function: "revive",
                                usuario: formattedUser,
                                status: status,
                                faction: faction,
                                faction_url: factionUrl,
                                details: details,
                            };

                            // Enviar datos al webhook
                            GM_xmlhttpRequest({
                                method: "POST",
                                url: "http://45.139.50.97:6224/webhook",
                                headers: {
                                    "Content-Type": "application/json",
                                },
                                data: JSON.stringify(data),
                                onload: function (webhookResponse) {
                                    if (webhookResponse.status === 200) {
                                        alert('Revive request sent successfully!');
                                    } else {
                                        alert('Failed to send revive request. Status: ' + webhookResponse.status);
                                    }
                                },
                                onerror: function () {
                                    alert('An error occurred while sending the revive request.');
                                }
                            });
                        } else {
                            alert('Failed to fetch Torn API data. Status: ' + response.status);
                        }
                    },
                    onerror: function () {
                        alert('An error occurred while fetching Torn API data.');
                    }
                });
            });

            targetContainer.appendChild(button);
        }
    });

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