From 8a7862fbc83a47f52e03d0a81af82ca76c473038 Mon Sep 17 00:00:00 2001 From: Wesley Luyten Date: Fri, 17 May 2024 12:16:28 -0500 Subject: [PATCH] fix: chrome.cast.isAvailable fatal typeerror (#15) * fix: chrome.cast.isAvailable fatal typeerror * fix: simplify logic, debug log instead of warn --- .../castable-remote-playback.js | 13 ++++++--- packages/castable-video/castable-utils.js | 27 ++++++------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/packages/castable-video/castable-remote-playback.js b/packages/castable-video/castable-remote-playback.js index e0729c3..4efb0f6 100644 --- a/packages/castable-video/castable-remote-playback.js +++ b/packages/castable-video/castable-remote-playback.js @@ -5,7 +5,6 @@ import { InvalidStateError, NotSupportedError, onCastApiAvailable, - isChromeCastAvailable, castContext, currentSession, currentMedia, @@ -19,8 +18,14 @@ const castElementRef = new WeakSet(); let cf; -onCastApiAvailable((isAvailable) => { - if (isAvailable && !cf) { +onCastApiAvailable(() => { + if (!globalThis.chrome?.cast?.isAvailable) { + // Useful to see in verbose logs if this shows undefined or false. + console.debug('chrome.cast.isAvailable', globalThis.chrome?.cast?.isAvailable); + return; + } + + if (!cf) { cf = cast.framework; castContext().addEventListener(cf.CastContextEventType.CAST_STATE_CHANGED, (e) => { @@ -109,7 +114,7 @@ export class RemotePlayback extends EventTarget { throw new InvalidStateError('disableRemotePlayback attribute is present.'); } - if (!isChromeCastAvailable()) { + if (!globalThis.chrome?.cast?.isAvailable) { throw new NotSupportedError('The RemotePlayback API is disabled on this platform.'); } diff --git a/packages/castable-video/castable-utils.js b/packages/castable-video/castable-utils.js index 6dc99ec..2a61a24 100644 --- a/packages/castable-video/castable-utils.js +++ b/packages/castable-video/castable-utils.js @@ -1,4 +1,4 @@ -/* global chrome, cast, WeakRef */ +/* global WeakRef */ export const privateProps = new WeakMap(); @@ -21,7 +21,7 @@ export const IterableWeakSet = globalThis.WeakRef ? } : Set; export function onCastApiAvailable(callback) { - if (!isChromeCastAvailable()) { + if (!globalThis.chrome?.cast?.isAvailable) { globalThis.__onGCastApiAvailable = () => { // The globalThis.__onGCastApiAvailable callback alone is not reliable for // the added cast.framework. It's loaded in a separate JS file. @@ -29,14 +29,14 @@ export function onCastApiAvailable(callback) { // https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js customElements .whenDefined('google-cast-button') - .then(() => callback(chrome.cast.isAvailable)); + .then(callback); }; - } else if (!isCastFrameworkAvailable()) { + } else if (!globalThis.cast?.framework) { customElements .whenDefined('google-cast-button') - .then(() => callback(chrome.cast.isAvailable)); + .then(callback); } else { - callback(chrome.cast.isAvailable); + callback(); } } @@ -47,26 +47,15 @@ export function requiresCastFramework() { export function loadCastFramework() { const sdkUrl = 'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1'; - if (globalThis.chrome.cast || document.querySelector(`script[src="${sdkUrl}"]`)) return; + if (globalThis.chrome?.cast || document.querySelector(`script[src="${sdkUrl}"]`)) return; const script = document.createElement('script'); script.src = sdkUrl; document.head.append(script); } -export function isChromeCastAvailable() { - return typeof chrome !== 'undefined' && chrome.cast && chrome.cast.isAvailable; -} - -export function isCastFrameworkAvailable() { - return typeof cast !== 'undefined' && cast.framework; -} - export function castContext() { - if (isCastFrameworkAvailable()) { - return cast.framework.CastContext.getInstance(); - } - return undefined; + return globalThis.cast?.framework?.CastContext.getInstance(); } export function currentSession() {