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
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": [
"shim/bundle.js"
, "vendor/webcomponents-lite.min.js"
, "vendor/webcomponents-lite.js"
]
}

View File

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

View File

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