forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify-dom-inject.js
39 lines (36 loc) · 1.31 KB
/
spotify-dom-inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Listen to a "play" event and communicate the current song to the extension
* via a Json string in a DOM node.
*
* @author Damien ALEXANDRE <dalexandre@jolicode.com>
*/
function chromeLastFMUpdateNowPlaying() {
"use strict";
var spotify_context = window.context;
if (!spotify_context) {
// Spotify context is not started yet, poll for it.
setTimeout(function () {
chromeLastFMUpdateNowPlaying();
}, 1000);
} else {
spotify_context.addEventListener("play", function () {
var context = this;
if (context._currentTrack && !context._currentTrack.advertisement) {
var songInfo = {
'title': context._currentTrack.name,
'artist': context._currentTrack.artists[0].name,
'duration': (context._currentTrack.duration / 1000)
};
var commDiv = document.getElementById('chromeLastFM');
commDiv.innerText = JSON.stringify(songInfo);
}
});
spotify_context.addEventListener("pause", function () {
var commDiv = document.getElementById('chromeLastFM');
commDiv.innerText = JSON.stringify({
'pause': true
});
});
}
}
chromeLastFMUpdateNowPlaying();