forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlemusic.js
139 lines (109 loc) · 3.71 KB
/
googlemusic.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
139
// 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 = '#playerSongInfo';
$(function(){
$(CONTAINER_SELECTOR).live('DOMSubtreeModified', function(e) {
if ($(CONTAINER_SELECTOR).length > 0) {
updateNowPlaying();
return;
}
});
// first load
updateNowPlaying();
});
/**
* Called every time we load a new song
*/
function updateNowPlaying(){
var parsedInfo = parseInfo();
artist = parsedInfo['artist']; //global
track = parsedInfo['track']; //global
duration = parsedInfo['duration']; //global
if (artist == '' || track == '' || duration == 0) {return;}
// check if the same track is being played and we have been called again
// if the same track is being played we return
if (clipTitle == track) {
return;
}
clipTitle = track;
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});
}
// on failure send nowPlaying 'unknown song'
else {
chrome.extension.sendRequest({type: 'nowPlaying', duration: duration});
}
});
}
function parseInfo() {
var artist = '';
var track = '';
var duration = 0;
// Get artist and song names
var artistParent = document.getElementById('playerArtist');
var trackParent = document.getElementById('playerSongTitle');
var durationValue = document.getElementById('duration').innerHTML;
if (null != artistParent) {
artist = artistParent.getElementsByClassName('fade-out-content')[0].getAttribute('title');
}
if (null != trackParent) {
track = trackParent.getElementsByClassName('fade-out-content')[0].getAttribute('title');
}
if (null != durationValue) {
duration = parseDuration(durationValue);
}
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 Google Music 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;
}
}
);