-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrobbler.js
348 lines (308 loc) · 11.9 KB
/
scrobbler.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// browser tab with actually scrobbled track
var nowPlayingTab = null;
// song structure, filled in nowPlaying phase, (artist, track, album, duration, startTime)
var song = {};
var scrobbleTimeout = null;
/**
* Used to track delayed event processing.
*/
var timeouts = [];
/**
* This is never disabled.
*/
var disabled = false;
/**
* Default settings & update notification
*/
{
// use notifications by default
if (localStorage.useNotifications == null)
localStorage.useNotifications = 1;
// now playing notifications
if (localStorage.useNotificationsNowPlaying == null)
localStorage.useNotificationsNowPlaying = 1;
// scrobbled notifications
if (localStorage.useNotificationsScrobbled == null)
localStorage.useNotificationsScrobbled = 1;
// no disabled connectors by default
if (localStorage.disabledConnectors == null)
localStorage.disabledConnectors = JSON.stringify([]);
// hide notifications by default
if (localStorage.autoHideNotifications == null)
localStorage.autoHideNotifications = 1;
// show update popup - based on different version
if (localStorage.appVersion != chrome.app.getDetails().version) {
localStorage.appVersion = chrome.app.getDetails().version;
// introduce new options if not already set
if (typeof localStorage.useAutocorrect == 'undefined')
localStorage.useAutocorrect = 0;
}
/**
* Automatically pop-up to register on first install.
*/
if (!localStorage["firstPopup"]) {
localStorage["firstPopup"] = true;
}
}
function reset() {
console.log('reset called');
if (scrobbleTimeout != null) {
clearTimeout(scrobbleTimeout);
scrobbleTimeout = null;
}
nowPlayingTab = null;
}
/**
* Sets up page action icon, including title and popup
*
* @param {integer} action one of the ACTION_ constants
* @param {integer} tabId
*/
function setActionIcon(action, tabId) {
var tab = tabId ? tabId : nowPlayingTab;
chrome.pageAction.hide(tab);
switch (action) {
case ACTION_UNKNOWN:
chrome.pageAction.setIcon({tabId: tab, path: ICON_UNKNOWN});
chrome.pageAction.setTitle({tabId: tab, title: 'Song not recognized. Click the icon to correct its title'});
chrome.pageAction.setPopup({tabId: tab, popup: 'popup.html'});
break;
case ACTION_NOWPLAYING:
chrome.pageAction.setIcon({tabId: tab, path: ICON_NOTE});
chrome.pageAction.setTitle({
tabId: tab,
title: 'Now playing: ' + song.artist + ' - ' + song.track + '\nClick to disable scrobbling'
});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_SCROBBLED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_TICK});
chrome.pageAction.setTitle({tabId: tab, title: 'Song has been scrobbled\nClick to disable scrobbling'});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_DISABLED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_NOTE_DISABLED});
chrome.pageAction.setTitle({tabId: tab, title: 'Scrobbling is disabled\nClick to enable'});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_REENABLED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_TICK_DISABLED});
chrome.pageAction.setTitle({tabId: tab, title: 'Scrobbling will continue for the next song'});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_CONN_DISABLED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_CONN_DISABLED});
chrome.pageAction.setTitle({
tabId: tab,
title: 'Scrobbling for this site is disabled, most likely because the site has changed its layout. Please contact the connector author.'
});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_SITE_RECOGNIZED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_LOGO});
chrome.pageAction.setTitle({tabId: tab, title: 'This site is supported for scrobbling'});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
case ACTION_SITE_DISABLED:
chrome.pageAction.setIcon({tabId: tab, path: ICON_LOGO});
chrome.pageAction.setTitle({tabId: tab, title: 'This site is supported, but you disabled it'});
chrome.pageAction.setPopup({tabId: tab, popup: ''});
break;
}
chrome.pageAction.show(tab);
}
/**
* Shows (or not) the notification, based on user settings
* Use 'force' to override settings and always show the notification (e.g. for errors)
*/
function scrobblerNotification(text, force) {
if (localStorage.useNotifications != 1 && !force)
return;
if (typeof(webkitNotifications) === "undefined")
return;
var title = 's';
var body = '';
var boom = text.split(NOTIFICATION_SEPARATOR);
if (boom.length == 1)
body = boom[0];
else {
title = boom[0];
body = boom[1];
}
var notification = webkitNotifications.createNotification(
'icon128.png',
title,
body
);
notification.show();
if (localStorage.autoHideNotifications == 1)
setTimeout(function () {
notification.cancel()
}, NOTIFICATION_TIMEOUT);
}
/**
* Shows an error notification (use this rather than alerts)
*/
function errorNotification(text) {
// Opera compatibility
if (typeof(webkitNotifications) === "undefined")
return;
var title = 'Scrobble Error';
var notification = webkitNotifications.createNotification(
'icon128.png',
title,
text
);
notification.show();
if (localStorage.autoHideNotifications == 1)
setTimeout(function () {
notification.cancel()
}, NOTIFICATION_TIMEOUT);
}
/**
* Called when NowPlaying has changed due to some event.
*/
function nowPlaying(request) {
/**
* Song is already playing!
*/
if (song && song.artist === request.artist &&
song.track === request.track) {
return true;
}
song = {
artist: request.artist,
track: request.track,
currentTime: request.currentTime,
duration: request.duration,
source: request.source,
startTime: ( parseInt(new Date().getTime() / 1000.0) - request.currentTime) // in seconds
}
if (typeof(request.album) != 'undefined') {
song.album = request.album;
}
/**
* Added to populate the tweet
*/
localStorage["song"] = song.track;
localStorage["artist"] = song.artist;
return false;
}
function isConnectorEnabled(player) {
var disabledArray = JSON.parse(localStorage["disabledConnectors"]);
var i = disabledArray.indexOf(player)
return (i == -1);
}
function updateFrontendDisplay() {
// song var now has: artist, track, source
}
/**
* Handling messages from the detector scripts
*/
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
switch (request.type) {
// Called when a new song has started playing. If the artist/track is filled,
// they have to be already validated! Otherwise they can be corrected from the popup.
// Also sets up a timout to trigger the scrobbling procedure (when all data are valid)
case "nowPlaying":
console.log("nowPlaying " + JSON.stringify(request));
// do the reset to be sure there is no other timer running
reset();
// remember the caller
nowPlayingTab = sender.tab ? sender.tab.id : -1;
// backward compatibility for connectors which dont use currentTime
if (typeof(request.currentTime) == 'undefined' || !request.currentTime)
request.currentTime = 0;
// data missing, save only startTime and show the unknown icon
if (typeof(request.artist) == 'undefined' || !request.artist
|| typeof(request.track) == 'undefined' || !request.track) {
// fill only the startTime, so the popup knows how to set up the timer
song = {
startTime: parseInt(new Date().getTime() / 1000.0) // in seconds
};
// if we know something...
if (typeof(request.artist) != 'undefined' && request.artist) {
song.artist = request.artist;
}
if (typeof(request.track) != 'undefined' && request.track) {
song.track = request.track;
}
if (typeof(request.currentTime) != 'undefined' && request.currentTime) {
song.currentTime = request.currentTime;
}
if (typeof(request.duration) != 'undefined' && request.duration) {
song.duration = request.duration;
}
if (typeof(request.album) != 'undefined' && request.album) {
song.album = request.album;
}
}
else {
var alreadyNowPlaying = nowPlaying(request);
console.log(localStorage["disabledConnectors"]);
if (isConnectorEnabled(request.tag) && !alreadyNowPlaying) {
updateFrontendDisplay();
}
else if (alreadyNowPlaying) {
console.log("Now Playing has not changed. Not scheduling push.");
}
else {
console.log("Tune will not be pushed since connector is disabled.");
}
}
sendResponse({});
break;
// called when the window closes / unloads before the song can be scrobbled
case "reset":
reset();
sendResponse({});
break;
case "trackStats":
_gaq.push(['_trackEvent', request.text]);
sendResponse({});
break;
// returns the options in key => value pseudomap
case "getOptions":
var opts = {};
for (var x in localStorage)
opts[x] = localStorage[x];
sendResponse({value: opts});
break;
// do we need this anymore? (content script can use ajax)
case "xhr":
var http_request = new XMLHttpRequest();
http_request.open("GET", request.url, true);
http_request.onreadystatechange = function () {
if (http_request.readyState == 4 && http_request.status == 200)
sendResponse({text: http_request.responseText});
};
http_request.send(null);
break;
// for login
case "newSession":
sessionID = "";
break;
// connector tells us it is disabled
case "reportDisabled":
/**
* @TODO - Does this apply to us?
*/
break;
/**
* Connector will not submit this to NowPlaying unless we validate.
* We want at least an artist and track
*/
case "validate":
if (!request.artist || !request.track) {
sendResponse(false);
break;
}
sendResponse(request);
break;
default:
console.log('Unknown request: %s', $.dump(request));
}
return true;
}
);