mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-08 08:39:59 +00:00
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
/**
|
|
* Cast Sender SDK page script loaded in place of remote cast_sender
|
|
* script. Handles API object creation and initializes sender apps.
|
|
*/
|
|
|
|
import logger from "../lib/logger";
|
|
import { loadScript } from "../lib/utils";
|
|
|
|
import pageMessaging from "./pageMessaging";
|
|
import CastSDK from "./sdk";
|
|
import { CAST_FRAMEWORK_SCRIPT_URL } from "./urls";
|
|
|
|
// Create page-accessible API object
|
|
window.chrome.cast = new CastSDK();
|
|
|
|
let frameworkScriptPromise: Promise<HTMLScriptElement> | undefined;
|
|
|
|
// Load remote CAF script if requested in script URL params.
|
|
if (document.currentScript) {
|
|
const currentScript = document.currentScript as HTMLScriptElement;
|
|
const currentScriptParams = new URLSearchParams(
|
|
new URL(currentScript.src).search
|
|
);
|
|
|
|
if (currentScriptParams.get("loadCastFramework") === "1") {
|
|
frameworkScriptPromise = new Promise((resolve, reject) => {
|
|
const scriptEl = document.createElement("script");
|
|
scriptEl.src = CAST_FRAMEWORK_SCRIPT_URL;
|
|
(document.head ?? document.documentElement).append(scriptEl);
|
|
scriptEl.addEventListener("load", () => resolve(scriptEl));
|
|
scriptEl.addEventListener("error", () => reject());
|
|
});
|
|
frameworkScriptPromise.catch(() => {
|
|
logger.error("Failed to load CAF script!");
|
|
});
|
|
}
|
|
}
|
|
|
|
pageMessaging.page.addListener(async message => {
|
|
switch (message.subject) {
|
|
case "cast:instanceCreated": {
|
|
// If framework API is loading, wait until completed
|
|
await frameworkScriptPromise;
|
|
|
|
// Call page script/framework API script's init function
|
|
const initFn = window.__onGCastApiAvailable;
|
|
if (initFn && typeof initFn === "function") {
|
|
initFn(message.data.isAvailable);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
});
|