FightButtonLibrary

Library for fight button usage start

Versión del día 16/03/2024. Echa un vistazo a la versión más reciente.

Este script no debería instalarse directamente. Es una biblioteca que utilizan otros scripts mediante la meta-directiva de inclusión // @require https://update.greasyfork.org/scripts/489910/1343963/FightButtonLibrary.js

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         FightButtonLibrary
// @namespace    http://tampermonkey.net/
// @version      0.19
// @description  Library for fight button usage start
// @author       h2o
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_xmlhttpRequest
// @esversion    8
// ==/UserScript==

(function() {
    'use strict';
    let step={};
    let sfight="";


     
    // Function to prompt user for API key
    function promptForApiKey() {
        var apiKey = prompt("Please enter your API key:");
        if (apiKey !== null && apiKey !== "") {
            GM_setValue("apiKey", apiKey); // Save API key locally
            return apiKey;
        } else {
            alert("API key cannot be empty!");
            return null;
        }
    }

    // Function to start the process
    function startProcess() {
        // Check if API key is already saved
        var savedApiKey = GM_getValue("apiKey");

        if (!savedApiKey) {
            savedApiKey = promptForApiKey();
        }

        if (savedApiKey) {
            // Extract user ID from URL
            var url = window.location.href;
            var userIdMatch = url.match(/user2ID=(\d+)/);
            var userId = userIdMatch ? userIdMatch[1] : null;

            if (userId) {
                // Define the URL
                var apiUrl = "https://api-torn-members.glitch.me/update/";

                // Define the query parameters
                var query = {
                    "item": "startFight",
                    "key": savedApiKey,
                    "opponent": userId,
                };

                // Construct the full URL with query parameters
                var fullUrl = apiUrl + "?item=" + query.item + "&key=" + query.key +"&opponent=" + query.opponent;

                // Send the request
                GM_xmlhttpRequest({
                    method: "GET",
                    url: fullUrl,
                    onload: function(response) {
                        handleResponse(response.responseText); // Handle the response
                        

                    },
                    onerror: function(error) {
                        console.error("Request failed:", error);
                    }
                });
            } else {
                console.error("User ID not found in URL.");
            }
        }
    }

    // Function to handle the response
    function handleResponse(responseText) {
        step=JSON.parse(responseText);
        step=string(step.warlink);
    }
    //create and style the button
    const coverDiv = createDiv('tornCoverDiv', 'fixed', '25%', '51%', '280px', '140px');
    const infoDiv = createDiv('tornInfoDiv', 'fixed', '25%', '86%', '170px', '420px');
    // Create and style button element
    const newButton = createButton('tornNewButton', 'fixed', coverDiv.style.top, coverDiv.style.left);
    //create the divs
    function createDiv(id, position, top, left, width, height) {
        const div = document.createElement('div');
        div.id = id;
        div.style.position = position;
        div.style.top = top;
        div.style.left = left;
        div.style.backgroundColor = 'black';
        div.style.color = 'white';
        div.style.padding = '10px';
        div.style.zIndex = '9998';
        div.style.width = width;
        div.style.height = height;
        div.style.textAlign = 'center';
        div.style.display = 'none';
        document.body.insertBefore(div, document.body.firstChild);
        return div;
    }
     // create the button
    function createButton(id, position, top, left) {
        const button = document.createElement('button');
        button.id = id;
        button.classList = 'torn-btn';
        button.style.position = position;
        button.style.top = top;
        button.style.left = left;
        button.style.transform = 'translate(75px,120px)';
        button.style.width = '170px';
        button.style.color = 'white';
        button.style.zIndex = '9999';
        button.style.display = 'none';
        button.textContent = 'WAIT for Counter';
        button.disabled = true;
        document.body.insertBefore(button, document.body.firstChild);
        return button;
    }
    
    newButton.addEventListener("click", function() {
        if (!newButton.disabled) {
            tornCoverDiv.innerHTML = `<strong style="font-size: 30px;">Fight Started</strong>`;
            sfight=step.warlink
            alert(sfight);
            const url = window.location.href;
            const x = url.indexOf("ID=");
            const ID = url.substring(x + 3);

            fetch("/loader.php?sid=attackData&mode=json", {
                method: "POST",
                headers: {

                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "x-requested-with": "XMLHttpRequest",
                },
                body: sfight,
            })
        .then(function(response) {
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
                
            }
            // You can handle the response here if needed
            alert("response problem");
        })
        .catch(function(error) {
            console.error("Error:", error);
            alert("error happened");
        });
    }
});
            


    // Expose the startProcess function to be called externally
    window.updateItemLibrary = {
        startProcess: startProcess
    };

})();