Created
January 3, 2024 02:33
-
-
Save erikdubbelboer/b9ddfd64196a4d41cad42db86d52348d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment