Only return the casting button when there are devices to cast to.

This commit is contained in:
Jaroslav Polakovič 2022-01-31 13:27:27 +01:00
Родитель cd26b29d6c
Коммит b02f5aa780
1 изменённых файлов: 24 добавлений и 4 удалений

Просмотреть файл

@ -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();