Fix framework API script load order

This commit is contained in:
hensm
2022-04-15 23:48:47 +01:00
parent ef9720b67f
commit 31757bb775
2 changed files with 28 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
"use strict"; "use strict";
import * as cast from "./api"; import * as cast from "./api";
import logger from "../lib/logger";
import { CAST_FRAMEWORK_SCRIPT_URL } from "../lib/endpoints"; import { CAST_FRAMEWORK_SCRIPT_URL } from "../lib/endpoints";
import { loadScript } from "../lib/utils"; import { loadScript } from "../lib/utils";
@@ -18,14 +19,6 @@ _window.chrome.cast = cast;
let bridgeInfo: any; let bridgeInfo: any;
let isFramework = false; let isFramework = false;
// Call page's API loaded function if defined
function callPageReadyFunction() {
const readyFunction = _window.__onGCastApiAvailable;
if (readyFunction && typeof readyFunction === "function") {
readyFunction(bridgeInfo && bridgeInfo.isVersionCompatible);
}
}
/** /**
* If loaded within a page via a <script> element, * If loaded within a page via a <script> element,
* document.currentScript should exist and we can check its * document.currentScript should exist and we can check its
@@ -42,29 +35,32 @@ if (document.currentScript) {
_window.cast = {}; _window.cast = {};
} }
/**
* Set isFramework flag to load framework once the base cast API is
* initialized
*/
isFramework = true; isFramework = true;
const script = loadScript(CAST_FRAMEWORK_SCRIPT_URL);
script.addEventListener("load", () => {
callPageReadyFunction();
});
/*
// TODO: Finish cast.framework and replace Google's implementation
import("./framework").then(framework => {
_window.cast.framework = framework.default;
});
*/
} }
} }
onMessage(message => { onMessage(async message => {
switch (message.subject) { switch (message.subject) {
case "cast:initialized": { case "cast:initialized": {
bridgeInfo = message.data; bridgeInfo = message.data;
if (!isFramework) { // If framework API is requested, load that first
callPageReadyFunction(); if (isFramework) {
try {
await loadScript(CAST_FRAMEWORK_SCRIPT_URL);
} catch (err) {
logger.error("Failed to load CAF script!");
}
}
// Call page script/framework API script's init function
const initFn = _window.__onGCastApiAvailable;
if (initFn && typeof initFn === "function") {
initFn(bridgeInfo && bridgeInfo.isVersionCompatible);
} }
break; break;

View File

@@ -117,10 +117,13 @@ export const REMOTE_MATCH_PATTERN_REGEX =
export function loadScript( export function loadScript(
scriptUrl: string, scriptUrl: string,
doc: Document = document doc: Document = document
): HTMLScriptElement { ): Promise<HTMLScriptElement> {
const scriptElement = doc.createElement("script"); return new Promise((resolve, reject) => {
scriptElement.src = scriptUrl; const scriptEl = doc.createElement("script");
(doc.head || doc.documentElement).append(scriptElement); scriptEl.src = scriptUrl;
(doc.head || doc.documentElement).append(scriptEl);
return scriptElement;
scriptEl.addEventListener("load", () => resolve(scriptEl));
scriptEl.addEventListener("error", () => reject());
});
} }