OverTimee

OTtaruih

目前為 2023-11-28 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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";
    }
})();