OverTimee

OTtaruih

当前为 2023-11-28 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         OverTimee
// @version      0.1
// @description  OTtaruih
// @author       You
// @match        https://www.erepublik.com/en/economy/myCompanies
// @icon         https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
// @grant        none
// @namespace https://greasyfork.org/users/1144622
// ==/UserScript==
(function() {
    'use strict';

    const _token = csrfToken;
    const interval = 3615000;

    async function mainFunction() {
        const captcha = checkSessionValidationExists();
        const next = checkLocalStorage();
        const now = getCurrentUnixTimestamp();

        if (!captcha) {
            if (next < now) {
                const job = await fetchData(`https://www.erepublik.com/en/main/job-data`);
                if (job) {
                    const worked = job.alreadyWorked;
                    const nextOT = job.overTime.nextOverTime;

                    const payload = payloadOT();
                    await delay(5000);
                    await OT(payload);


                    if (next < nextOT) {
                        editLocalStorage(nextOT);
                    }
                    console.log(worked);
                    console.log(nextOT);

                }
            }

        }
    }

    //end of line
    // Call the main function when the page loads
    window.addEventListener('load', mainFunction);
    setInterval(mainFunction, interval);


    function checkLocalStorage() {

        const checkData = localStorage.getItem("OT");
        if (checkData) {

            return parseInt(checkData, 10);
        } else {
            const zero = 0;
            localStorage.setItem("OT", zero.toString());
            return expirationTime;
        }
    }

    function editLocalStorage(newValue) {
        const checkData = checkLocalStorage();
        const currentTime = getCurrentUnixTimestamp();

        if (currentTime > checkData || (currentTime < newValue && newValue < checkData)) {
            localStorage.setItem("OT", newValue.toString());
        }
    }

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

    function getCurrentUnixTimestamp() {
        const currentTime = new Date();
        const unixTimestamp = Math.floor(currentTime.getTime() / 1000); // Convert milliseconds to seconds
        return unixTimestamp;
    }

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

    async function fetchData(url) {
        try {
            const response = await fetch(url);
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            const data = await response.json();
            return data;
        } catch (error) {
            throw new Error(`Failed to fetch data from ${url}: ${error.message}`);
        }
    }

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

        return {
            action_type,
            _token
        };
    }

    // Function to send the payload using POST request
    async function OT(payloadOT) {
        const url = "https://www.erepublik.com/en/economy/workOvertime";

        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 redirectToErepublik() {
        window.location.href = "https://www.erepublik.com/en";
    }
})();