| 1 | (async () => { |
| 2 | const response = await fetch('/snow-session', { cache: 'no-store' }); |
| 3 | if (!response.ok) throw new Error('Music sign-in failed'); |
| 4 | const match = (await response.text()).match(/window\.__APP_CONFIG__\s*=\s*(.+)/); |
| 5 | if (!match) throw new Error('Music session is unavailable'); |
| 6 | const value = JSON.parse(match[1].trim().replace(/;$/, '')); |
| 7 | const auth = (typeof value === 'string' ? JSON.parse(value) : value).auth; |
| 8 | if (!auth?.username || !auth.id || !auth.subsonicSalt || !auth.subsonicToken) { |
| 9 | throw new Error('Music session is unavailable'); |
| 10 | } |
| 11 | if (localStorage.getItem('snow_user') !== auth.username) { |
| 12 | localStorage.clear(); |
| 13 | await new Promise((resolve, reject) => { |
| 14 | const request = indexedDB.deleteDatabase('keyval-store'); |
| 15 | request.onsuccess = resolve; |
| 16 | request.onerror = request.onblocked = reject; |
| 17 | }); |
| 18 | localStorage.setItem('snow_user', auth.username); |
| 19 | } |
| 20 | const id = 'snowglobe'; |
| 21 | const saved = JSON.parse(localStorage.getItem('store_authentication') || 'null'); |
| 22 | const server = { |
| 23 | ...saved?.state?.serverList?.[id], |
| 24 | id, |
| 25 | name: 'Music', |
| 26 | type: 'navidrome', |
| 27 | url: window.location.origin, |
| 28 | username: auth.username, |
| 29 | userId: auth.id, |
| 30 | isAdmin: auth.isAdmin, |
| 31 | credential: `u=${encodeURIComponent(auth.username)}&s=${auth.subsonicSalt}&t=${auth.subsonicToken}`, |
| 32 | ndCredential: 'sso', |
| 33 | }; |
| 34 | const state = saved?.state || {}; |
| 35 | state.currentServer = server; |
| 36 | state.serverList = { [id]: server }; |
| 37 | localStorage.setItem('store_authentication', JSON.stringify({ state, version: 2 })); |
| 38 | if (document.readyState === 'loading') { |
| 39 | await new Promise(resolve => document.addEventListener('DOMContentLoaded', resolve, { once: true })); |
| 40 | } |
| 41 | const original = document.querySelector('script[type="application/x-snow-module"]'); |
| 42 | if (!original) throw new Error('Music application is unavailable'); |
| 43 | const script = document.createElement('script'); |
| 44 | script.type = 'module'; |
| 45 | script.src = original.src; |
| 46 | document.head.appendChild(script); |
| 47 | })(); |