diff --git a/src/jellyfin.ts b/src/jellyfin.ts index 3c87731..16b5fb5 100644 --- a/src/jellyfin.ts +++ b/src/jellyfin.ts @@ -1,6 +1,15 @@ import { Api, Jellyfin } from "@jellyfin/sdk"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; +const getDeviceId = (): string => { + const existing = Spicetify.LocalStorage.get("jellyfin-device-id"); + if (existing) return existing; + + const id = crypto.randomUUID(); + Spicetify.LocalStorage.set("jellyfin-device-id", id); + return id; +}; + export const sdk = new Jellyfin({ clientInfo: { name: "Spicetify", @@ -8,7 +17,7 @@ export const sdk = new Jellyfin({ }, deviceInfo: { name: "Spotify", - id: "spotify", // TODO: should be unique? + id: getDeviceId(), }, }); diff --git a/src/player.ts b/src/player.ts index bee18c8..897c59b 100644 --- a/src/player.ts +++ b/src/player.ts @@ -8,13 +8,6 @@ export const audio = new Audio(); export let hijackActive = false; export let currentVolume = 0.5; -export function setHijackActive(value: boolean) { - hijackActive = value; -} -export function setCurrentVolume(value: number) { - currentVolume = value; -} - const BITRATE_MAP: Record = { high: "320000", medium: "256000", @@ -24,6 +17,9 @@ const BITRATE_MAP: Record = { let currentItemId: string | null = null; let oldTime = 0; let lastProgressReport = 0; +export function setHijackActive(value: boolean) { + hijackActive = value; +} export async function playTrack(id: string) { if (!jellyfin.api) return; @@ -32,7 +28,7 @@ export async function playTrack(id: string) { const oldVolume = hijackActive ? currentVolume : Spicetify.Player.getVolume(); if (!hijackActive) Spicetify.Player.setVolume(0); // Set Spotify audio volume to 0 - setHijackActive(true); + hijackActive = true; Spicetify.Player.setVolume(oldVolume); // Volume is now hijacked, will now set Jellyfin audio volume and also update the volume slider const params = new URLSearchParams({ @@ -64,7 +60,7 @@ export async function playTrack(id: string) { } catch (error) { console.error("An error occurred trying to play a track on Jellyfin", error); Spicetify.showNotification("An error occurred trying to play a track on Jellyfin", true); - setHijackActive(false); + hijackActive = false; } } @@ -91,7 +87,7 @@ export function registerEvents() { const item = results.data.SearchHints?.[0]; if (!item?.Id) { - setHijackActive(false); + hijackActive = false; audio.pause(); Spicetify.Player.setVolume(currentVolume); return; @@ -156,7 +152,7 @@ export function registerEvents() { const playback = Spicetify.Platform.PlaybackAPI; playback.setVolume = new Proxy(playback.setVolume, { apply(target, thisArg, args) { - setCurrentVolume(args[0]); + currentVolume = args[0]; if (hijackActive) { audio.volume = Math.pow(currentVolume, 3); diff --git a/src/settings/views/settings.tsx b/src/settings/views/settings.tsx index 8520db7..03317c9 100644 --- a/src/settings/views/settings.tsx +++ b/src/settings/views/settings.tsx @@ -77,7 +77,7 @@ export default function SettingsView({ setView }: Props) {

Audio Hijack

-

Enable to replace Spotify song audio with Jellyfin audio

+

Enable to replace Spotify song audio with Jellyfin audio if found

setSettings((p) => ({ ...p, hijack: e.target.checked }))} className={styles.switch} />