Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Created January 3, 2024 02:33
Show Gist options
  • Select an option

  • Save erikdubbelboer/b9ddfd64196a4d41cad42db86d52348d to your computer and use it in GitHub Desktop.

Select an option

Save erikdubbelboer/b9ddfd64196a4d41cad42db86d52348d to your computer and use it in GitHub Desktop.

Revisions

  1. erikdubbelboer created this gist Jan 3, 2024.
    85 changes: 85 additions & 0 deletions poki.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    export function loadPokiSDK() {
    const getParameterByName = (name) => {
    const match = RegExp(`[?&]${name}=([^&]*)`).exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    };

    const queue = [];
    const isLocalhost = window.location.hostname === 'localhost';

    window.PokiSDK = new Proxy(
    {},
    {
    get(target, prop) {
    return (...args) => {
    return new Promise((resolve, reject) => {
    if (prop === 'rewardedBreak') {
    resolve(isLocalhost);
    } else if (prop === 'commercialBreak') {
    resolve();
    } else if (prop === 'displayAd') {
    const onCanDestroy = args[2];
    const onDisplayRendered = args[3];
    if (onDisplayRendered) {
    onDisplayRendered(true);
    }
    if (onCanDestroy) {
    onCanDestroy();
    }
    } else if (prop === 'getURLParam') {
    const key = args[0];
    return getParameterByName(`gd${key}`) || getParameterByName(key) || '';
    } else if (prop === 'getLanguage') {
    return navigator.language.toLowerCase().split('-')[0];
    } else if (prop === 'getIsoLanguage') {
    return getParameterByName('iso_lang');
    } else if (prop === 'isAdBlocked') {
    return;
    } else {
    queue.push({
    f: prop,
    a: args,
    r: resolve,
    j: reject,
    });
    }
    });
    };
    },
    }
    );

    PokiSDK.init({
    //debug: true,
    });

    function run() {
    queue.forEach((item) => {
    const { f, a, r, j } = item;
    if (typeof PokiSDK[f] === 'function') {
    const p = PokiSDK[f](...a);
    if (p && typeof p.then === 'function') {
    p.then(r);
    p.catch(j);
    }
    } else {
    console.warn(`${f} not in PokiSDK`);
    }
    });
    }

    // On localhost we don't want to load the SDK, so we just resolve all promises.
    if (isLocalhost) {
    queue.forEach((item) => {
    item.r(true);
    });
    queue.push = ({ r }) => {
    r(true);
    };
    } else {
    const script = document.createElement('script');
    script.src = 'https://game-cdn.poki.com/scripts/v2/poki-sdk.js';
    script.onload = run;
    document.body.appendChild(script);
    }
    }