Skip to content

Commit 144de4b

Browse files
authored
Update big_game_script.js
1 parent 9607a99 commit 144de4b

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

static/big_game_script.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ window.ccPorted = window.ccPorted || {};
22

33
const COGNITO_DOMAIN = "https://us-west-2lg1qptg2n.auth.us-west-2.amazoncognito.com/";
44
const CLIENT_ID = "4d6esoka62s46lo4d398o3sqpi";
5-
const REDIRECT_URI = `${window.location.origin}/profile/`;
5+
const REDIRECT_URI = `${window.location.origin}`;
66
const tGameID = treat(window.gameID || window.ccPorted.gameID);
77
const link = document.createElement("link");
88
const script = document.currentScript;
99
const seenPopup = (localStorage.getItem("ccported-popup") == "yes");
10-
const glocation = window.location.hostname;
1110
const framed = pageInIframe();
11+
const glocation = (!framed) ? window.location.hostname : document.location.ancestorOrigins[0];
1212
const gameIDExtractRG = /\/(game_\w+)\//;
1313
const gameIDExtract = gameIDExtractRG.exec(window.location.pathname);
1414
const gameID = window.ccPorted.gameID || window.gameID || ((gameIDExtract) ? gameIDExtract[1] : "Unknown Game");
@@ -1270,6 +1270,20 @@ class Stats {
12701270
}
12711271

12721272
window.addEventListener("beforeunload", cleanupTracking);
1273+
window.addEventListener("message", (e) => {
1274+
try {
1275+
if (e.origin !== new URL(window.location.ancestorOrigins[0]).origin) return;
1276+
}catch(e){
1277+
return;
1278+
}
1279+
const {data} = e;
1280+
if(data.action = "SET_TOKENS") {
1281+
const tokens = data.content;
1282+
localStorage.setItem("[ns_ccported]_accessToken", tokens.access_token);
1283+
localStorage.setItem("[ns_ccported]_idToken", tokens.id_token);
1284+
localStorage.setItem("[ns_ccported]_refreshToken", tokens.refresh_token);
1285+
}
1286+
})
12731287
window.addEventListener("load", () => {
12741288
init();
12751289
const login = document.querySelector('.loggedInReplacable');
@@ -1282,7 +1296,7 @@ window.addEventListener("load", () => {
12821296
const baseURL = "https://us-west-2lg1qptg2n.auth.us-west-2.amazoncognito.com/login";
12831297
const clientID = "4d6esoka62s46lo4d398o3sqpi";
12841298
const responseType = "code";
1285-
window.location.href = `${baseURL}?client_id=${clientID}&response_type=${responseType}&scope=${scopes.join("+")}&redirect_uri=${redirect}/profile/`;
1299+
window.location.href = `${baseURL}?client_id=${clientID}&response_type=${responseType}&scope=${scopes.join("+")}&redirect_uri=${redirect}`;
12861300
return false;
12871301
}
12881302
})
@@ -1400,7 +1414,7 @@ window.ccPorted.userPromise = new Promise(async (resolve, reject) => {
14001414
});
14011415

14021416
function log(...args) {
1403-
console.log("[CCPORTED]: ", ...args);
1417+
console.log(`[${gameID}]: `, ...args);
14041418
if (window.ccPorted.stats) {
14051419
window.ccPorted.stats.log(...args);
14061420
}
@@ -2202,8 +2216,7 @@ function emit() {
22022216
isFramed: framed,
22032217
}
22042218
if (framed) {
2205-
data["parentDomainHost"] = (window.parent.location.hostname.length > 0) ? window.parent.location.hostname : "unknown";
2206-
data["parentDomain"] = window.parent.location;
2219+
data["parentDomainHost"] = (new URL(window.location.ancestorOrigins[0]).hostname.length > 0) ? new URL(window.location.ancestorOrigins[0]).hostname : "unknown";
22072220
}
22082221
log(data);
22092222
gtag("event", "play_game", data);
@@ -2341,7 +2354,25 @@ async function initializeAWS() {
23412354
user = await initializeAuthenticated(idToken, accessToken, refreshToken);
23422355
} else {
23432356
console.warn("No auth code found in URL. User may need to log in.");
2344-
user = await initializeUnathenticated();
2357+
if (framed) {
2358+
console.log("Waiting for tokens from parent 3 second timeout");
2359+
await new Promise((r) => {
2360+
setTimeout(r, 3000);
2361+
});
2362+
let idToken = localStorage.getItem("[ns_ccported]_idToken");
2363+
let accessToken = localStorage.getItem("[ns_ccported]_accessToken");
2364+
let refreshToken = localStorage.getItem("[ns_ccported]_refreshToken");
2365+
if (!idToken || !accessToken) {
2366+
console.log("Waiting failed, initializing unauthenticated.");
2367+
user = await initializeUnathenticated();
2368+
} else {
2369+
user = await initializeAuthenticated(idToken, accessToken, refreshToken);
2370+
}
2371+
2372+
} else {
2373+
console.log("No parent to recieve tokens from... initializing unauthenticated");
2374+
user = await initializeUnathenticated();
2375+
}
23452376
}
23462377
} else {
23472378
log("Tokens found. Initializing user...");

0 commit comments

Comments
 (0)