Repository navigation
Expand file tree
/
Copy pathplayer.js
More file actions
740 lines (655 loc) · 25.3 KB
/
Copy pathplayer.js
File metadata and controls
740 lines (655 loc) · 25.3 KB
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*!
* HTML5/JS web player Differently imported
* https://chrome.google.com/webstore/detail/differently-imported-for/bnihjdccalbcoienhgcjjlilfdhacdkf
*
* phil / @ / pbarton / .co / .uk
* Copyright 2014, Phil Barton
*
This library is free to use, and always will be however the source is the property of the original developer.
You may make any modifications to the source for your own personal use, but you may not release or offer a
modified copy of this software under any terms of any licence. Modified source code must not be made public.
Any modifications that may be seen as useful or an improvement may be sent to the author for review.
Any inclusion will be made with full credit however inclusion is solely at the discretion of the author.
Who knows, enough mods offered and I can throw a plugin library together. Feel free to code Mlkdrop for me! :)
(Fork Milkshake on Github)
Any components listed specifically as being under the terms of a different License will fall under the
terms of that Licence and no other. These components will be clearly commented within the source code along
with any acknowledgements to original authors.
This library is distributed and free to be modified with the ideal that openness and honesty with code
is the key to security and trust, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
More importantly I hope you enjoy using the App.
* Date: 3rd March 2015. 16.33 GMT
*/
var context;
var source;
var analyser;
var audioContext;
var last_response = {};
var apiUrl = "http://api.audioaddict.com/v1/di/track_history";
var diIMG = "<img src='diffI.png' style='border:1px solid white; margin-top:20px; margin-left:15px; height:155px; width:155px;'>";
var playing = false;
$(document).ready(function () { //Set some vars
chrome.commands.onCommand.addListener(function (command) {
if (command == "toggle-play") {
if (!playing) {
playing = true;
server = $.cookie("Diserver");
key = $.cookie("diKeys");
channel = $.cookie("diChan");
vol = $.cookie("diVol");
play(channel, key, vol, server);
pollFlag = 1;
var audio = document.getElementById('diPlyr');
audio.volume = $.cookie("diVol") / 100;
} else {
playing = false;
stop();
}
}
});
setTimeout(function () {
makeTS();
timeEngine();
}, 2000);
capAudio = document.getElementById('diPlyr');
pollFlag = 1;
pollTime = 0;
playing = false;
newSess = true;
var server = "";
var key = "";
var channel = "";
var vol = "";
if (!$.cookie("diVol")) {
$.cookie("diChan", "324_00sclubhits", {
expires: 365
});
$.cookie("diVol", "75", {
expires: 365
});
$.cookie("diChPt", "00's Club Hits", {
expires: 365
});
$.cookie("diChPic", "http://api.audioaddict.com/v1/assets/image/1f2189badb0bb9ccba20e54163afff69.png?size=145x145", {
expires: 365
});
}
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
var xmasfrom = new Date(2015, 11, 19);
var nyfrom = new Date(2015, 11, 31);
var xmasto = new Date(2015, 11, 31);
var nyto = new Date(2016, 0, 2);
var check = new Date(yyyy, mm, dd);
if(check >= xmasfrom && check < xmasto){
motd = "<span>Happy Holidays from Phil @ Differently Imported<br /><a href ='https://www.facebook.com/DifferentlyImported' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Facebook</span></a> / <a href='https://chrome.google.com/webstore/detail/differently-imported-for/bnihjdccalbcoienhgcjjlilfdhacdkf' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Feedback</span></a></span>";
diIMG = "<img src='diffXmas.png' style='border:1px solid white; margin-top:20px; margin-left:15px; height:155px; width:155px;'>";
} else if(check >= nyfrom && check <= nyto){
motd = "<span>Happy New Year from Phil @ Differently Imported<br /><a href ='https://www.facebook.com/DifferentlyImported' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Facebook</span></a> / <a href='https://chrome.google.com/webstore/detail/differently-imported-for/bnihjdccalbcoienhgcjjlilfdhacdkf' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Feedback</span></a></span>";
diIMG = "<img src='diffXmas.png' style='border:1px solid white; margin-top:20px; margin-left:15px; height:155px; width:155px;'>";
}
else{
motd = "<span>Differently Imported. <a href='support.html' target='_blank'><span style='color:#cdcdcd; text-decoration: underline;'>All User Requested features.</span></a> Thanks for your input. V4.3.70 <br><a href ='https://www.facebook.com/DifferentlyImported' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Facebook</span></a> / <a href='https://chrome.google.com/webstore/detail/differently-imported-for/bnihjdccalbcoienhgcjjlilfdhacdkf' target='_blank'> <span style='color:#cdcdcd; text-decoration: underline;'>Feedback</span></a></span>";
}
$.cookie("diChTn", motd, {
expires: 365
});
$.cookie('ctractlen', 0, {'expires': 365});
$.cookie('ctractstart', 0, {'expires': 365});
$.cookie("newT", "1", {
expires: 365
});
chrome.browserAction.setBadgeBackgroundColor({
color: "#cccccc"
});
chrome.browserAction.setBadgeText({
text: " "
});
chrome.runtime.onMessage.addListener(function (action) { //Start the message listener and set some actions
if (action.play == "0") { // Play is a flag to control the player.
playing = false;
stop();
}
if (action.play == "1") {
playing = true;
server = action.server;
key = action.key;
channel = action.channel;
vol = 0.5;
play(channel, key, vol, server);
pollFlag = 1;
var audio = document.getElementById('diPlyr');
audio.volume = action.vol / 100;
}
if (action.play == "2") {
var audio = document.getElementById('diPlyr');
audio.volume = action.vol / 100;
}
if (action.play == "3") {
if (playing) {
showTrack();
}
if (newSess == true) {
newSess = false;
$.cookie("diChTn", motd, {
expires: 365
});
$.cookie('ctractlen', 0, {'expires': 365});
$.cookie('ctractstart', 0, {'expires': 365});
$.cookie("newT", "1", {
expires: 365
});
$.cookie("diHq", "1", {
expires: 365
});
}
}
});
function stop() {
scrollIcon(false); // disable the play badge on the icon
$("#diPlyr").attr("src", "");
$("#diPlyr").remove();
try{
audioContext.close();
}
catch(e){}
if (playing == "false") {
$.cookie("diChTn", motd, {
expires: 365
});
$.cookie('ctractlen', 0, {'expires': 365});
$.cookie('ctractstart', 0, {'expires': 365});
$.cookie("newT", "1", {
expires: 365
});
} else {}
}
function play(ch, ky, vol, server) {
delimitPos = ch.indexOf("_"); //parse the value. need both bits.
chLength = ch.length;
channelId = ch.substring(0, (delimitPos)); // channel id for the trackname
chUrl = ch.substring(delimitPos + 1, chLength); //channel name for the url
if ($.cookie("premium") == "1") {
if ($.cookie("diHq") == "1") {
chUrl = chUrl + "_hi"; // high or low quality stream
}
url = "http://prem" + server + ".di.fm:80/" + chUrl + "?" + ky;
} else {
url = "http://pub" + server + ".di.fm/di_" + chUrl + "_aac?type=.flv";
}
$.cookie("diChId", channelId, {
expires: 365
}); // save em for later
$.cookie("Diserver", server, {
expires: 365
});
if ($('#diPlyr').attr('src') != url) { //if something changed
playing = false;
stop(); // kill the player thats happening now.
pollFlag = 1;
}
vol = vol / 100;
if ($('#diPlyr').length != 1) { // if there is already a player and nothing changed do nothing.
scrollIcon(true); // enable the play badge on the icon
audio = $('<audio />', { // otherwise build a player
controls: 'controls',
id: 'diPlyr',
autoPlay: 'autoplay',
src: url,
volume: vol
});
audio.appendTo('body');
capAudio = document.getElementById('diPlyr');
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
source = audioContext.createMediaElementSource(capAudio);
source.connect(analyser);
analyser.connect(audioContext.destination);
}
playing = true;
showTrack(); // and kick off tracklist API
//document.getElementById('diPlyr').addEventListener("stalled", showErr, false);
document.getElementById('diPlyr').addEventListener("error", showErr, false);
//document.getElementById('diPlyr').addEventListener("emptied", showErr, false);
}
var anim = [". . . . ", "> . . .", ">> . .", ">>> .", ">>>>", ". >>>", ". . >>", ". . . >", ". . . . "];
var ts = 0;
var txTim;
function scrollIcon(flag) {
if (flag) {
if (!txTim) {
txTim = setInterval(function () {
if (playing) {
txtAnim = anim[ts];
ts++;
if (ts >= 8) {
ts = 0;
}
chrome.browserAction.setBadgeText({
text: txtAnim
});
} else {
clearInterval(txTim);
txTim = false;
chrome.browserAction.setBadgeText({
text: " "
});
stop();
}
}, 200);
}
} else {
clearInterval(txTim);
txTim = false;
chrome.browserAction.setBadgeText({
text: " "
});
}
}
//-----------------------------------------------
function showErr(event) {
if (playing == true) {
$.cookie("diChTn", "Network Error! Check Server; Lower Quality; Remove non-premium Keys", {
expires: 365
});
$.cookie("newT", "1", {
expires: 365
});
} else {
$.cookie("diChTn", motd, {
expires: 365
});
$.cookie('ctractlen', 0, {'expires': 365});
$.cookie('ctractstart', 0, {'expires': 365});
$.cookie("newT", "1", {
expires: 365
});
}
}
//-----------------------------------------------
function timeEngine() {
if ($.cookie('lastNP') == '1') {
nowplayLast();
}
var currentD = new Date();
if (($.cookie("diOnTrig") == "1") && (alOn <= currentD.getTime())) {
if (!playing) {
playing = true;
server = $.cookie("Diserver");
key = $.cookie("diKeys");
channel = $.cookie("diChan");
vol = $.cookie("diVol");
play(channel, key, vol, server);
pollFlag = 1;
var audio = document.getElementById('diPlyr');
audio.volume = $.cookie("diVol") / 100;
}
if ($.cookie("dailyPlay") != "1") {
$.cookie("diOnTrig", "0", {
expires: 365
});
} else {
makeTS();
}
}
if (($.cookie("diOffTrig") == '1') && (alOff <= currentD.getTime())) {
stop();
playing = false;
if ($.cookie("dailyStop") != "1") {
$.cookie("diOffTrig", "0", {
expires: 365
});
} else {
makeTS();
}
}
setTimeout(function () {
timeEngine();
}, 1000);
}
});
var alOff;
var alOn;
function makeTS() {
onH = $.cookie("diOnTH");
onM = $.cookie("diOnTM");
offH = $.cookie("diOffTH");
offM = $.cookie("diOffTM");
var currentD = new Date();
alOff = new Date();
alOff.setHours(offH, offM, 00);
if (currentD >= alOff) {
alOff.setDate(alOff.getDate() + 1);
alOff.setHours(offH, offM, 00);
}
alOn = new Date();
alOn.setHours(onH, onM, 00);
if (currentD >= alOn) {
alOn.setDate(alOn.getDate() + 1);
alOn.setHours(onH, onM, 00);
}
alOn = alOn.getTime();
alOff = alOff.getTime();
}
var sleepyTime = -1;
var sleepNumber;
var sleepyTimer;
function setSleep(inSleeps) {
sleepyTime = inSleeps;
sleepNumber = setInterval(function () {
sleepyTime = sleepyTime - 1;
}, 1000);
sleepyTimer = setTimeout(function () {
stopSleep();
playing = false;
stop();
}, sleepyTime * 1000);
}
function stopSleep() {
sleepyTime = -1;
clearInterval(sleepNumber);
clearTimeout(sleepyTimer);
}
var last_fm_key = 'a215d8f01fed30fa10b7fb9c2e82a54d';
var last_url = "http://ws.audioscrobbler.com/2.0/";
var tokenUrl = 'http://ws.audioscrobbler.com/2.0/?method=auth.gettoken&api_key=a215d8f01fed30fa10b7fb9c2e82a54d&format=json';
function get_token() {
$.ajax({
type: "POST",
url: tokenUrl,
data: {},
success: function (res) {
last_response['lt'] = res['token'];
var lastwin = window.open('http://www.last.fm/api/auth/?api_key=a215d8f01fed30fa10b7fb9c2e82a54d&token=' + last_response['lt']);
var last_tmer = setInterval(function () {
if (lastwin.closed) {
clearInterval(last_tmer);
last_get_token(last_response['lt']);
}
}, 1000);
},
dataType: 'json'
});
}
function last_call(method, data, sign) {
last_response[method] = false;
data['api_key'] = last_fm_key;
data['method'] = method;
if (sign) {
post_data = last_sign(data);
} else {
post_data = data;
}
post_data['format'] = 'json';
$.ajax({
type: "post",
url: last_url,
data: post_data,
success: function (res) {
last_response[method] = JSON.stringify(res);
doCallbackJS(JSON.stringify(res), method);
// Do something with response.
},
dataType: 'json'
});
}
//-***************************************--------------------------------
function doCallbackJS(data, method) {
console.log("LC: " + JSON.parse(last_response[method]));
//return last_response[method];
datafornow = JSON.parse(last_response[method]);
if (datafornow["error"] == 9) {
console.log('autherror');
alert('Last.FM has been disconnected from Differently Imported. Please reconnect through \'Settings\'');
$.cookie("lastSKey", '', {
expires: 0
});
$.cookie("lastToken", '', {
expires: 0
});
}
if (method == 'auth.getSession') {
$.cookie("lastSKey", datafornow['session']['key'], {
expires: 365
});
$.cookie("lastToken", last_response['lt'], {
expires: 365
});
$.cookie("lastUsername", datafornow['session']['name'], {
expires: 365
});
}
/*
elif (method== ''){
}
*/
else if (method == 'track.updateNowPlaying') {
console.log('Now Playing Captured');
}
else if (method =='track.love' || method =='track.unlove'){
trackgetInfo(last_response['last_np_artist'], last_response['last_np_track']);
}
else if (method == 'track.getInfo') {
if (typeof datafornow['track']['userloved'] !== 'undefined'){
$.cookie('lastLiked', datafornow['track']['userloved'],{
'expires': 1
});
}
if (last_response['nextFunc'] == 'track.scrobble') {
last_response['nextFunc'] = 0;
params = {
'token': $.cookie("lastToken"),
'artist': datafornow['track']['artist']['name'],
'track': datafornow['track']['name'],
'chosenByUser': '0',
'timestamp': Math.floor((new Date).getTime() / 1000),
'sk': $.cookie("lastSKey")
}
console.log(params);
result = last_call('track.scrobble', params, true);
}
else if (last_response['nextFunc'] == 'track.updateNowPlaying') {
last_response['nextFunc'] = 0;
params = {
'token': $.cookie("lastToken"),
'artist': datafornow['track']['artist']['name'],
'track': datafornow['track']['name'],
'sk': $.cookie("lastSKey")
}
console.log(params);
result = last_call('track.updateNowPlaying', params, true);
}
} else if (method == 'track.scrobble') {
console.log('Scrobbled');
}
}
//-***************************************--------------------------------
function scrobblage() {
/*
artist[i] (Required) : The artist name.
track[i] (Required) : The track name.
timestamp[i] (Required) : The time the track started playing, in UNIX timestamp format (integer number of seconds since 00:00:00, January 1st 1970 UTC). This must be in the UTC time zone.
api_key (Required) : A Last.fm API key.
api_sig (Required) : A Last.fm method signature. See authentication for more information.
sk (Required) : A session key generated
last_response['last_np_artist'] = val.artist;
last_response['last_np_track'] = val.title;
*/
trackgetInfo(last_response['last_np_artist'], last_response['last_np_track']);
last_response['nextFunc'] = 'track.scrobble';
}
function likeage() {
/*
*/
if ($.cookie('lastLiked') == '1'){
thefunk= 'track.unlove';
}
else{
thefunk = 'track.love';
}
params = {
'token': $.cookie("lastToken"),
'artist': last_response['last_np_artist'],
'track': last_response['last_np_track'],
'sk': $.cookie("lastSKey")
}
console.log(params);
result = last_call(thefunk, params, true);
}
var tl;
var ts;
var tp;
var tr;
function get_time(){
time_ping_url ="http://api.audioaddict.com/v1/ping.json" ;
$.get(time_ping_url, function (timeinfo) {
server_time = timeinfo['time'];
server_time = new Date(server_time).getTime() / 1000;
local_time = new Date().getTime() / 1000;
time_offset = (~~local_time - server_time);
$.cookie('servertimeoffset', time_offset, {'expires': 365});
});
}
//var nexttime = (2000 + (Math.round(+new Date()/1000)));
function showTrack() { // tracklist api call. timed with flags to stop server hammerage.
unix = Math.round(+new Date() / 1000);
if (unix >= $.cookie("diPt")) {
console.log('need new track');
pollFlag = 1;
}
/* if (unix <= nexttime){pollflag = 0;}
else {nexttime = (2000 + (Math.round(+new Date()/1000)));} */
if (pollFlag == 1) {
chId = $.cookie("diChId");
get_time();
$.getJSON(apiUrl, function (data) {
$.each(data, function (key, val) {
if (key == chId) {
tl = val.duration;
ts = val.started;
$.cookie('ctractlen', tl, {'expires': 365});
$.cookie('ctractstart', ts, {'expires': 365});
if (last_response['last_np_artist'] != val.artist){
$.cookie('lastLiked', '0', {'expires': 365});
}
tr = "<span title='" + val.track + "'>" + val.track + "</span>";
tp = val.art_url;
last_response['last_np_artist'] = val.artist;
last_response['last_np_track'] = val.title;
pollTime = parseInt(tl) + parseInt(ts);
$.cookie("diPt", pollTime, {
expires: 365
});
if (tp != null) {
tp = stripslashes(tp);
tp = makeHtml(tp);
} else {
tp = makeHtml($.cookie("diChImage"));
}
$.cookie("diChTn", tr, {
expires: 365
});
$.cookie("newT", "1", {
expires: 365
});
$.cookie("diChPic", tp, {
expires: 365
});
}
});
});
pollFlag = 0;
}
}
//-----------------------------------------------
function stripslashes(urlIn) {
urlIn = urlIn.replace("\\", "");
return ("http:" + urlIn);
}
//-----------------------------------------------
function makeHtml(image) {
var html = "<img title='drag and drop this album art into your browser to view full size.' src='" + image + "' style='border:1px solid white; margin-top:20px; margin-left:15px; height:155px; width:155px;'>";
return (html)
}
function trackgetInfo(artist, track) {
/*
artist[i] (Required) : The artist name.
track[i] (Required) : The track name.
utocorrect[0|1] (Op
api_key (Required) : A Last.fm API key.
*/
params = {
'artist': last_response['last_np_artist'],
'track': last_response['last_np_track'],
'autocorrect': 1,
'username': $.cookie('lastUsername')
}
method = 'track.getInfo';
result = last_call(method, params, false);
}
npt = "";
npa = "";
nptimer = false;
function nowplayLast() {
// returns session key.
// artist (Required) : The artist name.
//track (Required) : The track name.//
//api_key (Required) : A Last.fm API key.
//api_sig (Required) : A Last.fm method signature. See authentication for more information.
//sk (Required)
if (playing) {
if (nptimer == false) {
nptimer = setTimeout(function () {
showTrack();
nptimer = false
}, 5000);
}
if (last_response['last_np_artist'] != npa && last_response['last_np_track'] != npt && last_response['last_np_artist'] !== 'undefined') {
npa = last_response['last_np_artist'];
npt = last_response['last_np_track'];
trackgetInfo(last_response['last_np_artist'], last_response['last_np_track']);
last_response['nextFunc'] = 'track.updateNowPlaying';
setTimeout(function(){
scrobblage();
}, 1500);
}
}
}
function last_get_token(token) {
// returns session key.
// api_key: Your 32-character API Key.
// token: The authentication token received at your callback url as a GET variable.
// api_sig: Your 32-character API method signature, as explained in Section 6
var method = 'auth.getSession';
result = last_call(method, {
'token': last_response['lt']
}, true);
}
function last_sign(params) {
ss = "";
st = []
so = {}
Object.keys(params).forEach(function (key) {
st.push(key);
});
st.sort();
st.forEach(function (std) {
ss = ss + std + params[std];
so[std] = params[std];
});
var secret = 'fbfe3fec71f925e72dbeb638c561eab8';
ss = ss + secret;
hashed_sec = unescape(encodeURIComponent($.md5(ss)));
so['api_sig'] = hashed_sec;
return so;
}