From a92f301044c8ad88dfc98425f353e79781b49488 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Thu, 15 Feb 2024 13:44:43 +0000 Subject: [PATCH] Add logging in and requesting saves list Note: eventually I'll move the types into comments so this can be a plain .js file anyone can use --- galaxy.ts | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 galaxy.ts diff --git a/galaxy.ts b/galaxy.ts new file mode 100644 index 0000000..df691cc --- /dev/null +++ b/galaxy.ts @@ -0,0 +1,95 @@ +// TODO remove types and put them in comments so this can be a .js file + +export function initGalaxy({ + acceptedOrigins, + supportsSaving, + supportsSaveManager, + ignoreApiVersion, + onLoggedInChanged +}: { + acceptedOrigins?: string[]; + supportsSaving?: boolean; + supportsSaveManager?: boolean; + ignoreApiVersion?: boolean; + onLoggedInChanged?: (loggedIn: boolean) => void; +}) { + return new Promise((accept, reject) => { + acceptedOrigins = acceptedOrigins ?? ["https://galaxy.click"]; + if (acceptedOrigins.includes(window.origin)) { + // Callbacks to resolve promises + let saveListAccept: (value: SaveListResponse["list"]) => void, + saveListReject: (reason?: string) => void; + + const galaxy = { + acceptedOrigins, + supportsSaving, + supportsSaveManager, + ignoreApiVersion, + onLoggedInChanged, + origin: window.origin, + apiVersion: 0, + loggedIn: false, + postMessage: function ( + message: SupportsAction | SaveListAction | SaveAction | LoadAction + ) { + window.top?.postMessage(message, galaxy.origin); + }, + getSaveList: function () { + if (saveListAccept != null || saveListReject != null) { + console.warn("save_list action already in progress. Ignoring..."); + return; + } + galaxy.postMessage({ + action: "save_list" + }); + return new Promise((accept, reject) => { + saveListAccept = accept; + saveListReject = reject; + }); + } + }; + + window.addEventListener("message", e => { + if (e.origin === galaxy.origin) { + console.log("Received message from Galaxy", e.data); + const data = e.data as GalaxyResponse; + + switch (data.type) { + case "info": { + const { galaxy: isGalaxy, api_version, logged_in } = data; + // Ignoring isGalaxy check in case other accepted origins send it as false + if (api_version !== 1 && galaxy.ignoreApiVersion !== true) { + reject(`API version not recognized: ${api_version}`); + } else { + // Info responses may be sent again if the information gets updated + // Specifically, we care if logged_in gets changed + // We can use the api_version to determine if this is the first + // info response or a new one. + const firstInfoResponse = galaxy.apiVersion === 0; + galaxy.apiVersion = api_version; + galaxy.loggedIn = logged_in; + galaxy.origin = e.origin; + if (firstInfoResponse) { + accept(galaxy); + } else { + galaxy.onLoggedInChanged?.(logged_in); + } + } + break; + } + case "save_list": { + const { list, error, message } = data; + if (error) { + saveListReject(message); + } else { + saveListAccept(list); + } + } + } + } + }); + } else { + reject(`Project is not running on an accepted origin: ${window.origin}`); + } + }); +}