211211 const [ key , value ] = param . split ( '=' ) ;
212212 hashParams [ key ] = decodeURIComponent ( value ) ;
213213 } ) ;
214-
215- // If we got a new token, save it immediately
216214 if ( hashParams . access_token ) {
217- console . log ( 'Got new token from hash:' , hashParams . access_token . substring ( 0 , 10 ) + '...' ) ;
218215 localStorage . setItem ( 'spotify_access_token' , hashParams . access_token ) ;
219216 }
220-
221217 return hashParams ;
222218 }
223219
226222 if ( ! playlistId ) {
227223 throw new Error ( 'Could not extract playlist ID from URL' ) ;
228224 }
229-
230225 const response = await spotifyFetch ( `https://api.spotify.com/v1/playlists/${ playlistId } /tracks` ) ;
231226 if ( ! response ) return [ ] ;
232-
233227 const data = await response . json ( ) ;
234228 return data . items
235229 . filter ( item => item ?. track && item . track . album ?. release_date )
244238 function createQRCard ( track , isQRSide ) {
245239 const card = document . createElement ( 'div' ) ;
246240 card . className = 'card' ;
247-
248241 if ( isQRSide ) {
249242 const qrContainer = document . createElement ( 'div' ) ;
250243 qrContainer . className = 'qr-container' ;
251244 qrContainer . id = `qr-${ track . id } ` ;
252245 card . appendChild ( qrContainer ) ;
253-
254246 new QRCode ( qrContainer , {
255247 text : `https://www.gptgames.dev/tools/spotify_music_quiz_cards.html?track=${ track . id } ` ,
256248 width : 220 ,
262254 } else {
263255 const metadata = document . createElement ( 'div' ) ;
264256 metadata . className = 'metadata' ;
265- metadata . innerHTML = `
266- <h3 class="title">${ track . name } </h3>
267- <p class="artist">${ track . artist } </p>
268- <p class="year">${ track . year } </p>
269- ` ;
257+ metadata . innerHTML = `<h3 class="title">${ track . name } </h3><p class="artist">${ track . artist } </p><p class="year">${ track . year } </p>` ;
270258 card . appendChild ( metadata ) ;
271259 }
272-
273260 return card ;
274261 }
275262
276263 async function createQRCodesFromPlaylist ( ) {
277264 const input = prompt ( "Enter Spotify playlist URL:" ) ;
278265 if ( ! input ) return ;
279-
280266 try {
281267 const tracks = await getPlaylistTracks ( input ) ;
282268 if ( tracks . length === 0 ) {
283269 throw new Error ( 'No valid tracks found in playlist' ) ;
284270 }
285-
286271 const printArea = document . getElementById ( 'printArea' ) ;
287272 printArea . innerHTML = '' ;
288-
289- // Create wrapper divs
290273 const qrPage = document . createElement ( 'div' ) ;
291274 qrPage . className = 'page' ;
292275 const qrContainer = document . createElement ( 'div' ) ;
293276 qrContainer . className = 'card-container' ;
294-
295- // Create QR codes page
296277 const selectedTracks = tracks . slice ( 0 , 21 ) ;
297278 selectedTracks . forEach ( track => {
298279 qrContainer . appendChild ( createQRCard ( track , true ) ) ;
299280 } ) ;
300-
301281 qrPage . appendChild ( qrContainer ) ;
302282 printArea . appendChild ( qrPage ) ;
303-
304- // Create metadata page
305283 const metadataPage = document . createElement ( 'div' ) ;
306284 metadataPage . className = 'page' ;
307285 const metadataContainer = document . createElement ( 'div' ) ;
308286 metadataContainer . className = 'card-container' ;
309-
310287 selectedTracks . forEach ( track => {
311288 metadataContainer . appendChild ( createQRCard ( track , false ) ) ;
312289 } ) ;
313-
314290 metadataPage . appendChild ( metadataContainer ) ;
315291 printArea . appendChild ( metadataPage ) ;
316-
317292 setTimeout ( ( ) => {
318293 window . print ( ) ;
319294 } , 1000 ) ;
320-
321295 } catch ( error ) {
322296 alert ( 'Error processing tracks: ' + error . message ) ;
323297 console . error ( 'Detailed error:' , error ) ;
@@ -328,7 +302,6 @@ <h3 class="title">${track.name}</h3>
328302 document . getElementById ( 'setup' ) . classList . add ( 'hidden' ) ;
329303 document . getElementById ( 'player' ) . classList . remove ( 'hidden' ) ;
330304 localStorage . setItem ( 'spotify_access_token' , token ) ;
331-
332305 playerInstance = new Spotify . Player ( {
333306 name : 'Development Player' ,
334307 getOAuthToken : cb => {
@@ -348,7 +321,6 @@ <h3 class="title">${track.name}</h3>
348321 play : true
349322 } )
350323 } ) ;
351-
352324 const trackId = getUrlParameter ( 'track' ) ;
353325 if ( trackId ) {
354326 await fetch ( `https://api.spotify.com/v1/me/player/play?device_id=${ device_id } ` , {
@@ -364,8 +336,9 @@ <h3 class="title">${track.name}</h3>
364336 }
365337 } ) ;
366338
367- document . getElementById ( 'pauseResumeButton' ) . onclick = togglePauseResume ;
368- document . getElementById ( 'createQRButton' ) . onclick = createQRCodesFromPlaylist ;
339+ playerInstance . addListener ( 'not_ready' , ( { device_id } ) => {
340+ console . error ( 'Device ID has gone offline' , device_id ) ;
341+ } ) ;
369342
370343 playerInstance . addListener ( 'authentication_error' , ( { message } ) => {
371344 console . error ( 'Authentication error:' , message ) ;
@@ -377,10 +350,14 @@ <h3 class="title">${track.name}</h3>
377350 refreshAuth ( ) ;
378351 } ) ;
379352
353+ document . getElementById ( 'pauseResumeButton' ) . onclick = togglePauseResume ;
354+ document . getElementById ( 'createQRButton' ) . onclick = createQRCodesFromPlaylist ;
380355 playerInstance . connect ( ) ;
381356 }
382357
383358 function refreshAuth ( ) {
359+ localStorage . clear ( ) ;
360+ sessionStorage . clear ( ) ;
384361 const clientId = localStorage . getItem ( 'spotify_client_id' ) ;
385362 const scope = 'streaming user-read-email user-read-private user-modify-playback-state playlist-read-private' ;
386363 window . location . href = `https://accounts.spotify.com/authorize?client_id=${ clientId } &response_type=token&redirect_uri=${ encodeURIComponent ( redirectUri ) } &scope=${ encodeURIComponent ( scope ) } ` ;
@@ -394,20 +371,13 @@ <h3 class="title">${track.name}</h3>
394371 ...options . headers
395372 }
396373 } ) ;
397-
398374 if ( response . status === 401 ) {
399- localStorage . clear ( ) ;
400- sessionStorage . clear ( ) ;
401- const clientId = localStorage . getItem ( 'spotify_client_id' ) ;
402- const scope = 'streaming user-read-email user-read-private user-modify-playback-state playlist-read-private' ;
403- window . location . href = `https://accounts.spotify.com/authorize?client_id=${ clientId } &response_type=token&redirect_uri=${ encodeURIComponent ( redirectUri ) } &scope=${ encodeURIComponent ( scope ) } ` ;
375+ refreshAuth ( ) ;
404376 return ;
405377 }
406-
407378 if ( ! response . ok ) {
408379 throw new Error ( `API request failed with status ${ response . status } ` ) ;
409380 }
410-
411381 return response ;
412382 }
413383</ script >
0 commit comments