From 1c88e3caf185fee2263eafcf8047a80f6e57015c Mon Sep 17 00:00:00 2001 From: Gautam Anand Date: Mon, 21 Oct 2024 23:18:07 -0500 Subject: [PATCH] Crazygames fix --- components/headContent.js | 10 +++++++++- components/home.js | 23 +++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/components/headContent.js b/components/headContent.js index 894f1373..e511910e 100644 --- a/components/headContent.js +++ b/components/headContent.js @@ -25,7 +25,15 @@ export default function HeadContent({text}) { // const script = document.createElement('script'); script.src = "https://sdk.crazygames.com/crazygames-sdk-v3.js"; - script.async = true; + script.async = false; + console.log(window.CrazyGames) + // on script load + script.onload=() => { + console.log("sdk loaded", window.CrazyGames) + if(window.onCrazyload) { + window.onCrazyload(); + } + } document.body.appendChild(script); return () => { document.body.removeChild(script); diff --git a/components/home.js b/components/home.js index a5299eb0..bcc8aefc 100644 --- a/components/home.js +++ b/components/home.js @@ -189,6 +189,7 @@ export default function Home({ }) { async function crazyAuthListener() { + console.log("crazygames auth listener") const user = await window.CrazyGames.SDK.user.getUser(); if(user) { console.log("crazygames user", user) @@ -256,10 +257,13 @@ export default function Home({ }) { if(window.location.search.includes("crazygames")) { setInCrazyGames(true); window.inCrazyGames = true; + setLoading(true) + + window.onCrazyload = () => { + // initialize the sdk try { - console.log("init crazygames sdk") - setLoading(true) + console.log("init crazygames sdk", window.CrazyGames) window.CrazyGames.SDK.init().then(async () => { console.log("sdk initialized") @@ -281,14 +285,25 @@ export default function Home({ }) { console.error("crazygames sdk init failed", e) setLoading(false) }) - } catch(e) {} + } catch(e) { + console.error("crazygames sdk init failed", e) + finish() + setLoading(false) + } + } + + if(window.CrazyGames) { + window.onCrazyload(); + } } initialMultiplayerState.createOptions.displayLocation = text("allCountries") return () => { try { window.CrazyGames.SDK.user.removeAuthListener(crazyAuthListener); - } catch(e){} + } catch(e){ + console.error("crazygames remove auth listener error", e) + } } }, []);