forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.js
71 lines (61 loc) · 2.82 KB
/
spotify.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Chrome-Last.fm-Scrobbler play.spotify.com connector
* @author Damien ALEXANDRE <dalexandre@jolicode.com>
* Heavily inspired by jango.js by Stephen Hamer <stephen.hamer@gmail.com>
*/
(function ChromeLastFmSpotifyScrobbler($) {
"use strict";
/**
* Cache the last song called to avoid multiple calls
* @type {String}
*/
var lastSongTitle = '';
var updateNowPlaying = function() {
var commDiv = document.getElementById('chromeLastFM'), songInfo;
try {
songInfo = JSON.parse(commDiv.innerText);
} catch (e) {
// Skip malformed communication blobs
return;
}
// If this is a "pause" event, just call the "reset"
if (songInfo.pause && songInfo.pause === true) {
lastSongTitle = '';
chrome.extension.sendRequest({type: "reset"});
return;
}
// If the title is the same as the previous one, just leave
if (songInfo.title === lastSongTitle) {
return;
}
lastSongTitle = songInfo.title;
chrome.extension.sendRequest({'type': 'validate', 'artist': songInfo.artist, 'track': songInfo.title}, function (response) {
if (response !== false) {
chrome.extension.sendRequest({type: 'nowPlaying', 'artist': songInfo.artist, 'track': songInfo.title, 'duration': songInfo.duration});
} else {
// on validation failure send nowPlaying 'unknown song'
chrome.extension.sendRequest({'type': 'nowPlaying', 'duration': songInfo.duration});
}
});
};
$(document).ready(function () {
// XXX(shamer): we can't directly access the page javascript from the
// extension. To get code running in the page context we have to add a script tag
// to the DOM. The script then is executed in the global context.
// To communicate between the injected JS and the extension a DOM node is updated with the text to send.
var comNode = $('<div id="chromeLastFM" style="display: none"></div>');
document.body.appendChild(comNode[0]);
$('body').append('<script type="text/javascript">(function(l) {\n' +
" var injectScript = document.createElement('script');\n" +
" injectScript.type = 'text/javascript';\n" +
" injectScript.src = l;\n" +
" document.getElementsByTagName('head')[0].appendChild(injectScript);\n" +
" })('" + chrome.extension.getURL('spotify-dom-inject.js') + "');</script>");
// Listen for 'messages' from the injected script
$('#chromeLastFM').bind('DOMSubtreeModified', updateNowPlaying);
$(window).unload(function () {
chrome.extension.sendRequest({type:'reset'});
return true;
});
});
})(jQuery);