Greasy Fork is available in English.

Panda Crazy Helper Emulation

Meant to be @included in other scripts to communicate with Panda Crazy.

Dette scriptet burde ikke installeres direkte. Det er et bibliotek for andre script å inkludere med det nye metadirektivet // @require https://greasyfork.org/scripts/36173-panda-crazy-helper-emulation/code/Panda%20Crazy%20Helper%20Emulation.js?version=244288

var PandaCrazy = (function createPandaCrazy() {

    let _lastSentPingTime;
    let _lastReceivedPongTime;

    let _onlineSinceLastPing;

    let _pcListener;

    const MAX_WAIT_FOR_PANDA_CRAZY_RESPONSE_MS = 1000;

    function ping() {
        _lastSentPingTime = Date.now();
        localStorage.setItem("JR_message_ping_pandacrazy", `{"theTarget": "${Math.random()}"}`);
    }

    function hasIndicatedOnlineSinceLastPing() {
        if(_lastSentPingTime !== undefined && _lastReceivedPongTime !== undefined) {
            return _lastReceivedPongTime >= _lastSentPingTime;
        }
        else {
            return undefined;
        }
    }

    function online() {

        function respondToStorage(resolve, reject, e) {

            if(e.key.includes("JR_message_pong") && Boolean(e.newValue)) {
                _lastReceivedPongTime = Date.now();
                let pongData = JSON.parse(e.newValue);
                let lag = Number(pongData.time) - Number(_lastReceivedPongTime);
                if(hasIndicatedOnlineSinceLastPing()) {
                    resolve("online");
                }
            }

        }

        let isOnlinePromise = new Promise((resolve, reject) => {

            setTimeout(() => {reject("timeout");}, MAX_WAIT_FOR_PANDA_CRAZY_RESPONSE_MS);
            if(_pcListener) {window.removeEventListener("storage", _pcListener);}
            _pcListener = respondToStorage.bind(window, resolve, reject);
            window.addEventListener("storage", _pcListener);

        });

        ping();

        return isOnlinePromise;
    }

    function addJob(gid, once, metadata) {
        let commandString = once ? "addOnceJob" : "addJob";

        localStorage.setItem("JR_message_pandacrazy", JSON.stringify({
            time: Date.now(),
            command: commandString,
            data: {
                groupId: gid,
                title: (metadata ? metadata.hitTitle || metadata.title : undefined),
                requesterName: (metadata ? metadata.requesterName : undefined),
                requesterId: (metadata ? metadata.requesterID || metadata.requesterId || metadata.rid : undefined),
                pay: (metadata ? metadata.hitValue || metadata.pay : undefined),
                duration: (metadata ? metadata.duration : undefined),
                hitsAvailable: (metadata ? metadata.hitsAvailable : undefined)
            }
        }));
    }

    function startJob(gid) {
        localStorage.setItem("JR_message_pandacrazy", JSON.stringify({
            time: Date.now(),
            command: "startcollect",
            data: {
                groupId: gid
            }
        }));
    }

    return {
        addJob,
        startJob,
        ping,
        online
    };
})();