forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugdj-dom-inject.js
55 lines (46 loc) · 1.47 KB
/
plugdj-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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Listen to a "play" event and communicate the current song to the extension
* via a Json string in a DOM node.
*
* @author Anton Stroganov <stroganov.a@gmail.com>
*/
function chromeLastFMUpdateNowPlaying(mediaObject) {
"use strict";
var commDiv = document.getElementById('chromeLastFM'),
message = null;
if(mediaObject != null) {
message = {
'track': mediaObject.title,
'artist': mediaObject.author,
'duration': mediaObject.duration
};
} else {
message = {
'pause': true
}
}
commDiv.innerText = JSON.stringify(message);
}
function chromeLastFMinit() {
"use strict";
var plugdjAPI = window.API;
if (!plugdjAPI) {
// Plug.DJ context is not started yet, poll for it.
setTimeout(function () {
chromeLastFMinit();
}, 1000);
} else {
// subscrive to song change event
plugdjAPI.addEventListener(plugdjAPI.DJ_ADVANCE, function (plugdj_event) {
var mediaObject = plugdj_event == null ? null : plugdj_event.media;
chromeLastFMUpdateNowPlaying(mediaObject);
});
// getMedia() will return your next queued media object if nobody is playing
// so let's make sure that a DJ is performing, then send currently playing
// song info
if(API.getDJs().length > 0) {
chromeLastFMUpdateNowPlaying(API.getMedia());
}
}
}
chromeLastFMinit();