From 221a41978bdb9d9ee0815a61ea5dbc7b1dab6552 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 8 Apr 2025 09:43:44 +0100 Subject: [PATCH 1/2] Updated to allow the specification of a url Also added an example of some event listeners and a simple override for when the window is created. --- .../apply-snapshot/client/src/provider.ts | 63 ++++++++++++++++--- .../apply-snapshot/public/html/provider.html | 4 ++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/how-to/use-platform/apply-snapshot/client/src/provider.ts b/how-to/use-platform/apply-snapshot/client/src/provider.ts index 9f522ec0..88006ea2 100644 --- a/how-to/use-platform/apply-snapshot/client/src/provider.ts +++ b/how-to/use-platform/apply-snapshot/client/src/provider.ts @@ -1,26 +1,75 @@ +import type OpenFin from "@openfin/core"; document.addEventListener("DOMContentLoaded", async () => { - await fin.Platform.init(); + await fin.Platform.init({ + overrideCallback: async (Provider) => { + /** + * Override the provider class. + */ + class Override extends Provider { + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type, jsdoc/require-jsdoc + public async createWindow( + options: OpenFin.PlatformWindowCreationOptions, + callerIdentity: { uuid: string; name: string } | undefined + ) { + console.log("createWindow called", options, callerIdentity); + // default behavior for all other creation reasons + const win = await super.createWindow(options, callerIdentity); + console.log("createWindow completed", win.identity.name); + return win; + } + } + return new Override(); + } + }); const platform = fin.Platform.getCurrentSync(); const launchOneBtn: HTMLButtonElement = document.querySelector("#launch-one") as HTMLButtonElement; const launchTenBtn: HTMLButtonElement = document.querySelector("#launch-ten") as HTMLButtonElement; const launchTwentyBtn: HTMLButtonElement = document.querySelector("#launch-twenty") as HTMLButtonElement; const baseUrl = "http://localhost:5050"; + const url: HTMLInputElement = document.querySelector("#url") as HTMLInputElement; + await platform.on("window-created", async (event) => { + console.log("Window created", event); + }); + await platform.on("window-did-start-loading", async (event) => { + console.log("Window did start loading", event); + }); + await platform.on("window-did-finish-load", async (event) => { + console.log("Window did finish load", event); + }); + await platform.on("window-end-load", async (event) => { + console.log("Window end load", event); + }); + await platform.on("window-initialized", async (event) => { + console.log("Window initialized", event); + }); + await platform.on("window-performance-report", async (event) => { + console.log("Window performance report", event); + }); + await platform.on("window-start-load", async (event) => { + console.log("Window start load", event); + }); + if (launchOneBtn || launchTenBtn || launchTwentyBtn) { launchOneBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-one.json`); - const snapshotJson = await snapshot.json(); - await platform.applySnapshot(snapshotJson); + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); }); launchTenBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-ten.json`); - const snapshotJson = await snapshot.json(); - await platform.applySnapshot(snapshotJson); + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); }); launchTwentyBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-twenty.json`); - const snapshotJson = await snapshot.json(); - await platform.applySnapshot(snapshotJson); + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); }); } }); diff --git a/how-to/use-platform/apply-snapshot/public/html/provider.html b/how-to/use-platform/apply-snapshot/public/html/provider.html index 30a7a77a..0b344833 100644 --- a/how-to/use-platform/apply-snapshot/public/html/provider.html +++ b/how-to/use-platform/apply-snapshot/public/html/provider.html @@ -17,6 +17,10 @@

How to use the applySnapshot API

Click on the button below to launch 1, 10, or 20 Platform Windows.

+
+ + +
From 4dc52560e03fcb9ca3349a6017a96d15bf52a0dd Mon Sep 17 00:00:00 2001 From: John Date: Tue, 8 Apr 2025 14:25:13 +0100 Subject: [PATCH 2/2] Update logic --- .../apply-snapshot/client/src/provider.ts | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/how-to/use-platform/apply-snapshot/client/src/provider.ts b/how-to/use-platform/apply-snapshot/client/src/provider.ts index 88006ea2..3f1ccd06 100644 --- a/how-to/use-platform/apply-snapshot/client/src/provider.ts +++ b/how-to/use-platform/apply-snapshot/client/src/provider.ts @@ -55,21 +55,36 @@ document.addEventListener("DOMContentLoaded", async () => { if (launchOneBtn || launchTenBtn || launchTwentyBtn) { launchOneBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-one.json`); - const snapshotJson = await snapshot.text(); - const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); - await platform.applySnapshot(JSON.parse(snapshotString)); + if(url.value.trim() === "") { + const payload = await snapshot.json(); + await platform.applySnapshot(payload); + } else { + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); + } }); launchTenBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-ten.json`); - const snapshotJson = await snapshot.text(); - const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); - await platform.applySnapshot(JSON.parse(snapshotString)); + if(url.value.trim() === "") { + const payload = await snapshot.json(); + await platform.applySnapshot(payload); + } else { + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); + } }); launchTwentyBtn.addEventListener("click", async () => { const snapshot = await fetch(`${baseUrl}/common/snapshots/snapshot-twenty.json`); - const snapshotJson = await snapshot.text(); - const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); - await platform.applySnapshot(JSON.parse(snapshotString)); + if(url.value.trim() === "") { + const payload = await snapshot.json(); + await platform.applySnapshot(payload); + } else { + const snapshotJson = await snapshot.text(); + const snapshotString = snapshotJson.replace(/about:blank/g, url.value.trim()); + await platform.applySnapshot(JSON.parse(snapshotString)); + } }); } });