forked from web-scrobbler/web-scrobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google+.js
214 lines (168 loc) · 5.96 KB
/
google+.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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 = '#playlist-page';
$(function(){
$(window).unload(function() {
// reset the background scrobbler song data
chrome.extension.sendRequest({type: 'reset'});
return true;
});
$(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();
if (parsedInfo == null)
return;
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;
var actualEl = getActualElement();
if(!actualEl || actualEl.length == 0) {
return null;
}
var children = actualEl.children();
var title = children.eq(1).find('span').text();
var a_t = parseTitle(title);
var artist = a_t.artist;
var track = a_t.track;
var duration = children.eq(2).text();
duration = parseDuration(duration);
return {artist: artist, track: track, duration : duration};
}
function getActualElement() {
var playlist = $('#playlist-page');
var actualEl;
playlist.children().each(function() {
_this = $(this);
var classes = _this.attr('class').split(/\s+/);
if(classes.length > 1) {
actualEl = _this;
return false;
}
});
return actualEl;
}
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;
}
}
/**
* Parse given string into artist and track
* @return {artist, track}
*/
function parseTitle(artistTitle) {
var artist = '';
var track = '';
// Figure out where to split; use " - " rather than "-"
if (artistTitle.indexOf(' - ') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf(' - '));
track = artistTitle.substring(artistTitle.indexOf(' - ') + 3);
} else if (artistTitle.indexOf('-') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf('-'));
track = artistTitle.substring(artistTitle.indexOf('-') + 1);
} else if (artistTitle.indexOf(':') > -1) {
artist = artistTitle.substring(0, artistTitle.indexOf(':'));
track = artistTitle.substring(artistTitle.indexOf(':') + 1);
} else {
// can't parse
return {artist:'', track:''};
}
return cleanArtistTrack(artist, track);
}
function cleanArtistTrack(artist, track) {
// Do some cleanup
artist = artist.replace(/^\s+|\s+$/g,'');
track = track.replace(/^\s+|\s+$/g,'');
// Strip crap
track = track.replace(/\s*\*+\s?\S+\s?\*+$/, ''); // **NEW**
track = track.replace(/\s*\[[^\]]+\]$/, ''); // [whatever]
track = track.replace(/\s*\.(avi|wmv|mpg|mpeg|flv)$/i, ''); // video extensions
track = track.replace(/\s*(of+icial\s*)?(music\s*)?video/i, ''); // (official)? (music)? video
track = track.replace(/\s+\(\s*(HD|HQ)\s*\)$/, ''); // HD (HQ)
track = track.replace(/\s+(HD|HQ)\s*$/, ''); // HD (HQ)
track = track.replace(/\s*video\s*clip/i, ''); // video clip
track = track.replace(/\s+\(?live\)?$/i, ''); // live
track = track.replace(/\(\s*\)/, ''); // Leftovers after e.g. (official video)
track = track.replace(/^(|.*\s)"(.*)"(\s.*|)$/, '$2'); // Artist - The new "Track title" featuring someone
track = track.replace(/^(|.*\s)'(.*)'(\s.*|)$/, '$2'); // 'Track title'
track = track.replace(/^[\/\s,:;~-]+/, ''); // trim starting white chars and dash
track = track.replace(/[\/\s,:;~-]+$/, ''); // trim trailing white chars and dash
return {artist: artist, track: track};
}
/**
* Simply request the scrobbler.js to submit song previusly specified by calling updateNowPlaying()
*/
function scrobbleTrack() {
// stats
chrome.extension.sendRequest({type: 'trackStats', text: 'Google+ YouTube Player 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;
}
}
);