Romusha

try to build the world!

// ==UserScript==
// @name         Romusha
// @namespace    erepfarm
// @version      2024-05-14
// @description  try to build the world!
// @author       Prince of Spade
// @match        https://www.erepublik.com/es/economy/myCompanies
// @icon         https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
// @grant        none
// ==/UserScript==
(function() {
    'use strict';

    window.addEventListener('load', mainFunction);

    async function mainFunction() {
        const capthca = checkSessionValidationExists();
        if (!capthca) {
            const _token = csrfToken;
            let energy = erepublik.citizen.energy;
            const loops = Math.floor(energy / 100);
            createDivElement('energyloop', 'sidebar', 'LOOPS: ');
            displayValueInHtml('energyloop', loops);
            createDivElement('energyloop', 'sidebar', '=================');
            createDivElement('list', 'sidebar', '');

            for (let i = 0; i < loops; i++) {
                const payload = payloadOT(_token);
                const ot = "https://www.erepublik.com/en/economy/workOvertime";
                await PostRequest(payload, ot);
                await delay(3000);
                displayValueInHtml('list', i);
            }

            createDivElement('energyloop', 'sidebar', '== DONE ==');
        }



    }

    function checkSessionValidationExists() {
        if (typeof SERVER_DATA !== 'undefined' && SERVER_DATA.sessionValidation !== undefined) {
            return true;
        } else {
            return false;
        }
    }

    // Function to construct the payload from variables
    function payloadOT(_token) {
        const action_type = "workOvertime";

        return {
            action_type,
            _token
        };
    }

    // Function to send the payload using POST request
    async function PostRequest(payload, url) {

        try {
            const response = await fetch(url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                body: Object.keys(payload)
                    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(payload[key])}`)
                    .join('&')
            });

            const responseData = await response.json();
            return responseData;
        } catch (error) {
            console.error("Error:", error);
            return null;
        }
    }

    function delay(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    function createDivElement(divId, parentId, textContent) {
        const parentElement = document.querySelector(`.${parentId}`);
        if (parentElement) {
            const newDiv = document.createElement('div');
            newDiv.id = divId;
            newDiv.textContent = textContent;
            parentElement.appendChild(newDiv);
        } else {
            console.error(`Parent element with class '${parentId}' not found.`);
        }
    }

    // Function to display any value in HTML
    function displayValueInHtml(elementId, value) {
        const element = document.getElementById(elementId);
        if (element) {
            element.textContent = `${element.textContent} ${value}`;
        } else {
            console.error(`Element with ID '${elementId}' not found.`);
        }
    }
})();