From 35c57dcc8dac10f168fee33cff5d9c5dbaf82109 Mon Sep 17 00:00:00 2001 From: awex 3 Date: Sat, 30 Apr 2022 09:12:00 -0400 Subject: [PATCH] Add auto refresh for spotify widget --- static/js/spotifyWidget.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/static/js/spotifyWidget.js b/static/js/spotifyWidget.js index 628a7ea..a794f32 100644 --- a/static/js/spotifyWidget.js +++ b/static/js/spotifyWidget.js @@ -2,9 +2,14 @@ async function getState() { const data = await fetch("https://api.thatalex.dev/v0/web/spotify/player/state", { method: "GET" }).then(response => response.json()) - .catch(function(response) { - console.log(`Uh oh, a little fucky wucky owo occurred\n\n${response}`) - document.getElementById("songContent").innerHTML = `

Uh oh! There was an error fetching song content.
The API may be temporarily down.

` + .catch(async function(response) { + const d = await response.json(); + if (response.status == 401 && d.message == "Access Token was invalid. Token was refreshed & next request should succeed.") { + location.reload(); + } else if (response.status != 200 || response.status != 204) { + console.log(`Uh oh, a little fucky wucky owo occurred\n\n${d}`) + document.getElementById("songContent").innerHTML = `

Uh oh! There was an error fetching song content.
The API may be temporarily down.

` + } }); if(data.playing) document.getElementById("songContent").innerHTML = `

${data.data.songData.songName} by ${data.data.songData.artists[0].name} at ${data.data.device.volume}% volume.`