Return to residency

Return to residency after some time. Claim daily rewards, claim weekly rewards.

当前为 2022-06-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Return to residency
// @namespace    https://greasyfork.org/en/users/2402-n-tsvetkov
// @version      0.5
// @description  Return to residency after some time. Claim daily rewards, claim weekly rewards.
// @author       Nikolai Tsvetkov
// @match        https://www.erepublik.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=erepublik.com
// @grant        none
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';
    if (window.top != window.self) {
        return;
    }
    var returnIn = 5e3 * 60;
    var pageRefresh = 5e3 * 60;
    var homePage = window.Environment.isOnHomepage || false;
    var erepublik = window.erepublik || {};
    var lang = erepublik.settings.culture;
    var O = erepublik.citizen || {};
    var H = O.residence;
    var t = window.csrfToken;
    var e = localStorage.notInResidence || 0;
    var dailiesToClaim;

    if (homePage) {
        console.log('home');
        getRewards();
        collectDailies();
        if (H.hasResidence && O.regionLocationId != H.regionId) {
            var now = Date.now();
            if (e == 0) {
                localStorage.notInResidence = now;
            } else {
                if ((now - e) >= returnIn) {
                    returnToResidence(H.countryId, H.regionId)
                }
            }
        } else {
            localStorage.notInResidence = 0;
        }

        setTimeout(() => location.reload(), pageRefresh)
    }

    function returnToResidence(countryId, regionId) {
        localStorage.notInResidence = 0;
        var body = "_token=" + t + "&battleId=0&toCountryId=" + countryId + "&inRegionId=" + regionId;
        fetch("/" + lang + "/main/travel/", {
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            },
            credentials: "same-origin",
            body: body,
        })
            .then(() => {
            location.reload();
        });
    }

    function getRewards() {
        setTimeout(() => {
            var collectAll = document.querySelector('.collectAll');
            console.log(collectAll);
            if (collectAll) {
                collectAll.click();
            }
        }, 5e3);
    }

    function collectDailies() {
        if (O.dailiesToCollect > 0) {
            document.querySelector("#dailyMissionsPopupTrigger").click();
            setTimeout(function() {
                let timer = setInterval((dailiesToClaim) => {
                    let claimButton = document.querySelector(".claimButton");
                    if (claimButton) {
                        setTimeout(() => {
                            claimButton.click();
                        }, 1e3);
                    } else {
                        clearInterval(timer);
                        let closeButton = document.querySelector(".closeButton");
                        closeButton && setTimeout(() => closeButton.click(), 5e3);
                    }
                }, 1e3);
            }, 1e3);
        }
    }
})();