forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
61.js
138 lines (110 loc) · 3.75 KB
/
61.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
130
131
132
133
134
135
136
137
138
// State for event handlers
var state = 'init';
// Used only to remember last song title
var clipTitle = '';
// Timeout to scrobble track ater minimum time passes
var scrobbleTimeout = null;
// Glabal constant for the song container ....
var CONTAINER_SELECTOR = '#song_panel_title';
$(function(){
$(CONTAINER_SELECTOR).bind('DOMSubtreeModified', function(e) {
// init ----> loading
if (state == 'init' && $(CONTAINER_SELECTOR).length > 0) {
state = 'loading';
return;
}
// loading ---> playing
if (state == 'loading' && $(CONTAINER_SELECTOR).length > 0) {
updateNowPlaying();
state = 'init';
return;
}
});
// first load
updateNowPlaying();
state = 'init';
});
/**
* Called every time we load a new song
*/
function updateNowPlaying(){
var parsedInfo = parseInfo($("title").text());
artist = parsedInfo['artist']; //global
track = parsedInfo['track']; //global
duration = parsedInfo['duration']; //global
if (artist == '' || track == '' || duration == 0) {return;}
chrome.extension.sendRequest({type: 'validate', artist: artist, track: track}, function(response) {
if (response != false){
chrome.extension.sendRequest({type: 'nowPlaying', artist: artist, track: track, duration: duration});
}
});
}
function parseInfo(artistTitle) {
var artist = '';
var track = '';
var duration = 0;
duration = parseDuration(artistTitle);
artistTitle = artistTitle.replace(/\(\d+:\d+\)(.)*?/g , "");
// Figure out where to split; use " - " rather than "-"
if (artistTitle.indexOf(' - ') > -1) {
track = artistTitle.substring(0, artistTitle.indexOf(' - '));
artist = artistTitle.substring(artistTitle.indexOf(' - ') + 3);
} else if (artistTitle.indexOf('-') > -1) {
track = artistTitle.substring(0, artistTitle.indexOf('-'));
artist = artistTitle.substring(artistTitle.indexOf('-') + 1);
} else if (artistTitle.indexOf(':') > -1) {
track = artistTitle.substring(0, artistTitle.indexOf(':'));
artist = artistTitle.substring(artistTitle.indexOf(':') + 1);
} else {
// can't parse
return {artist:'', track:'', duration: 0};
}
artist = artist.replace(/^\s+|\s+$/g,'');
track = track.replace(/^\s+|\s+$/g,'');
return {artist: artist, track: track, duration : duration};
}
function parseDuration(artistTitle){
try{
match = artistTitle.match(/\d+:\d+/g)[0]
mins = match.substring(0, match.indexOf(':'));
seconds = match.substring(match.indexOf(':')+1);
return parseInt(mins*60) + parseInt(seconds);
}catch(err){
return 0;
}
}
/**
* Simply request the scrobbler.js to submit song previusly specified by calling updateNowPlaying()
*/
function scrobbleTrack() {
// stats
chrome.extension.sendRequest({type: 'trackStats', text: 'The sixtyone song scrobbled'});
// scrobble
chrome.extension.sendRequest({type: 'submit'});
}
/**
* Listen for requests from scrobbler.js
*/
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
switch(request.type) {
// called after track has been successfully marked as 'now playing' at the server
case 'nowPlayingOK':
/*
var min_time = (240 < (duration/2)) ? 240 : (duration/2); //The minimum time is 240 seconds or half the track's total length. Duration comes from updateNowPlaying()
// cancel any previous timeout
if (scrobbleTimeout != null)
clearTimeout(scrobbleTimeout);
// set up a new timeout
scrobbleTimeout = setTimeout(function(){setTimeout(scrobbleTrack, 2000);}, min_time*1000);
*/
break;
// not used yet
case 'submitOK':
break;
// not used yet
case 'submitFAIL':
break;
}
}
);