Fix __onGCastApiAvailable being called early

This commit is contained in:
hensm
2019-03-21 14:37:36 +00:00
parent 2a327573da
commit d0129f66b7
4 changed files with 24 additions and 16 deletions

View File

@@ -35,3 +35,15 @@ export function getWindowCenteredProps (
// tslint:disable-next-line:max-line-length // tslint:disable-next-line:max-line-length
export const REMOTE_MATCH_PATTERN_REGEX = /^(?:(?:(\*|https?|ftp):\/\/(\*|(?:\*\.(?:[^\/\*:]\.?)+(?:[^\.])|[^\/\*:]*))?)(\/.*)|<all_urls>)$/; export const REMOTE_MATCH_PATTERN_REGEX = /^(?:(?:(\*|https?|ftp):\/\/(\*|(?:\*\.(?:[^\/\*:]\.?)+(?:[^\.])|[^\/\*:]*))?)(\/.*)|<all_urls>)$/;
export function loadScript (
scriptUrl: string
, doc: Document = document): HTMLScriptElement {
const scriptElement = doc.createElement("script");
scriptElement.src = scriptUrl;
(doc.head || doc.documentElement).append(scriptElement);
return scriptElement;
}

View File

@@ -47,6 +47,6 @@
] ]
, "web_accessible_resources": [ , "web_accessible_resources": [
"shim/bundle.js" "shim/bundle.js"
, "vendor/webcomponents-lite.min.js" , "vendor/webcomponents-lite.js"
] ]
} }

View File

@@ -3,12 +3,11 @@
import { Message } from "../types"; import { Message } from "../types";
import { onMessageResponse, sendMessage } from "./messageBridge"; import { onMessageResponse, sendMessage } from "./messageBridge";
import { loadScript } from "../lib/utils";
if ((window as any)._isFramework) { if ((window as any)._isFramework) {
const polyfillScriptElement = document.createElement("script"); loadScript(browser.runtime.getURL("vendor/webcomponents-lite.js"));
polyfillScriptElement.src = browser.runtime.getURL(
"vendor/webcomponents-lite.js")
document.head.append(polyfillScriptElement);
} }

View File

@@ -4,6 +4,8 @@ import * as cast from "./cast";
import { onMessage } from "./messageBridge"; import { onMessage } from "./messageBridge";
import { loadScript } from "../lib/utils";
const _window = (window as any); const _window = (window as any);
@@ -18,7 +20,7 @@ const CAST_FRAMEWORK_SCRIPT_URL =
"https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js"; "https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js";
let bridgeInfo: any; let bridgeInfo: any;
let scriptLoaded = false; let isFramework = false;
// Call page's API loaded function if defined // Call page's API loaded function if defined
function callPageReadyFunction () { function callPageReadyFunction () {
@@ -45,16 +47,11 @@ if (document.currentScript) {
_window.cast = {}; _window.cast = {};
} }
const scriptElement = document.createElement("script"); isFramework = true;
scriptElement.src = CAST_FRAMEWORK_SCRIPT_URL;
(document.head || document.documentElement).append(scriptElement);
scriptElement.addEventListener("load", ev => { const script = loadScript(CAST_FRAMEWORK_SCRIPT_URL);
if (bridgeInfo) { script.addEventListener("load", ev => {
callPageReadyFunction(); callPageReadyFunction();
}
scriptLoaded = true;
}); });
/* /*
@@ -72,7 +69,7 @@ onMessage(message => {
case "shim:/initialized": { case "shim:/initialized": {
bridgeInfo = message.data; bridgeInfo = message.data;
if (!scriptLoaded) { if (!isFramework) {
callPageReadyFunction(); callPageReadyFunction();
} }