From d0129f66b7c613b83d59c7c7b2dcfb54615b86a6 Mon Sep 17 00:00:00 2001 From: hensm Date: Thu, 21 Mar 2019 14:37:36 +0000 Subject: [PATCH] Fix __onGCastApiAvailable being called early --- ext/src/lib/utils.ts | 12 ++++++++++++ ext/src/manifest.json | 2 +- ext/src/shim/content.ts | 7 +++---- ext/src/shim/index.ts | 19 ++++++++----------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ext/src/lib/utils.ts b/ext/src/lib/utils.ts index 98d76f8..3c2f336 100644 --- a/ext/src/lib/utils.ts +++ b/ext/src/lib/utils.ts @@ -35,3 +35,15 @@ export function getWindowCenteredProps ( // tslint:disable-next-line:max-line-length export const REMOTE_MATCH_PATTERN_REGEX = /^(?:(?:(\*|https?|ftp):\/\/(\*|(?:\*\.(?:[^\/\*:]\.?)+(?:[^\.])|[^\/\*:]*))?)(\/.*)|)$/; + + +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; +} diff --git a/ext/src/manifest.json b/ext/src/manifest.json index fa07647..37b8e2a 100755 --- a/ext/src/manifest.json +++ b/ext/src/manifest.json @@ -47,6 +47,6 @@ ] , "web_accessible_resources": [ "shim/bundle.js" - , "vendor/webcomponents-lite.min.js" + , "vendor/webcomponents-lite.js" ] } diff --git a/ext/src/shim/content.ts b/ext/src/shim/content.ts index c2b203c..4d01ba9 100644 --- a/ext/src/shim/content.ts +++ b/ext/src/shim/content.ts @@ -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")); } diff --git a/ext/src/shim/index.ts b/ext/src/shim/index.ts index 8bfc158..38b5f71 100755 --- a/ext/src/shim/index.ts +++ b/ext/src/shim/index.ts @@ -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(); }