diff --git a/lib/index.html b/lib/index.html index 37c8fa5..518a8f8 100644 --- a/lib/index.html +++ b/lib/index.html @@ -6,7 +6,7 @@ -
Retrieving album data...
+
Waiting for Spotify...
\ No newline at end of file diff --git a/lib/index.js b/lib/index.js index aa4c949..b0328f7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,10 +1,14 @@ -require('ipc').on('coverUrl', function(url) { +var ipc = require('ipc'); +ipc.on('coverUrl', function(url) { document.getElementById('background').src = url; document.getElementById('cover').src = url; }); +ipc.on('loadingText', function(text) { + document.getElementById('loading').innerHTML = text; +}); document.onkeydown = function(e) { e = e || window.event; if (e.keyCode == 27) { require('remote').getCurrentWindow().close(); } -}; +}; \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 04e58c4..e5981bf 100644 --- a/lib/main.js +++ b/lib/main.js @@ -3,7 +3,6 @@ const electron = require('electron'); // Module to control application life. const app = electron.app; -const ipc = require('ipc'); // Module to create native browser window. const BrowserWindow = electron.BrowserWindow; @@ -53,6 +52,9 @@ app.on('activate', function () { }); process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; +process.on('uncaughtException', function (error) { + console.log(error); +}); const https = require('https'); @@ -68,6 +70,8 @@ const DEFAULT_HTTPS_CONFIG = { let config; let version; +version = {}; +version.running = false; let csrf; let oauth; let albumId; @@ -141,28 +145,49 @@ function getAlbumCover(id) { }); } -config = copyConfig(); -config.host = generateLocalHostname(); -config.path = '/service/version.json?service=remote'; -getJson(config, function(data) { version = data; }); -config.host = generateLocalHostname(); -config.path = '/simplecsrf/token.json'; -getJson(config, function(data) { csrf = data.token; }); -config.host = 'open.spotify.com'; -config.path = '/token'; -config.port = 443; -getJson(config, function(data) { oauth = data.t; }); -let updateTrackCover; -let waitForRequest = setInterval(function() { - if (typeof version !== 'undefined' && typeof csrf !== 'undefined' && typeof oauth !== 'undefined') { - clearInterval(waitForRequest); - console.log('done.'); - console.log(version); - console.log(csrf); - console.log(oauth); - updateTrackCover = setInterval(getCurrentAlbumId, UPDATE_INTERVAL); +function grabTokens() { + if (mainWindow !== null) { + mainWindow.webContents.send('loadingText', 'Connecting to Spotify...'); + } + config.host = generateLocalHostname(); + config.path = '/simplecsrf/token.json'; + getJson(config, function(data) { csrf = data.token; }); + config.host = 'open.spotify.com'; + config.path = '/token'; + config.port = 443; + getJson(config, function(data) { oauth = data.t; }); + let updateTrackCover; + let waitForRequest = setInterval(function() { + if (typeof version !== 'undefined' && typeof csrf !== 'undefined' && typeof oauth !== 'undefined') { + clearInterval(waitForRequest); + console.log('done.'); + console.log(version); + console.log(csrf); + console.log(oauth); + updateTrackCover = setInterval(getCurrentAlbumId, UPDATE_INTERVAL); + } + else { + console.log('waiting for authentication...'); + } + }, 500); +} + +let waitForSpotify = setInterval(function() { + if (typeof version !== 'undefined' && version.running) { + clearInterval(waitForSpotify); + grabTokens(); } else { - console.log('waiting...'); + config = copyConfig(); + config.host = generateLocalHostname(); + config.path = '/service/version.json?service=remote'; + getJson(config, function(data) { + if (!('running' in data)) { + data.running = true; + } + version = data; + console.log(version); + }); + console.log('waiting for spotify...'); } }, 500);