Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
4.2 kB
2
Indexable
Never
// ==UserScript==
// @name         BoosterToTheMAX
// @description  Activate all 3 min PP boosters
// @namespace    https://tampermonkey.net/
// @version      1.00
// @author       madcorp
// @supportURL   https://www.erepublik.com/en/citizen/profile/2493570
// @match        https://www.erepublik.com/en/economy/inventory
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    const waitForElm = (selector) => {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

            const observer = new MutationObserver(mutations => {
                if (document.querySelector(selector)) {
                    resolve(document.querySelector(selector));
                    observer.disconnect();
                }
            });

            observer.observe(document.body, {
                childList: true,
                subtree: true
            });
        });
    };

    waitForElm('#inventory_100_prestige_points_1_180').then((elm) => {
        const pp3m_el = elm;

        if(pp3m_el !== null){
            const pp3m_qty_el = pp3m_el.querySelector('#stock_100_1');
            const pp3m_qty = (pp3m_qty_el.innerHTML !== null) ? ~~pp3m_qty_el.innerHTML : 0;
            //console.log(pp3m_qty);

            if(pp3m_qty > 2){

                const myToken = unsafeWindow.csrfToken;
                const activateAll = (limit, csrftoken) => {
                    if(limit !== null && csrftoken !== null){
                        return fetch("https://www.erepublik.com/en/economy/activateBooster", {
                            "headers": {
                                "accept": "application/json, text/plain, */*",
                                "accept-language": "en-GB,en;q=0.9,en-US;q=0.8,bg;q=0.7",
                                "content-type": "application/x-www-form-urlencoded",
                                "sec-fetch-dest": "empty",
                                "sec-fetch-mode": "cors",
                                "sec-fetch-site": "same-origin",
                                "x-requested-with": "XMLHttpRequest"
                            },
                            "referrer": "https://www.erepublik.com/en/economy/inventory",
                            "referrerPolicy": "same-origin",
                            "body": "type=prestige_points&quality=1&duration=180&fromInventory=true&_token=" + csrftoken,
                            "method": "POST",
                            "mode": "cors",
                            "credentials": "include"
                        }).then(res => {
                            if (res.status === 200 && --limit) {
                                return activateAll(limit, csrftoken);
                            }
                            return res.json();
                        });
                    }
                };

                //console.log(myToken);

                var sidebarCnt = document.querySelector('div.sidebar_container');
                var zNode = document.createElement ('div');
                zNode.innerHTML = '<button id="pp_max_btn" type="button" class="std_global_btn smallSize redColor">Lazy PP activate</button>';
                zNode.setAttribute ('id', 'myContainer');
                sidebarCnt.appendChild (zNode);

                document.getElementById ("pp_max_btn").addEventListener ("click", ButtonClickAction, false);

                function ButtonClickAction (zEvent) {
                    var pp_limit = prompt("Колко 3 минутни PP буустера искаш да активираш?\n\nРазполагаш с "+pp3m_qty+", които ще стигнат за ~"+Math.floor(pp3m_qty * 3 / 60)+" часа");
                    //alert('Activated PP boosters: ' + pp_limit);
                    activateAll(pp_limit, myToken);

                }

                GM_addStyle (`
                    #myContainer { width: 100%; display: grid; }
                    #pp_max_btn { border: none; margin: 0 auto; }
                `);

            }
        }
    });
})();