Skip to content

Commit

Permalink
Only return the casting button when there are devices to cast to.
Browse files Browse the repository at this point in the history
  • Loading branch information
dero committed Jan 31, 2022
1 parent cd26b29 commit b02f5aa
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,40 @@ window.kinoInitGoogleCast = (function kinoInitGoogleCastIIFE() {
return () => {
if (!castButtonPromise) {
castButtonPromise = new Promise((resolve) => {
const initCastApi = () => {
window.cast.framework.CastContext.getInstance().setOptions({
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
});
let resolved = false;

const resolveWithButton = () => {
const castButton = document.createElement('button');
const castCustomElement = document.createElement('google-cast-launcher');

castButton.setAttribute('aria-label', 'Cast this video');
castButton.appendChild(castCustomElement);

resolved = true;
resolve(castButton);
};

const checkCastState = () => {
const castState = window.cast.framework.CastContext.getInstance().getCastState();

if (castState !== 'NO_DEVICES_AVAILABLE' && !resolved) {
resolveWithButton();
}
};

const initCastApi = () => {
window.cast.framework.CastContext.getInstance().setOptions({
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
});

checkCastState();

window.cast.framework.CastContext.getInstance().addEventListener(
window.cast.framework.CastContextEventType.CAST_STATE_CHANGED,
checkCastState,
);
};

window.__onGCastApiAvailable = (isAvailable) => {
if (isAvailable) {
initCastApi();
Expand Down

0 comments on commit b02f5aa

Please sign in to comment.