forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk.js
61 lines (53 loc) · 1.6 KB
/
vk.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
var lastTrack = null;
var $r = chrome.extension.sendRequest;
function parseDurationString(timestr) {
if (timestr) {
var m = /(\d+):(\d+)/.exec(timestr);
return parseInt(m[1], 10) * 60 + parseInt(m[2], 10);
}
return 0;
}
function scrobble(e) {
var timestr = '';
if ($("#ac_duration").length > 0) {
timestr = $("#ac_duration").text();
} else if ($("#pd_duration").length > 0) {
timestr = $("#pd_duration").text();
}
if (timestr != '') {
if (timestr[0] == '-') {
timestr = timestr.substring(1);
}
var duration = parseDurationString(timestr);
}
var artist = $("#gp_performer").text();
var title = $("#gp_title").text();
if (lastTrack != artist + " " + title) {
var total = duration;
lastTrack = artist + " " + title;
$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});
}
});
}
}
$(function() {
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
$(document).bind("DOMNodeInserted", function(e) {
if (e.target.id === "gp_performer") {
$("#gp_info>div").bind('DOMSubtreeModified', function(e) { setTimeout(scrobble, 500) });
}
});
});