1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/autocast.js
Yasin Silavi ea47793820 fix: check both player id and current player id
Co-authored-by: Bill Thornton <thornbill@users.noreply.github.com>
2023-09-11 12:13:49 -04:00

53 lines
1.4 KiB
JavaScript

import { playbackManager } from '../components/playback/playbackmanager';
import ServerConnections from '../components/ServerConnections';
import Events from '../utils/events.ts';
export function supported() {
return typeof(Storage) !== 'undefined';
}
export function enable(enabled) {
if (!supported()) return;
if (enabled) {
const currentPlayerInfo = playbackManager.getPlayerInfo();
if (currentPlayerInfo?.id) {
localStorage.setItem('autocastPlayerId', currentPlayerInfo.id);
}
} else {
localStorage.removeItem('autocastPlayerId');
}
}
export function isEnabled() {
if (!supported()) return false;
const playerId = localStorage.getItem('autocastPlayerId');
const currentPlayerInfo = playbackManager.getPlayerInfo();
return playerId && currentPlayerInfo?.id === playerId;
}
function onOpen() {
const playerId = localStorage.getItem('autocastPlayerId');
playbackManager.getTargets().then(function (targets) {
for (const target of targets) {
if (target.id == playerId) {
playbackManager.trySetActivePlayer(target.playerName, target);
break;
}
}
});
}
try {
const apiClient = ServerConnections.currentApiClient();
if (apiClient && supported()) {
Events.on(apiClient, 'websocketopen', onOpen);
}
} catch (ex) {
console.warn('Could not get current apiClient', ex);
}