Greasy Fork is available in English.

Jalan-jalan

try to travel over the world!

// ==UserScript==
// @name         Jalan-jalan
// @namespace    Amazing Journey
// @version      0.3.2
// @description  try to travel over the world!
// @author       You
// @match        https://www.erepublik.com/en
// @icon         https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    const targetKM = 750000;


    const _token = csrfToken;
    const current = erepublik.citizen.regionLocationId;
    const residence = erepublik.citizen.residence.regionId;
    let target;
    let country;
    if (current == 714) {
        //ke tujuan
        target = 173;
        country = 15;
    } else {
        //ke sarawak
        target = 714;
        country = 84;
    }

    async function mainFunction() {
        await delay(1000);
        const total = 10;

        if (total < targetKM) {
            const tujuan = payload(target, country);
            await delay(1000);
            await send(tujuan);
            await delay(10000);
            redirectToErepublik()
        } else {
            if (current != residence) {
                //balik ke residence
                target = residence;
                country = erepublik.citizen.residence.countryId;
                const kembali = payload(target, country);
                await delay(1000);
                await send(kembali);
                await delay(10000);
                redirectToErepublik()
            }
        }


    }

    mainFunction();

    function payload(target, country) {
        const check = "moveAction";
        const travelMethod = "preferTicket";
        const inRegionId = target;
        const toCountryId = country;


        return {
            check,
            _token,
            travelMethod,
            inRegionId,
            toCountryId
        };
    }

    // Function to send the payload using POST request
    async function send(payloadData) {
        const url = "https://www.erepublik.com/en/main/travel";

        try {
            const response = await fetch(url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                body: Object.keys(payloadData)
                    .map(
                        key =>
                        `${encodeURIComponent(key)}=${encodeURIComponent(payloadData[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 redirectToErepublik() {
        window.location.href = "https://www.erepublik.com/en";
    }

    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}`);
        }
    }
})();