2020-08-14 08:46:34 +02:00
|
|
|
import { Events } from 'jellyfin-apiclient';
|
2020-10-18 18:57:20 +01:00
|
|
|
import { playbackManager } from '../components/playback/playbackmanager';
|
|
|
|
import ServerConnections from '../components/ServerConnections';
|
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();
|
|
|
|
|
2020-09-22 07:34:46 -04:00
|
|
|
if (currentPlayerInfo && 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 (currentPlayerInfo && playerId && currentPlayerInfo.id === playerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onOpen() {
|
|
|
|
const playerId = localStorage.getItem('autocastPlayerId');
|
|
|
|
|
|
|
|
playbackManager.getTargets().then(function (targets) {
|
2020-10-07 21:12:14 +09:00
|
|
|
for (let i = 0; i < targets.length; i++) {
|
2020-09-07 12:54:12 -04:00
|
|
|
if (targets[i].id == playerId) {
|
|
|
|
playbackManager.trySetActivePlayer(targets[i].playerName, targets[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-22 01:09:18 -05:00
|
|
|
try {
|
|
|
|
const apiClient = ServerConnections.currentApiClient();
|
2020-10-18 18:57:20 +01:00
|
|
|
|
2020-11-22 01:09:18 -05: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
|
|
|
}
|