forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmusic.js
49 lines (41 loc) · 1.48 KB
/
zmusic.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
function parseDurationString(timestr) {
if (timestr) {
var m = /(\d\d):(\d\d)/.exec(timestr);
return parseInt(m[1], 10) * 60 + parseInt(m[2], 10);
}
return 0;
}
$(function() {
var lastTrack = null;
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
$(".time").bind('DOMSubtreeModified', function (e) {
var current = parseDurationString($(".time").text());
if (current > 0) {
var m = /(.*)\s+-\s+(.*)\s+\((\d\d:\d\d)\)/.exec($(".track_name").text());
var artist = m[1];
var title = m[2];
var total = parseDurationString(m[3]);
var track = artist + "-" + title;
var $r = chrome.extension.sendRequest;
if (track != lastTrack) {
lastTrack = track;
$r({type: 'validate', artist: artist, track: title}, function(response) {
if (response != false) {
$r({
type: 'nowPlaying',
artist: response.artist,
track: response.track,
duration: total
});
} else {
$r({type: 'nowPlaying', duration: total});
}
});
}
}
});
});