forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indieshuffle.js
88 lines (67 loc) · 2.02 KB
/
indieshuffle.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
/*
* Chrome-Last.fm-Scrobbler indieshuffle.com Connector by tiii
* v1.0, 09-05-2012
*/
var watchedContainer = "#now-playing";
var wait = false;
var song = {};
var extractDuration = function(context) {
var duration = 0;
var time = $('#time #jplayer_total_time').text().split(':');
// if there are ever songs that take longer than
// 1 day to play i'll be happy to update this line
$.each(time, function(index) {
var t = parseInt(time[index],10);
if(index != time.length-1)
duration += 60*t;
else
duration += t;
});
return duration;
};
var extractTitle = function(context) {
var trackContainer = $('#now-playing-title a', context);
var artist = $('strong',trackContainer).text();
var track = trackContainer.text().substr(artist.length);
return {
artist: artist,
track: track
};
};
var updateNowPlaying = function() {
var context = watchedContainer;
var current = extractTitle(context);
if((!song.artist && !song.track) ||
song.artist !== current.artist ||
song.track !== current.track) {
song = current;
}
if(!song.duration || song.duration === 0) {
song.duration = extractDuration(context);
} else {
if(!song.submitted) {
song.submitted = true;
chrome.extension.sendRequest({type: 'validate', artist: song.artist, track: song.track}, function(response) {
if (response !== false) { // autocorrected song object
chrome.extension.sendRequest({type: 'nowPlaying', artist: response.artist, track: response.track, duration: song.duration});
} else {
chrome.extension.sendRequest({type: 'nowPlaying', duration: song.duration});
}
});
}
}
// reset wait
wait = false;
};
$(function(){
$(watchedContainer).live('DOMSubtreeModified', function(e) {
if(!wait) {
wait = true;
setTimeout(updateNowPlaying, 1000);
}
});
$(window).unload(function() {
chrome.extension.sendRequest({type: 'reset'});
return true;
});
});