-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcontent.ts
48 lines (39 loc) · 1.5 KB
/
content.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 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 = loadScript(CAST_FRAMEWORK_SCRIPT_URL);
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;
}
}
});