From 2c1fb6468080cd0c14294a02f6e1d81048e07b40 Mon Sep 17 00:00:00 2001 From: hensm Date: Fri, 14 Jun 2019 19:27:15 +0100 Subject: [PATCH] Move all Google endpoint URLs to a separate module --- ext/src/endpoints.ts | 45 +++++++++++++++++++++ ext/src/main.ts | 21 ++++------ ext/src/shim/cast/classes/SessionRequest.ts | 4 +- ext/src/shim/cast/classes/Timeout.ts | 10 ++--- ext/src/shim/contentSetup.ts | 17 +++----- ext/src/shim/index.ts | 5 +-- 6 files changed, 67 insertions(+), 35 deletions(-) create mode 100644 ext/src/endpoints.ts diff --git a/ext/src/endpoints.ts b/ext/src/endpoints.ts new file mode 100644 index 0000000..97d0161 --- /dev/null +++ b/ext/src/endpoints.ts @@ -0,0 +1,45 @@ +"use strict"; + +/** + * Cast sender API loader script URL. + * + * Since the actual cast sender API script is hosted locally + * within Chrome, this script just acts a loader script for + * the real script whilst also doing some UA string + * checking. + */ +export const CAST_LOADER_SCRIPT_URL = + "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"; + +/** + * Framework API loader script URL. + * + * Same URL as the usual loader script, but the additional + * search parameter is checked from within the script and + * the framework API script is conditionally loaded in + * addition to the regular API script. + */ +export const CAST_FRAMEWORK_LOADER_SCRIPT_URL = + `${CAST_LOADER_SCRIPT_URL}?loadCastFramework=1`; + +/** + * Cast API script URLs. + * + * Cast functionality in Chrome was previously provided by + * an extension. The cast API script is still provided via + * chrome-extension URLs for compatibility reasons. + */ +export const CAST_SCRIPT_URLS = [ + "chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/cast_sender.js" + , "chrome-extension://enhhojjnijigcajfphajepfemndkmdlo/cast_sender.js" +]; + +/** + * Framework API script URL. + * + * Unlike the basic cast sender API, the framework API is + * not hosted locally within Chrome and is the only script + * fetched directly from Google servers. + */ +export const CAST_FRAMEWORK_SCRIPT_URL = + "https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js"; diff --git a/ext/src/main.ts b/ext/src/main.ts index 85705f5..2cd1af5 100755 --- a/ext/src/main.ts +++ b/ext/src/main.ts @@ -22,6 +22,9 @@ import { ReceiverStatusMessage , ServiceDownMessage , ServiceUpMessage } from "./messageTypes"; +import { CAST_FRAMEWORK_LOADER_SCRIPT_URL + , CAST_LOADER_SCRIPT_URL } from "./endpoints"; + const _ = browser.i18n.getMessage; @@ -244,13 +247,6 @@ browser.menus.onShown.addListener(info => { }); -// Google-hosted API loader script -const SENDER_SCRIPT_URL = - "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"; - -const SENDER_SCRIPT_FRAMEWORK_URL = - `${SENDER_SCRIPT_URL}?loadCastFramework=1`; - /** * Sender applications load a cast_sender.js script that * functions as a loader for the internal chrome-extension: @@ -261,11 +257,9 @@ const SENDER_SCRIPT_FRAMEWORK_URL = */ browser.webRequest.onBeforeRequest.addListener( async details => { - - const isFramework = details.url === SENDER_SCRIPT_FRAMEWORK_URL; - await browser.tabs.executeScript(details.tabId, { - code: `window._isFramework = ${isFramework}` + code: `window._isFramework = ${ + details.url === CAST_FRAMEWORK_LOADER_SCRIPT_URL}` , frameId: details.frameId , runAt: "document_start" }); @@ -276,14 +270,13 @@ browser.webRequest.onBeforeRequest.addListener( , runAt: "document_start" }); - return { redirectUrl: browser.runtime.getURL("shim/bundle.js") }; } , { urls: [ - SENDER_SCRIPT_URL - , SENDER_SCRIPT_FRAMEWORK_URL + CAST_LOADER_SCRIPT_URL + , CAST_FRAMEWORK_LOADER_SCRIPT_URL ]} , [ "blocking" ]); diff --git a/ext/src/shim/cast/classes/SessionRequest.ts b/ext/src/shim/cast/classes/SessionRequest.ts index 8959b3e..2851b67 100755 --- a/ext/src/shim/cast/classes/SessionRequest.ts +++ b/ext/src/shim/cast/classes/SessionRequest.ts @@ -1,7 +1,7 @@ "use strict"; -import Timeout from "./Timeout"; import { Capability } from "../enums"; +import Timeout from "./Timeout"; // https://developers.google.com/cast/docs/reference/chrome/chrome.cast.SessionRequest @@ -15,5 +15,5 @@ export default class SessionRequest { Capability.VIDEO_OUT , Capability.AUDIO_OUT ] , public requestSessionTimeout: number - = (new Timeout).requestSession) {} + = (new Timeout()).requestSession) {} } diff --git a/ext/src/shim/cast/classes/Timeout.ts b/ext/src/shim/cast/classes/Timeout.ts index 8dfb5d6..0276daf 100755 --- a/ext/src/shim/cast/classes/Timeout.ts +++ b/ext/src/shim/cast/classes/Timeout.ts @@ -2,9 +2,9 @@ // https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Timeout export default class Timeout { - leaveSession = 3000; - requestSession = 60000; - sendCustomMessage = 3000; - setReceiverVolume = 3000; - stopSession = 3000; + public leaveSession = 3000; + public requestSession = 60000; + public sendCustomMessage = 3000; + public setReceiverVolume = 3000; + public stopSession = 3000; } diff --git a/ext/src/shim/contentSetup.ts b/ext/src/shim/contentSetup.ts index 9277a90..2de200f 100644 --- a/ext/src/shim/contentSetup.ts +++ b/ext/src/shim/contentSetup.ts @@ -1,20 +1,15 @@ "use strict"; +import { CAST_LOADER_SCRIPT_URL + , CAST_SCRIPT_URLS } from "../endpoints"; + + const _window = (window.wrappedJSObject as any); _window.chrome = cloneInto({}, window); _window.navigator.presentation = cloneInto({}, window); - -const EXT_SENDER_SCRIPT_URLS = [ - "chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/cast_sender.js" - , "chrome-extension://enhhojjnijigcajfphajepfemndkmdlo/cast_sender.js" -]; - -const SENDER_SCRIPT_URL = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"; - - /** * Replace the src property setter on