forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitchfork.js
98 lines (90 loc) · 3.44 KB
/
pitchfork.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
/*
* Chrome-Last.fm-Scrobbler pitchfork.com Connector by vintitres
* (based on MySpace.com Connector by Yasin Okumus)
*/
var trackFrameSel = '#player-modal .detail';
var trackSel = trackFrameSel + ' .hgroup h1';
var artistSel = trackFrameSel + ' .hgroup h2';
var durationSel = "#player-modal .time-unplayed";
var playedSel = "#player-modal .time-played";
$(function () {
// bind page unload function to discard current "now listening"
cancel();
});
var lastTrack = '';
$(document).ready(function () {
// while player not loaded on page monitor all changes
$('body').bind('DOMNodeInserted', function () {
if ($(trackSel).length) {
$('body').unbind('DOMNodeInserted');
// when player loaded monitor track changes
$(trackFrameSel).bind('DOMNodeInserted', function () {
// check if wanted change (2 or 3 new elements inserted into
// trackFrame when new track starts playing, we are
// interested only once)
if (!$(trackSel).length) {
return;
}
trackStr = $(trackSel).text() + $(artistSel).text();
if(trackStr === lastTrack) {
return;
}
lastTrack = trackStr;
// on new track wait until it starts playing to get duration if
// available
$(playedSel).bind('DOMSubtreeModified', function () {
$(playedSel).unbind('DOMSubtreeModified');
var
artist = $(artistSel).text(),
track = $(trackSel).text().replace(/"|\[|\]/g, '');
chrome.extension.sendRequest(
{
type: 'validate',
artist: artist,
track: track
},
function (response) {
if (response !== false) {
var duration = parseDuration(
$(durationSel).text()
);
if (duration === 0) {
// some tracks don't have information about
// duration, so we use hardcore AI to
// determine it
duration = 120;
}
chrome.extension.sendRequest({
type: 'nowPlaying',
artist: artist,
track: track,
duration: duration
});
}
}
);
});
});
}
});
});
function parseDuration(match) {
if (match === '--:--') {
return 0;
}
try {
var
mins = match.substring(1, match.indexOf(':')),
seconds = match.substring(match.indexOf(':') + 1);
return parseInt(mins, 10) * 60 + parseInt(seconds, 10);
} catch (err) {
return 0;
}
}
function cancel() {
$(window).unload(function () {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
}