mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-08 08:39:59 +00:00
Fix framework API script load order
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user