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

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-10-18 18:57:20 +01:00
import { playbackManager } from '../components/playback/playbackmanager';
import ServerConnections from '../components/ServerConnections';
import Events from '../utils/events.ts';
2020-09-07 12:54:12 -04:00
export function supported() {
return typeof(Storage) !== 'undefined';
}
2020-09-07 13:10:38 -04:00
export function enable(enabled) {
2020-09-07 12:54:12 -04:00
if (!supported()) return;
2020-09-07 13:10:38 -04:00
if (enabled) {
2020-09-07 12:54:12 -04:00
const currentPlayerInfo = playbackManager.getPlayerInfo();
2023-07-06 13:39:48 -04:00
if (currentPlayerInfo?.id) {
2020-09-07 12:54:12 -04:00
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;
2020-09-07 12:54:12 -04:00
}
function onOpen() {
const playerId = localStorage.getItem('autocastPlayerId');
playbackManager.getTargets().then(function (targets) {
2023-05-04 11:27:15 -04:00
for (const target of targets) {
if (target.id == playerId) {
playbackManager.trySetActivePlayer(target.playerName, target);
2020-09-07 12:54:12 -04:00
break;
}
}
});
}
try {
const apiClient = ServerConnections.currentApiClient();
2020-10-18 18:57:20 +01:00
if (apiClient && supported()) {
Events.on(apiClient, 'websocketopen', onOpen);
}
} catch (ex) {
console.warn('Could not get current apiClient', ex);
2020-09-07 12:54:12 -04:00
}