forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deezer.js
129 lines (113 loc) · 3.32 KB
/
deezer.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Chrome-Last.fm-Scrobbler Deezer.com Connector by @damienalexandre
*
* v1.0, 5 march 2012
*
* The difficulty here is that the song duration can appear a long time after the
* song starts playing.
* We use the title change to know when a song is played.
*
* @todo Handle the song "pause"? (do we have to cancel the scrobble?)
* @todo Improve the way we deal with the first played song. I have made it lazy for perf purpose.
*/
var currentDeezerTimeout = null;
$(document).ready(function() {
$("title").bind('DOMSubtreeModified', function()
{
cancel(); // In case we switch song before the scrobble (as the duration is async, we may warn the extension too late)
if (currentDeezerTimeout) // Handle song fast zapping
{
window.clearTimeout(currentDeezerTimeout);
}
currentDeezerTimeout = window.setTimeout(sendTrack, 1000); // As the duration may be not available.
});
sendTrack(); // We maybe have a song playing right away. There is no retry if this call fails.
$(window).unload(function()
{
cancel();
return true;
});
});
/**
* Handle the chrome.extension
* and can retry itself too.
*/
function sendTrack()
{
if (currentDeezerTimeout)
{
window.clearTimeout(currentDeezerTimeout);
}
var deezerSong = getCurrentTrack();
if (deezerSong && deezerSong.duration > 0)
{
chrome.extension.sendRequest({type: 'validate', artist: deezerSong.artist, track: deezerSong.track}, function(response)
{
if (response != false)
{
var song = response; // contains valid artist/track now
chrome.extension.sendRequest({type: 'nowPlaying', artist: song.artist, track: song.track, duration: deezerSong.duration});
}
else
{
chrome.extension.sendRequest({type: 'nowPlaying', duration: deezerSong.duration});
displayMsg('Not recognized');
}
});
}
else if (currentDeezerTimeout)
{
// Retry to fetch the song infos later
currentDeezerTimeout = window.setTimeout(sendTrack, 1000);
}
}
/**
* Try to get the song infos (track, artist, duration)
* @return object|boolean
*/
function getCurrentTrack()
{
if ($('#h_play').is(":hidden")) { // Play button hidden, the song is playing
return {
track: $('#current-track').html(),
artist: $('#current-artist').html(),
duration: parseDuration($('#end-track').html())
}
}
return false;
}
/**
* Binded on the unload
*/
function cancel()
{
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
}
/**
* Maybe this kind of common method should be in the Core
*
* @param durationString Like "13;37".
* @return int The duration string translated to seconds
*/
function parseDuration(durationString)
{
try
{
var match = durationString.match(/\d+:\d+/g);
if (match)
{
var mins = match[0].substring(0, match[0].indexOf(':'));
var seconds = match[0].substring(match[0].indexOf(':')+1);
return parseInt(mins*60, 10) + parseInt(seconds, 10);
}
else
{
return 0;
}
}
catch(err)
{
return 0;
}
}