Untitled - MARKUP 5.79 KB
                    
             // ==UserScript==
// @name         Gargonem - dodatki Priweejta
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  Zestaw dodatków do nowego i starego interfejsu w grze Margonem
// @author       Priw8
// @match        https://*.margonem.pl/
// @exclude      https://www.margonem.pl/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=margonem.pl
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        unsafeWindow
// ==/UserScript==

!function() {
    const version = 0.6;
    const window = unsafeWindow;

    const gargonemMainSrc = "https://priw8.com/margo/gargonem/dist/main.js";

    /** @type {string[]} */
    const scriptsToLoad = [gargonemMainSrc];
    const gmPrivilegedScripts = [gargonemMainSrc];

    const InterfaceKind = {
        NEW: 0,
        OLD: 1
    };
    
    /** @returns {number} */
    function getInterfaceKind() {
        return typeof window.Engine == "undefined" ? InterfaceKind.OLD : InterfaceKind.NEW;
    }

    const interfaceKind = getInterfaceKind();

    const cachedScriptMaxLifetime = 12 * 60 * 60; // 12 hours
    function getScriptVersion() {
        const lastUpdateCheck = Number(GM_getValue(`_loaderLastUpdateCheck`, "0"));
        const ts = Math.floor(new Date().getTime() / 1000);
        if (lastUpdateCheck + cachedScriptMaxLifetime < ts) {
            GM_setValue(`_loaderLastUpdateCheck`, ts.toString());
            return ts;
        } else {
            return lastUpdateCheck;
        }
    }

    const scriptVersion = getScriptVersion();

    /** @param src {string} */
    function loadScript(src) {
        const $script = window.document.createElement("script");
        if (gmPrivilegedScripts.includes(src)) {
            $script.GM_setValue = GM_setValue;
            $script.GM_getValue = GM_getValue;
            $script.GM_deleteValue = GM_deleteValue;
            $script.GM_listValues = GM_listValues;
            $script.loaderVersion = version;
            $script.gargonem = true;
            $script.addScript = addScript;
        }
        $script.src = `${src}?v=${scriptVersion}`;
        return new Promise((resolve, reject) => {
            $script.addEventListener("load", resolve);
            $script.addEventListener("error", reject);
            window.document.head.appendChild($script);
        });
    }

    /** @param src {string} */
    function addScript(src) {
        scriptsToLoad.push(src);
    }

    function firefoxFix_waitUntilFullyLoaded() {
        // On firefox, the userscript may somehow be executed while Engine exists but not Engine.communication
        if (interfaceKind == InterfaceKind.NEW && typeof window.Engine.communication == "undefined") {
            return new Promise(resolve => {
                Object.defineProperty(window.Engine, "communication", {
                    get() {
                        return null;
                    },
                    set(v) {
                        console.log(`[GargonemFirefoxFix]: Engine.communication loaded`);
                        delete window.Engine.communication;
                        window.Engine.communication = v;
                        resolve();
                        return true;
                    },
                    configurable: true
                })
            });
        }
    }

    let overwrittenFunction = null;
    let overwrittenFunctionThis = null;
    let overwrittenFunctionArgs = null;
    let wasOverwrittenFunctionCalled = false;
    async function beginLoading() {
        await firefoxFix_waitUntilFullyLoaded();

        // Block game loading until all scripts are loaded
        if (interfaceKind == InterfaceKind.OLD) {
            overwrittenFunction = window.startGame;
            window.startGame = function() {
                wasOverwrittenFunctionCalled = true;
                overwrittenFunctionThis = this;
                overwrittenFunctionArgs = arguments;
            }
        } else {
            overwrittenFunction = window.Engine.communication.startCallInitAfterRecivedAddons;
            window.Engine.communication.startCallInitAfterRecivedAddons = function() {
                wasOverwrittenFunctionCalled = true;
                overwrittenFunctionThis = this;
                overwrittenFunctionArgs = arguments;
            }
        }

        loadBatch();
    }

    function endLoading() {
        if (interfaceKind == InterfaceKind.OLD) {
            window.startGame = overwrittenFunction;
        } else {
            window.Engine.communication.startCallInitAfterRecivedAddons = overwrittenFunction;
        }

        if (wasOverwrittenFunctionCalled) {
            overwrittenFunction.apply(overwrittenFunctionThis, overwrittenFunctionArgs);
        }
    }

    async function loadBatch() {
        if (scriptsToLoad.length) {
            const toLoad = scriptsToLoad.splice(0, scriptsToLoad.length);
            const results = await Promise.allSettled(toLoad.map(loadScript));
            for (let i=0; i<results.length; ++i) {
                const res = results[i];
                if (res.status == "rejected") {
                    const url = toLoad[i];
                    console.error(`script load error for ${url}; an error message detailing the failure may be displayed above`);
                }
            }
            loadBatch();
        } else {
            endLoading();
        }
    }

    beginLoading();
}();
            
            

Paste Hosted With By Wklejamy.pl