From cd35120d272593ee3208dcac0f67c26b672d74c6 Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Fri, 18 Nov 2016 16:31:59 -0500 Subject: [PATCH 1/7] Add check to prevent double notificaiton on slow album art load --- scripts/protocols/global.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/protocols/global.js b/scripts/protocols/global.js index 3053533..2680856 100644 --- a/scripts/protocols/global.js +++ b/scripts/protocols/global.js @@ -18,6 +18,7 @@ function update() { 'data': music_status }); } + } } @@ -27,6 +28,13 @@ function create_background_msg(oldValue, newValue) { msg.newValue = newValue; if (oldValue !== undefined && (oldValue.title != newValue.title || oldValue.artist != newValue.artist || oldValue.album_art != newValue.album_art)) { + + // hacky check for fake out + if (oldValue.title != newValue.title && oldValue.artist != newValue.artist && + oldValue.album_art == newValue.album_art) { + return null; + } + msg.scrobble = true; if (newValue.title != '') { msg.notify = true; From f95f35e52ed192e1a066272817b97de5d4e3617f Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Fri, 18 Nov 2016 16:37:06 -0500 Subject: [PATCH 2/7] add comment --- scripts/protocols/global.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/protocols/global.js b/scripts/protocols/global.js index 2680856..2b234af 100644 --- a/scripts/protocols/global.js +++ b/scripts/protocols/global.js @@ -18,7 +18,6 @@ function update() { 'data': music_status }); } - } } @@ -29,7 +28,12 @@ function create_background_msg(oldValue, newValue) { if (oldValue !== undefined && (oldValue.title != newValue.title || oldValue.artist != newValue.artist || oldValue.album_art != newValue.album_art)) { - // hacky check for fake out + // There's a bug wwhere 2 notifications are shown sometimes + // because the album artwork takes a while to load. The first + // notification will have the new title and artist, but old artwork. + // + // This fix prevents that notification from being displayed, showing + // the notification only once the new artwork is also fetched. if (oldValue.title != newValue.title && oldValue.artist != newValue.artist && oldValue.album_art == newValue.album_art) { return null; From 124546a3a48d534eb6a91df2c9b592acaf7fa198 Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Fri, 18 Nov 2016 16:37:53 -0500 Subject: [PATCH 3/7] Fix typo --- scripts/protocols/global.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/protocols/global.js b/scripts/protocols/global.js index 2b234af..f1f1ca5 100644 --- a/scripts/protocols/global.js +++ b/scripts/protocols/global.js @@ -28,12 +28,12 @@ function create_background_msg(oldValue, newValue) { if (oldValue !== undefined && (oldValue.title != newValue.title || oldValue.artist != newValue.artist || oldValue.album_art != newValue.album_art)) { - // There's a bug wwhere 2 notifications are shown sometimes + // There's a bug where 2 notifications are shown sometimes // because the album artwork takes a while to load. The first // notification will have the new title and artist, but old artwork. // // This fix prevents that notification from being displayed, showing - // the notification only once the new artwork is also fetched. + // the notification only once the new artwork is also fetched.p if (oldValue.title != newValue.title && oldValue.artist != newValue.artist && oldValue.album_art == newValue.album_art) { return null; From c2411f1fdc9877b73a6487ae53eb0c940c01641d Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Fri, 9 Dec 2016 17:22:01 -0500 Subject: [PATCH 4/7] Get notifications working again --- scripts/background.js | 24 +++++++-------- scripts/controllers.js | 4 ++- scripts/loader.js | 3 +- scripts/protocols/global.js | 2 ++ scripts/protocols/spotify/interface.js | 12 ++++++++ scripts/protocols/spotify/status.js | 42 ++++++++++++++++++-------- 6 files changed, 60 insertions(+), 27 deletions(-) diff --git a/scripts/background.js b/scripts/background.js index 4aafcae..2f4a896 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -6,10 +6,12 @@ var interface_port = null, popup_port = null; var loader_ports = {}; chrome.runtime.onConnect.addListener(function(port) { + if (port.name == "loader") { port.id = port.sender.tab.id; loader_ports[port.id] = port; port.onMessage.addListener(function(msg) { + if (msg.protocol) { chrome.tabs.executeScript(port.id, {file: "scripts/enums.js"}); chrome.tabs.executeScript(port.id, {file: "scripts/protocols/global.js"}); @@ -22,15 +24,18 @@ chrome.runtime.onConnect.addListener(function(port) { loader_ports[port.id] = null; }); } - if (port.name == "interface" && !interface_port) { + if (port.name == "interface" /*&& !interface_port*/) { + interface_port = port; interface_port.id = port.sender.tab.id; port.onMessage.addListener(function(msg) { - if (msg.scrobble == true) { - scrobble(msg.oldValue); - } + + // if (msg.scrobble == true) { + // scrobble(msg.oldValue); + // } if (msg.notify == true) { - create_notification(msg.newValue); + //create_notification(msg.newValue); + notify_helper(msg.newValue, msg.newValue.album_art); now_playing(msg.newValue); } }); @@ -54,7 +59,7 @@ chrome.runtime.onConnect.addListener(function(port) { }); function notify_helper(details, url) { - chrome.notifications.create('', + chrome.notifications.create('100', { type: 'basic', title: details.title, @@ -71,12 +76,7 @@ function notify_helper(details, url) { iconUrl: 'img/notification_ff.png' }] }, function(id){ - chrome.storage.local.get('last_notification', function (data) { - if (data['last_notification']) { - chrome.notifications.clear(data['last_notification'], function(cleared){}); - } - chrome.storage.local.set({'last_notification': id}); - }); + console.log('notified', id); }); } diff --git a/scripts/controllers.js b/scripts/controllers.js index fa3d1cc..cbacb02 100644 --- a/scripts/controllers.js +++ b/scripts/controllers.js @@ -42,7 +42,7 @@ var controller = popupApp.controller('PopupController', ['$scope', function($sco thumb: ThumbEnum.NONE, artist_id: '', album_id: '', - protocol: 'gmusic' + protocol: 'spotify' }; $scope.data = { @@ -403,6 +403,7 @@ var controller = popupApp.controller('PopupController', ['$scope', function($sco $scope.background_port.onMessage.addListener(function(msg) { if (msg.type == 'connect') { + $scope.interface_port = chrome.tabs.connect(msg.id, {name: "popup"}); $scope.interface_port.id = msg.id; $scope.interface_port.onDisconnect.addListener(function() { @@ -492,6 +493,7 @@ var controller = popupApp.controller('PopupController', ['$scope', function($sco } else { $.extend($scope.music_status, response); + if (response.title === '') { $scope.$apply(function() { $scope.set_state(StateEnum.NO_SONG); diff --git a/scripts/loader.js b/scripts/loader.js index 364632b..5619192 100644 --- a/scripts/loader.js +++ b/scripts/loader.js @@ -4,10 +4,11 @@ function load() { gmusic: "play.google.com/music", pandora: "pandora.com", songza: "songza.com", - spotify: "play.spotify.com" + spotify: "open.spotify.com" }; var background_port = chrome.runtime.connect({name: "loader"}); + for (var protocol in urls) { if (urls.hasOwnProperty(protocol) && document.URL.search(urls[protocol]) > 0) { background_port.postMessage({"protocol": protocol}); diff --git a/scripts/protocols/global.js b/scripts/protocols/global.js index f1f1ca5..68ae340 100644 --- a/scripts/protocols/global.js +++ b/scripts/protocols/global.js @@ -9,6 +9,7 @@ function update() { old_status = JSON.parse(JSON.stringify(music_status)); music_status.update(); var msg = create_background_msg(old_status, music_status); + if (msg != null) { background_port.postMessage(msg); } @@ -25,6 +26,7 @@ function create_background_msg(oldValue, newValue) { var msg = {scrobble: false, notify: false}; msg.oldValue = oldValue; msg.newValue = newValue; + if (oldValue !== undefined && (oldValue.title != newValue.title || oldValue.artist != newValue.artist || oldValue.album_art != newValue.album_art)) { diff --git a/scripts/protocols/spotify/interface.js b/scripts/protocols/spotify/interface.js index 3cc2b83..a4ec702 100644 --- a/scripts/protocols/spotify/interface.js +++ b/scripts/protocols/spotify/interface.js @@ -31,6 +31,18 @@ function update_slider(position, slider) { //position is in % function send_command(message) { var iframe = document.querySelector('#app-player').contentDocument; + var track = document.querySelector('div.track-info-name'); + + chrome.notifications.create('42', { + type: 'basic', + title: 'test track', + message: 'name; ' + track, + contextMessage: 'test context', + iconUrl: 'https://chambermaster.blob.core.windows.net/userfiles/UserFiles/chambers/2219/Image/christmas-tree-clip-art-xmas_christmas_tree_5-3333px.png' + }, function(id) { + console.log('id', id); + }); + var button = null; switch (message.type) { case 'play': diff --git a/scripts/protocols/spotify/status.js b/scripts/protocols/spotify/status.js index 6e371c7..c7e8fb4 100644 --- a/scripts/protocols/spotify/status.js +++ b/scripts/protocols/spotify/status.js @@ -23,23 +23,39 @@ var music_status = { }, get_album_art : function () { - var url = getComputedStyle(document.querySelector('#app-player').contentDocument.querySelector('.sp-image-img'))['background-image']; - return url ? url.substring(url.search('http'), url.indexOf(')') - 1) : 'img/default_album.png'; + var artworkDOM = document.querySelector('div.now-playing'); + console.log('artwork', artworkDOM); + var artworkURL = null; + + if (artworkDOM) { + var bgImage = artworkDOM.querySelector('.cover-art-image').style['background-image']; + artworkURL = bgImage.substring(bgImage.search('http'), bgImage.indexOf(')') - 1); + } + + return artworkURL ? artworkURL : 'img/default_album.png'; + }, + + get_play_pause_status: function() { + var pauseButton = document.querySelector('.spoticon-pause-32'); + var playButton = document.querySelector('.spoticon-play-32'); + + return playButton ? StatusEnum.PAUSED : StatusEnum.PLAYING; }, update : function() { - var iframe = document.querySelector('#app-player').contentDocument; - this.title = iframe.querySelector('#track-name > a').innerText; - this.artist = iframe.querySelector('#track-artist > a').innerText; + + var nowPlayingContent = document.querySelector('.now-playing'); + var trackInfo = nowPlayingContent.querySelector('.track-info'); + + this.title = trackInfo.querySelector('.track-info-name > a').innerText; + this.artist = trackInfo.querySelector('.track-info-artists > span > a').innerText; this.album_art = this.get_album_art(); - this.current_time = iframe.querySelector('#track-current').innerText; - this.total_time = iframe.querySelector('#track-length').innerText; - this.current_time_s = this.get_time(this.current_time); - this.total_time_s = this.get_time(this.total_time); - this.repeat = iframe.querySelector('#repeat').classList.contains('active') ? RepeatEnum.ALL : RepeatEnum.NONE; - this.shuffle = iframe.querySelector('#shuffle').classList.contains('active'); - this.status = (iframe.querySelector('#play-pause').classList.contains('playing')) ? StatusEnum.PLAYING : StatusEnum.PAUSED; - this.volume = parseFloat(getComputedStyle(iframe.querySelector('#vol-position')).left, 10) / 108 * 100; + this.protocol = 'spotify'; + this.status = this.get_play_pause_status(); + + var volumeStr = document.querySelector('.volume-bar > div > div > div').style.width; + this.volume = parseInt(volumeStr.substring(0, volumeStr.length - 2)); + return this; } }; From 10eae634060a02726813c5acc9c76d2bc4dd9f14 Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Tue, 13 Dec 2016 17:07:45 -0500 Subject: [PATCH 5/7] Reinject extension if spotify is refreshed --- scripts/background.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/scripts/background.js b/scripts/background.js index 2f4a896..66e07c5 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -5,6 +5,14 @@ chrome.storage.local.set({'last_notification': ''}); var interface_port = null, popup_port = null; var loader_ports = {}; + +// Listen for spotify to open +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo,tab) { + if (tab.url.indexOf("open.spotify.com") > -1 && changeInfo.url === undefined) { + chrome.tabs.executeScript(tabId, {file: "scripts/loader.js"}); + } +}); + chrome.runtime.onConnect.addListener(function(port) { if (port.name == "loader") { From 0f9e4f48259a903688cfafba26c3db71c043b213 Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Tue, 13 Dec 2016 17:12:36 -0500 Subject: [PATCH 6/7] Only display notification if the music is playing --- scripts/protocols/global.js | 6 ++++++ scripts/protocols/spotify/status.js | 1 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/protocols/global.js b/scripts/protocols/global.js index 68ae340..4128bd1 100644 --- a/scripts/protocols/global.js +++ b/scripts/protocols/global.js @@ -45,6 +45,12 @@ function create_background_msg(oldValue, newValue) { if (newValue.title != '') { msg.notify = true; } + + // Do not send if music isn't playing + if (newValue.status == StatusEnum.PAUSED) { + return null; + } + return msg; } else { diff --git a/scripts/protocols/spotify/status.js b/scripts/protocols/spotify/status.js index c7e8fb4..23a24f3 100644 --- a/scripts/protocols/spotify/status.js +++ b/scripts/protocols/spotify/status.js @@ -24,7 +24,6 @@ var music_status = { get_album_art : function () { var artworkDOM = document.querySelector('div.now-playing'); - console.log('artwork', artworkDOM); var artworkURL = null; if (artworkDOM) { From f41bbc5a51cf65f959ca14f2149924a692bfd9c4 Mon Sep 17 00:00:00 2001 From: Michael Caputo Date: Fri, 6 Jan 2017 10:14:56 -0500 Subject: [PATCH 7/7] Update status to properly parse new spotify now playing layout --- scripts/protocols/spotify/status.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/protocols/spotify/status.js b/scripts/protocols/spotify/status.js index 23a24f3..b8350e8 100644 --- a/scripts/protocols/spotify/status.js +++ b/scripts/protocols/spotify/status.js @@ -23,7 +23,7 @@ var music_status = { }, get_album_art : function () { - var artworkDOM = document.querySelector('div.now-playing'); + var artworkDOM = document.querySelector('div.now-playing-bar'); var artworkURL = null; if (artworkDOM) { @@ -43,11 +43,11 @@ var music_status = { update : function() { - var nowPlayingContent = document.querySelector('.now-playing'); + var nowPlayingContent = document.querySelector('.now-playing-bar'); var trackInfo = nowPlayingContent.querySelector('.track-info'); - this.title = trackInfo.querySelector('.track-info-name > a').innerText; - this.artist = trackInfo.querySelector('.track-info-artists > span > a').innerText; + this.title = nowPlayingContent.querySelector('div.react-contextmenu-wrapper > a').innerText; + this.artist = nowPlayingContent.querySelector('span > a').innerText; this.album_art = this.get_album_art(); this.protocol = 'spotify'; this.status = this.get_play_pause_status();