forked from tjhrulz/WebNowPlaying-BrowserExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebNowPlaying.js
636 lines (606 loc) · 15.7 KB
/
WebNowPlaying.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
var currTitle;
var currArtist;
var currAlbum;
var currCover;
var currPos;
var currDur;
var currVolume;
var currRating;
var currRepeat;
var currShuffle;
var currState;
//Always make sure this is set
var currPlayer;
//Only set if the Rainmeter plugin will need to do extra cleanup with an external API
//NOTE: Doing this will require a plugin update and will have to be approved first
var currTrackID;
var currArtistID;
var currAlbumID;
var ws;
var connected = false;
var reconnect;
var sendData;
var outdatedCheck;
var musicEvents;
var musicInfo;
/*
ooooo ooooo oooooooooooo ooooo ooooooooo. oooooooooooo ooooooooo. .oooooo..o
`888' `888' `888' `8 `888' `888 `Y88. `888' `8 `888 `Y88. d8P' `Y8
888 888 888 888 888 .d88' 888 888 .d88' Y88bo.
888ooooo888 888oooo8 888 888ooo88P' 888oooo8 888ooo88P' `"Y8888o.
888 888 888 " 888 888 888 " 888`88b. `"Y88b
888 888 888 o 888 o 888 888 o 888 `88b. oo .d8P
o888o o888o o888ooooood8 o888ooooood8 o888o o888ooooood8 o888o o888o 8""88888P'
*/
function pad(number, length)
{
var str = String(number);
while (str.length < length)
{
str = "0" + str;
}
return str;
}
//Convert seconds to a time string acceptable to Rainmeter
function convertTimeToString(timeInSeconds)
{
var timeInMinutes = parseInt(timeInSeconds / 60);
if (timeInMinutes < 60)
{
return timeInMinutes + ":" + pad(parseInt(timeInSeconds % 60), 2);
}
return parseInt(timeInMinutes / 60) + ":" + pad(parseInt(timeInMinutes % 60), 2) + ":" + pad(parseInt(timeInSeconds % 60), 2);
}
//Convert every words to start with capital (Note: Does NOT ignore words that should not be)
function capitalize(str)
{
str = str.replace(/-/g, ' ');
return str.replace(/\w\S*/g, function(txt)
{
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
/*
.oooooo. oooooooooo. oooo oooooooooooo .oooooo. ooooooooooooo .oooooo..o
d8P' `Y8b `888' `Y8b `888 `888' `8 d8P' `Y8b 8' 888 `8 d8P' `Y8
888 888 888 888 888 888 888 888 Y88bo.
888 888 888oooo888' 888 888oooo8 888 888 `"Y8888o.
888 888 888 `88b 888 888 " 888 888 `"Y88b
`88b d88' 888 .88P 888 888 o `88b ooo 888 oo .d8P
`Y8bood8P' o888bood8P' .o. 88P o888ooooood8 `Y8bood8P' o888o 8""88888P'
`Y888P
*/
//@TODO Maybe add the ability to pass an already made object
//Use this object to define custom event logic
function createNewMusicEventHandler()
{
musicEvents = {};
musicEvents.readyCheck = null;
musicEvents.playpause = null;
musicEvents.next = null;
musicEvents.previous = null;
musicEvents.progress = null;
musicEvents.progressSeconds = null;
musicEvents.volume = null;
musicEvents.repeat = null;
musicEvents.shuffle = null;
musicEvents.toggleThumbsUp = null;
musicEvents.toggleThumbsDown = null;
musicEvents.rating = null;
return musicEvents;
}
//Use this object to define custom logic to retrieve data
function createNewMusicInfo()
{
musicInfo = {};
//Mandatory, just give the player name
musicInfo.player = null;
//Check player is ready to start doing info checks. ie. it is fully loaded and has the song title
//While false no other info checks will be called
musicInfo.readyCheck = null;
musicInfo.state = null;
musicInfo.title = null;
musicInfo.artist = null;
musicInfo.album = null;
musicInfo.cover = null;
musicInfo.duration = null;
musicInfo.position = null;
musicInfo.durationString = null;
musicInfo.positionString = null;
musicInfo.volume = null;
musicInfo.rating = null;
musicInfo.repeat = null;
musicInfo.shuffle = null;
//Optional, only use if more data parsing needed in the Rainmeter plugin
musicInfo.trackID = null;
musicInfo.artistID = null;
musicInfo.albumID = null;
//@TODO Make it possible to define custom update rates?
//@TODO Event based updating?
return musicInfo;
}
/*
ooooo ooo ooooooooo. oooooooooo. .o. ooooooooooooo oooooooooooo ooooooooo.
`888' `8' `888 `Y88. `888' `Y8b .888. 8' 888 `8 `888' `8 `888 `Y88.
888 8 888 .d88' 888 888 .8"888. 888 888 888 .d88'
888 8 888ooo88P' 888 888 .8' `888. 888 888oooo8 888ooo88P'
888 8 888 888 888 .88ooo8888. 888 888 " 888`88b.
`88. .8' 888 888 d88' .8' `888. 888 888 o 888 `88b.
`YbodP' o888o o888bood8P' o88o o8888o o888o o888ooooood8 o888o o888o
*/
function updateInfo()
{
//Try catch for each updater to make sure info is fail safe
//This would be a lot cleaner if javascript had nice things like enums, then I could just foreach this
//UPDATE STATE
if (musicInfo.readyCheck === null || musicInfo.readyCheck())
{
var temp;
try
{
if (musicInfo.state !== null)
{
temp = musicInfo.state();
if (currState !== temp && temp !== null)
{
ws.send("STATE:" + temp);
currState = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating state for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE TITLE
try
{
if (musicInfo.title !== null)
{
temp = musicInfo.title();
if (currTitle !== temp && temp !== null)
{
ws.send("TITLE:" + temp);
currTitle = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating title for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE ARTIST
try
{
if (musicInfo.artist !== null)
{
temp = musicInfo.artist();
if (currArtist !== temp && temp !== null)
{
ws.send("ARTIST:" + temp);
currArtist = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating artist for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE ALBUM
try
{
if (musicInfo.album !== null)
{
temp = musicInfo.album();
if (currAlbum !== temp && temp !== null)
{
ws.send("ALBUM:" + temp);
currAlbum = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating album for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE COVER
try
{
if (musicInfo.cover !== null)
{
temp = musicInfo.cover();
if (currCover !== temp && temp !== null)
{
ws.send("COVER:" + temp);
currCover = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating cover for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE DURATION
try
{
if (musicInfo.durationString !== null)
{
temp = musicInfo.durationString();
if (currDur !== temp && temp !== null)
{
ws.send("DURATION:" + temp);
currDur = temp;
}
}
else if (musicInfo.duration !== null)
{
temp = musicInfo.duration();
if (currDur !== temp && temp !== null && !isNaN(temp))
{
ws.send("DURATION:" + convertTimeToString(temp));
currDur = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating duration for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE POSITION
try
{
if (musicInfo.positionString !== null)
{
temp = musicInfo.positionString();
if (currPos !== temp && temp !== null)
{
ws.send("POSITION:" + temp);
currPos = temp;
}
}
else if (musicInfo.position !== null)
{
temp = musicInfo.position();
if (currPos !== temp && temp !== null && !isNaN(temp))
{
ws.send("POSITION:" + convertTimeToString(temp));
currPos = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating position for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE VOLUME
try
{
if (musicInfo.volume !== null)
{
temp = parseFloat(musicInfo.volume()) * 100;
if (currVolume !== temp && temp !== null && !isNaN(temp))
{
ws.send("VOLUME:" + temp);
currVolume = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating volume for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE RATING
try
{
if (musicInfo.rating !== null)
{
temp = musicInfo.rating();
if (currRating !== temp && temp !== null)
{
ws.send("RATING:" + temp);
currRating = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating rating for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE REPEAT
try
{
if (musicInfo.repeat !== null)
{
temp = musicInfo.repeat();
if (currRepeat !== temp && temp !== null)
{
ws.send("REPEAT:" + temp);
currRepeat = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating repeat for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE SHUFFLE
try
{
if (musicInfo.shuffle !== null)
{
temp = musicInfo.shuffle();
if (currShuffle !== temp && temp !== null)
{
ws.send("SHUFFLE:" + temp);
currShuffle = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating shuffle for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//OPTIONAL ID UPDATERS FOR PLUGIN USE
//UPDATE TRACKID
try
{
if (musicInfo.trackID !== null)
{
temp = musicInfo.trackID();
if (currShuffle !== temp && temp !== null)
{
ws.send("TRACKID:" + temp);
currShuffle = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating trackID for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE ARTISTID
try
{
if (musicInfo.artistID !== null)
{
temp = musicInfo.artistID();
if (currShuffle !== temp && temp !== null)
{
ws.send("ARTISTID:" + temp);
currShuffle = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating artistID for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
//UPDATE ALBUMID
try
{
if (musicInfo.albumID !== null)
{
temp = musicInfo.albumID();
if (currShuffle !== temp && temp !== null)
{
ws.send("ALBUMID:" + temp);
currShuffle = temp;
}
}
}
catch (e)
{
ws.send("Error:Error updating albumID for " + musicInfo.player());
ws.send("ErrorD:" + e);
}
}
else
{
//@TODO Maybe make it so it clears data/disconnects if this is true and not just sets music to stopped
if (currState !== 0)
{
ws.send("STATE:" + 0);
currState = 0;
}
}
}
/*
oooooooooooo oooooo oooo oooooooooooo ooooo ooo ooooooooooooo .oooooo..o
`888' `8 `888. .8' `888' `8 `888b. `8' 8' 888 `8 d8P' `Y8
888 `888. .8' 888 8 `88b. 8 888 Y88bo.
888oooo8 `888. .8' 888oooo8 8 `88b. 8 888 `"Y8888o.
888 " `888.8' 888 " 8 `88b.8 888 `"Y88b
888 o `888' 888 o 8 `888 888 oo .d8P
o888ooooood8 `8' o888ooooood8 o8o `8 o888o 8""88888P'
*/
function fireEvent(event)
{
try
{
if (musicEvents.readyCheck === null || musicEvents.readyCheck())
{
if (event.data.toLowerCase() == "playpause" && musicEvents.playpause !== null)
{
musicEvents.playpause();
}
else if (event.data.toLowerCase() == "next" && musicEvents.next !== null)
{
musicEvents.next();
}
else if (event.data.toLowerCase() == "previous" && musicEvents.previous !== null)
{
musicEvents.previous();
}
else if (event.data.toLowerCase().includes("setprogress ") || event.data.toLowerCase().includes("setposition "))
{
if (musicEvents.progress !== null)
{
var progress = event.data.toLowerCase();
//+9 because "progress " is 9 chars
progress = progress.substring(progress.indexOf("progress ") + 9);
//Goto the : at the end of the command, this command is now a compound command the first half is seconds the second is percent
progress = parseFloat(progress.substring(0, progress.indexOf(":")));
musicEvents.progress(progress);
}
else if (musicEvents.progressSeconds !== null)
{
var position = event.data.toLowerCase();
//+9 because "position " is 9 chars
position = position.substring(position.indexOf("position ") + 9);
//Goto the : at the end of the command, this command is now a compound command the first half is seconds the second is percent
position = parseInt(position.substring(0, position.indexOf(":")));
musicEvents.progressSeconds(position);
}
}
else if (event.data.toLowerCase().includes("setvolume ") && musicEvents.volume !== null)
{
var volume = event.data.toLowerCase();
//+7 because "volume " is 7 chars
volume = parseInt(volume.substring(volume.indexOf("volume ") + 7)) / 100;
musicEvents.volume(volume);
}
else if (event.data.toLowerCase() == "repeat" && musicEvents.repeat !== null)
{
musicEvents.repeat();
}
else if (event.data.toLowerCase() == "shuffle" && musicEvents.shuffle !== null)
{
musicEvents.shuffle();
}
else if (event.data.toLowerCase() == "togglethumbsup" && musicEvents.toggleThumbsUp !== null)
{
musicEvents.toggleThumbsUp();
}
else if (event.data.toLowerCase() == "togglethumbsdown" && musicEvents.toggleThumbsDown !== null)
{
musicEvents.toggleThumbsDown();
}
else if (event.data.toLowerCase() == "rating " && musicEvents.rating !== null)
{
musicEvents.rating();
}
}
}
catch (e)
{
ws.send("Error:Error sending event to " + musicInfo.player);
ws.send("ErrorD:" + e);
throw e;
}
}
/*
.oooooo..o oooooooooooo ooooooooooooo ooooo ooo ooooooooo.
d8P' `Y8 `888' `8 8' 888 `8 `888' `8' `888 `Y88.
Y88bo. 888 888 888 8 888 .d88'
`"Y8888o. 888oooo8 888 888 8 888ooo88P'
`"Y88b 888 " 888 888 8 888
oo .d8P 888 o 888 `88. .8' 888
8""88888P' o888ooooood8 o888o `YbodP' o888o
*/
function init()
{
//@TODO allow custom ports
var url = "ws://127.0.0.1:8974/";
ws = new WebSocket(url);
ws.onopen = function()
{
connected = true;
currPlayer = musicInfo.player();
ws.send("PLAYER:" + currPlayer);
//If this is not cleared in 1000 seconds then assume plugin version is so old it has no version send
outdatedCheck = setTimeout(function()
{
chrome.runtime.sendMessage(
{"method": "flagAsOutdated"}
);
}, 500);
//@TODO Dynamic update rate based on success rate
sendData = setInterval(function()
{
updateInfo();
}, 50);
};
ws.onclose = function()
{
connected = false;
chrome.runtime.sendMessage(
{"method": "flagAsNotConnected"}
);
clearInterval(sendData);
reconnect = setTimeout(function()
{
init();
}, 5000);
};
ws.onmessage = function(event)
{
var versionNumber = event.data.toLowerCase().split(":");
if (versionNumber[0].includes("version"))
{
//Check that version number is the same major version
if (versionNumber[1].split(".")[1] < 4)
{
chrome.runtime.sendMessage(
{"method": "flagAsOutdated"}
);
}
else
{
//Clear timeout set above
clearTimeout(outdatedCheck);
chrome.runtime.sendMessage(
{"method": "flagAsConnected"}
);
}
}
try
{
fireEvent(event);
}
catch (e)
{
ws.send("Error:" + e);
throw e;
}
};
ws.onerror = function(event)
{
if (typeof event.data != 'undefined')
{
console.log("Websocket Error:" + event.data);
}
};
currPlayer = null;
currTitle = null;
currArtist = null;
currAlbum = null;
currCover = null;
currPos = null;
currDur = null;
currVolume = null;
currRating = null;
currRepeat = null;
currShuffle = null;
currState = null;
currTrackID = null;
currArtistID = null;
currAlbumID = null;
}
window.onbeforeunload = function()
{
ws.onclose = function() {}; // disable onclose handler first
ws.close();
};