forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newmyspace.js
50 lines (45 loc) · 1.43 KB
/
newmyspace.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
/*
* Chrome-Last.fm-Scrobbler new.myspace.com Connector by Ed Rackham
* http://edrackham.com
*/
var durationPart = "#nowPlaying .time";
var totalPart = "#nowPlaying .duration";
var trackPart = ".track .title a";
var artistPart = ".track .artist a";
var lastTrack = null;
$(function(){
cancel();
});
$(durationPart).bind('DOMSubtreeModified',function(e){
var duration = parseDuration($(durationPart).text()+$(totalPart).text());
if(duration.current > 0){
var artist = $(artistPart).text();
var track = $(trackPart).text();
if (lastTrack != track){
lastTrack = track;
console.log("MySpaceScrobbler: scrobbling '" + track + "' by " + artist);
chrome.extension.sendRequest({type: 'validate', artist: artist, track: track}, function(response) {
if (response != false){
chrome.extension.sendRequest({type: 'nowPlaying', artist: response.artist, track: response.track, duration: duration.total});
}else{
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration.total});
}
});
}
}
});
var durationRegex = /[ \n]*(\d+):(\d+)[ \n]*\/[ \n]*(\d+):(\d+)[ \n]*/;
function parseDuration(match){
try{
var m = durationRegex.exec(match);
return {current: parseInt(m[1],10)*60 + parseInt(m[2],10), total: parseInt(m[3],10)*60 + parseInt(m[4],10)};
}catch(err){
return 0;
}
}
function cancel(){
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
}