Return to residency

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

当前为 2022-05-29 提交的版本,查看 最新版本

// ==UserScript==
// @name         Return to residency
// @namespace    https://greasyfork.org/en/users/2402-n-tsvetkov
// @version      0.1
// @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
// ==/UserScript==

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;

function returnToResidence(countryId, regionId) {
    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() {
    var collectAll = document.querySelector('.collectAll');
    if (collectAll) {
        collectAll.click();
    }
}

function collectDailies() {
    if (O.dailiesToCollect > 0) {
        document.querySelector("#dailyMissionsPopupTrigger").click();
        setTimeout((dailiesToClaim) => {
            var i = 1;
            document.querySelectorAll(".claimButton").forEach(function(el) {
                setTimeout(() => {
                    el.click();
                }, (i+1) * 1e3);
                i++;
            });
        }, 3e3);
    }
}

(function() {
    'use strict';
    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);
    }
})();