From 03964cd8c8882ca092a724b2920578e284a26fb2 Mon Sep 17 00:00:00 2001 From: Valentin Trimaille Date: Mon, 11 Nov 2019 19:23:01 +0100 Subject: [PATCH 1/3] Limited 'message' event listener to execute only once. Handled response '204 NO CONTENT' from Spotify API --- public/spotify-player.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/public/spotify-player.js b/public/spotify-player.js index 4ccfdfa..1c0453d 100644 --- a/public/spotify-player.js +++ b/public/spotify-player.js @@ -96,6 +96,11 @@ class SpotifyPlayer { const width = 450, height = 730, left = screen.width / 2 - width / 2, top = screen.height / 2 - height / 2; + const eventOptions = { + once: true, + capture: false + }; + window.addEventListener( 'message', event => { @@ -113,7 +118,7 @@ class SpotifyPlayer { } } }, - false + eventOptions ); const w = window.open( @@ -157,6 +162,9 @@ class SpotifyPlayer { // assume an error on Spotify's site console.error('Got error when fetching player', response); return null; + } else if (response.status === 204) { + // No song playing, or account in private mode + return Promise.resolve(null); } else { return response.json(); } From 1b6e490b7ebc7978aa28459ff92c5e970086ab05 Mon Sep 17 00:00:00 2001 From: Valentin Trimaille Date: Mon, 11 Nov 2019 20:14:58 +0100 Subject: [PATCH 2/3] Removed handling of '204 NO CONTENT' response, for pull request. --- public/spotify-player.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/public/spotify-player.js b/public/spotify-player.js index 1c0453d..3b237ba 100644 --- a/public/spotify-player.js +++ b/public/spotify-player.js @@ -162,9 +162,6 @@ class SpotifyPlayer { // assume an error on Spotify's site console.error('Got error when fetching player', response); return null; - } else if (response.status === 204) { - // No song playing, or account in private mode - return Promise.resolve(null); } else { return response.json(); } From b3123920739357b2970844855f4e2ca8003cf4ca Mon Sep 17 00:00:00 2001 From: Valentin Trimaille Date: Mon, 11 Nov 2019 20:43:00 +0100 Subject: [PATCH 3/3] Handled reponse '204 NO CONTENT' from Spotify API. Change in client code necessary as shown in https://codepen.io/montspy/pen/KKKBGpP --- public/spotify-player.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/public/spotify-player.js b/public/spotify-player.js index 4ccfdfa..d1ed4d0 100644 --- a/public/spotify-player.js +++ b/public/spotify-player.js @@ -58,9 +58,7 @@ class SpotifyPlayer { if (!this.obtainingToken) { this.fetchPlayer() .then(data => { - if (data !== null && data.item !== null) { - this.dispatch('update', data); - } + this.dispatch('update', data); }) .catch(e => { console.log('Logging user out due to error', e); @@ -157,6 +155,17 @@ class SpotifyPlayer { // assume an error on Spotify's site console.error('Got error when fetching player', response); return null; + } else if (response.status === 204) { + // No song playing, or account in private mode + return response.json() + .then(json => { + // Account in private mode, json = {} + return Promise.resolve(json); + }) + .catch(err => { + // No song playing, empty response + return Promise.resolve(null); + }); } else { return response.json(); }