This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxscript.js
767 lines (701 loc) · 30 KB
/
xscript.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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
The MIT License (MIT)
Copyright (c) 2015 Phanta
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Init
if (initialized) {
plainMsg("xScript already loaded! Reloading script.");
reloadScript();
}
else {
// Constants
var IS_BETA = true;
var ScriptUrls = {
SCRIPT_URL: 'https://cdn.rawgit.com/phantamanta44/xScript/master/xscript.js',
STYLESHEET_URL: 'https://cdn.rawgit.com/phantamanta44/xScript/master/xscript.css',
BADGE_URL: 'https://cdn.rawgit.com/phantamanta44/xScript/master/bdg_large.png'
};
var BetaUrls = {
SCRIPT_URL: 'https://dl.dropboxusercontent.com/u/75796840/xScript/xscript.js',
STYLESHEET_URL: 'https://dl.dropboxusercontent.com/u/75796840/xScript/xscript.css',
BADGE_URL: 'https://dl.dropboxusercontent.com/u/75796840/xScript/bdg_large.png'
};
var URL_REGEX = new RegExp('(https?://\\S*\\.\\S{2,4}(/\\S{0,})?)');
var IMG_REGEX = new RegExp('https?://\\S*/\\S*\\.(png|jpeg|jpg|gif|tiff|tif|svg|bmp)');
var INLINE_IMG_REGEX = new RegExp('>(https?://\\S*/\\S*\\.(png|jpeg|jpg|gif|tiff|tif|svg|bmp))<');
var NOTIFICATION_SND = new Audio('https://dl.dropboxusercontent.com/u/75796840/xScript/notify.wav');
var MSG_COLOR = '#2196F3';
// Enums
var PlugRole = {
NORMAL: 0,
RDJ: 1,
BOUNCER: 2,
MANAGER: 3,
COHOST: 4,
HOST: 5
};
var Mod = {
AUTOJOIN: 0,
AUTOWOOT: 1,
AUTOMEH: 2,
JOINALERT: 3,
WOOTALERT: 4,
IMPENDALERT: 5,
DURALERT: 6,
CHATALERT: 7,
TWITCHEMOTES: 8,
TRACKSYN: 9
};
var ItemDisplay = {
TRUE: 'block',
FALSE: 'none'
};
var UrlType = {
SCRIPT: 0,
STYLESHEET: 1,
BADGE: 2
};
var TwitchEmotes = {};
// Data
var isGrab = false;
// Dynamic vars
var initialized = false;
var pastName;
var theUser;
var userTag;
var initialBg;
var waitListPos = -1;
// HTML objects
var XS = {
chatSettingsBtn: undefined,
chatSettingsDiv: undefined,
trackSynBtn: undefined,
twitchEmoteBtn: undefined,
joinAlertBtn: undefined,
wootAlertBtn: undefined,
impendAlertBtn: undefined,
durAlertBtn: undefined,
chatAlertBtn: undefined,
settingsDiv: undefined,
settingsBtn: undefined,
autoJoinBtn: undefined,
autoWootBtn: undefined,
autoMehBtn: undefined
};
// Plug objects
var Plug = {
playbackCont: $(document.getElementById('playback-container')),
chatBox: $(document.getElementById('chat-messages')),
wootBtn: $(document.getElementById('woot')),
mehBtn: $(document.getElementById('meh')),
volSlider: $(document.getElementById('volume').childNodes[4]),
volBtn: $(document.getElementById('volume').childNodes[0]),
chatDiv: $(document.getElementById('chat')),
chatHead: $(document.getElementById('chat-header')),
appCont: $(document.getElementById('app'))
};
/*
* Initialize the script
*/
var init = function() {
if (!!getCookie('xscriptsettings'))
settings = JSON.parse(getCookie('xscriptsettings'));
theUser = API.getUser();
userTag = tag(theUser.username);
registerListeners();
injMods();
initialized = true;
onInit();
logMsg('xScript initialized. Use "/xshelp" to see help.');
if (document.location.pathname === '/friendshipismagic/' || document.location.pathname === '/friendshipismagic') {
if (!document.getElementById('bpm-resources')) {
plainMsg('Attempting to load BetterPonymotes...');
$.getScript('https://cdn.p0ne.com/scripts/plug_p0ne.bpm.js');
}
else
plainMsg('Ponymotes already loaded.');
}
$.get('https://twitchemotes.com/api_cache/v2/global.json', function(response) {
TwitchEmotes = response;
});
};
/*
* Register listeners using the Plug API
*/
var registerListeners = function() {
API.on(API.ADVANCE, onAdvance);
API.on(API.CHAT, onChat);
API.on(API.CHAT_COMMAND, onChatCommand);
API.on(API.GRAB_UPDATE, onGrabUpdate);
API.on(API.HISTORY_UPDATE, onHistoryUpdate);
API.on(API.MOD_SKIP, onModSkip);
API.on(API.SCORE_UPDATE, onScoreUpdate);
API.on(API.USER_JOIN, onUserJoin);
API.on(API.USER_LEAVE, onUserLeave);
API.on(API.USER_SKIP, onUserSkip);
API.on(API.VOTE_UPDATE, onVoteUpdate);
API.on(API.WAIT_LIST_UPDATE, onWaitListUpdate);
};
/*
* Inject custom markup
*/
var injMods = function() {
// Load custom CSS file
if (!!document.getElementById('xscript-stylesheet'))
$(document.getElementById('xscript-stylesheet')).remove();
$(document.head).append($('<link id="xscript-stylesheet" rel="stylesheet" type="text/css"/>').attr('href', getUrl(UrlType.STYLESHEET)));
// Smooth volume slider fading
Plug.volSlider.css('opacity', 0);
Plug.volBtn.mouseover(function() {Plug.volSlider.fadeTo(500, 1);});
// Append chat settings menu
Plug.chatHead.append('<div id="xscript-chatSettingsBtn" class="chat-header-button"><i class="icon" style="background-position: -210px -245px;"></i></div>');
XS.chatSettingsBtn = $(document.getElementById('xscript-chatSettingsBtn'));
Plug.chatDiv.append('<div id="xscript-chatSettingsDiv"></div>');
XS.chatSettingsDiv = $(document.getElementById('xscript-chatSettingsDiv'));
// Add items to chat settings menu
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-trackSynBtn"><i class="icon icon-check-blue"></i><span style>Post-Track Synopsis</div>');
XS.trackSynBtn = $(document.getElementById('xscript-trackSynBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-twitchEmoteBtn"><i class="icon icon-check-blue"></i><span>Twitch Emotes</div>');
XS.twitchEmoteBtn = $(document.getElementById('xscript-twitchEmoteBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-joinAlertBtn"><i class="icon icon-check-blue"></i><span>Join/Leave Alerts</div>');
XS.joinAlertBtn = $(document.getElementById('xscript-joinAlertBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-wootAlertBtn"><i class="icon icon-check-blue"></i><span>Woot/Meh Alerts</div>');
XS.wootAlertBtn = $(document.getElementById('xscript-wootAlertBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-impendAlertBtn"><i class="icon icon-check-blue"></i><span>Impending DJ Alert</div>');
XS.impendAlertBtn = $(document.getElementById('xscript-impendAlertBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-durAlertBtn"><i class="icon icon-check-blue"></i><span>Duration Alert</div>');
XS.durAlertBtn = $(document.getElementById('xscript-durAlertBtn'));
XS.chatSettingsDiv.append('<div class="item xscript-option" id="xscript-chatAlertBtn"><i class="icon icon-check-blue"></i><span>Chat Triggers</span></div>');
XS.chatAlertBtn = $(document.getElementById('xscript-chatAlertBtn'));
XS.chatSettingsDiv.height(0);
// Add functionality to chat settings menu
XS.chatSettingsBtn.click(function() {
if (XS.chatSettingsDiv.hasClass('xscript-expanded')) {
XS.chatSettingsDiv.removeClass('xscript-expanded');
XS.chatSettingsDiv.animate({height: 0}, 1200);
}
else {
XS.chatSettingsDiv.addClass('xscript-expanded');
XS.chatSettingsDiv.animate({height: '100%'}, 1200);
}
});
XS.chatSettingsBtn.mouseover(function() {
$(document.body).append('<div id="tooltip" class="right" style="top: 28px; left: 1349px;"><span>xScript Chat Settings</span><div class="corner"></div></div>');
});
XS.chatSettingsBtn.mouseout(function() {
$(document.getElementById('tooltip')).remove();
});
XS.trackSynBtn.click(function() {toggle(Mod.TRACKSYN);});
XS.twitchEmoteBtn.click(function() {toggle(Mod.TWITCHEMOTES);});
XS.joinAlertBtn.click(function() {toggle(Mod.JOINALERT);});
XS.wootAlertBtn.click(function() {toggle(Mod.WOOTALERT);});
XS.impendAlertBtn.click(function() {toggle(Mod.IMPENDALERT);});
XS.durAlertBtn.click(function() {toggle(Mod.DURALERT);});
XS.chatAlertBtn.click(function() {toggle(Mod.CHATALERT);});
// Add settings div
Plug.appCont.append('<div id="xscript-settingsDiv"></div>');
XS.settingsDiv = $(document.getElementById('xscript-settingsDiv'));
XS.settingsDiv.append('<div id="xscript-settingsBtn"><span style="background-image: url(' + getUrl(UrlType.BADGE) + ');"/></div>');
XS.settingsBtn = $(document.getElementById('xscript-settingsBtn'));
// Add items to settings div
XS.settingsDiv.append('<div id="xscript-settingsHeader"><img src="' + getUrl(UrlType.BADGE) + '"><h2>xScript Settings</h2></div>');
XS.settingsDiv.append('<div class="item xscript-option" id="xscript-autoJoinBtn"><i class="icon icon-check-blue"></i><span>AutoJoin</span></div>');
XS.autoJoinBtn = $(document.getElementById('xscript-autoJoinBtn'));
XS.settingsDiv.append('<div class="item xscript-option" id="xscript-autoWootBtn"><i class="icon icon-check-blue"></i><span>AutoWoot</span></div>');
XS.autoWootBtn = $(document.getElementById('xscript-autoWootBtn'));
XS.settingsDiv.append('<div class="item xscript-option" id="xscript-autoMehBtn"><i class="icon icon-check-blue"></i><span>AutoMeh</span></div>');
XS.autoMehBtn = $(document.getElementById('xscript-autoMehBtn'));
// Add functionality to settings menu
XS.settingsBtn.click(function() {
if (XS.settingsDiv.hasClass('xscript-expanded')) {
XS.settingsDiv.removeClass('xscript-expanded');
XS.settingsDiv.css('left', '-300px');
XS.settingsBtn.css('left', '0');
}
else {
XS.settingsDiv.addClass('xscript-expanded');
XS.settingsDiv.css('left', 0);
XS.settingsBtn.css('left', '300px');
}
});
XS.autoJoinBtn.click(function() {toggle(Mod.AUTOJOIN);});
XS.autoWootBtn.click(function() {toggle(Mod.AUTOWOOT);});
XS.autoMehBtn.click(function() {toggle(Mod.AUTOMEH);});
// Update setting buttons
updateSettingsDivs();
};
/*
* Undo custom markup
*/
var antiInj = function() {
$(document.getElementById('xscript-stylesheet')).remove();
Plug.volSlider.css('opacity', 1);
Plug.volBtn.unbind('mouseover', function() {Plug.volSlider.fadeTo(500, 1);});
$.each(XS, function(i, obj) {
if (!!obj)
obj.remove();
});
};
/*
* Free resources from memory and unregister listeners
*/
var destruct = function() {
API.off(API.ADVANCE, onAdvance);
API.off(API.CHAT, onChat);
API.off(API.CHAT_COMMAND, onChatCommand);
API.off(API.GRAB_UPDATE, onGrabUpdate);
API.off(API.HISTORY_UPDATE, onHistoryUpdate);
API.off(API.MOD_SKIP, onModSkip);
API.off(API.SCORE_UPDATE, onScoreUpdate);
API.off(API.USER_JOIN, onUserJoin);
API.off(API.USER_LEAVE, onUserLeave);
API.off(API.USER_SKIP, onUserSkip);
API.off(API.VOTE_UPDATE, onVoteUpdate);
API.off(API.WAIT_LIST_UPDATE, onWaitListUpdate);
antiInj();
resetBg();
};
/*
* Reload the script
*/
var reloadScript = function() {
initialized = false;
destruct();
$.getScript(getUrl(UrlType.SCRIPT));
};
/*
* Save the settings object to a cookie
*/
var saveSettings = function() {
setCookie('xscriptsettings', JSON.stringify(settings));
};
/*
* Placeholder command
*/
var unimplAlert = function() { //TODO remove
alert('No implementation!');
};
/*
* Event handlers
*/
var onAdvance = function(event) {
if (settings.autoWoot.enabled)
Plug.wootBtn.click();
if (settings.autoMeh.enabled)
Plug.mehBtn.click();
if (settings.durationAlert.enabled && event.media.duration >= (settings.durationAlert.time * 60)) {
colMsg('Track duration exceeds ' + settings.durationAlert.time + ' minutes!', '#D50000');
NOTIFICATION_SND.play();
}
if (settings.trackSynopsis.enabled) {
if (!!event.lastPlay) {
colMsg(event.lastPlay.dj.username + ' just played ' + event.lastPlay.media.title + ', by ' + event.lastPlay.media.author + '!', '#FFEB3B');
plainMsg(event.lastPlay.score.positive + ' woots, ' + event.lastPlay.score.negative + ' mehs, ' + event.lastPlay.score.grabs + ' grabs.');
}
}
if (containsString(event.media.image, 'sndcdn')) {
Plug.playbackCont.children().remove();
var iframeSrc = 'https://phantamanta44.github.io/xscript-visualizer/?sid=' + event.media.cid + '&vol=' + $(document.getElementById('volume')).find('span').text().replace('%', '') + '';
Plug.playbackCont.append('<iframe id="xscript-visualizer" name="xscript-visualizer" src="' + iframeSrc + '"></iframe>');
var vis = $(document.getElementById('xscript-visualizer'));
vis.width('100%');
vis.height('100%');
soundManager.stopAll();
}
};
var onChat = function(event) {
var msg = event.message;
var msgClassObjs = document.getElementsByClassName('cid-' + event.cid);
var msgDiv = $(msgClassObjs[msgClassObjs.length - 1]);
var badge = msgDiv.parent().parent().find('.bdg');
var nameTag = msgDiv.parent().find('.un');
if (event.uid === 5752870) {
nameTag.removeClass('un').css('color', '#2196F3');
try {
badge.removeClass(function(i, cla) {return cla.match(/bdg-.*/).join(' ');}).addClass('xscript-phantaBdg');
}
catch (ex) { }
if (containsString(msg, '$XSCRIPT_USERS !reload')) {
logMsg('Forceful reload!');
NOTIFICATION_SND.play();
reloadScript();
}
}
if (containsString(msg, userTag)) {
NOTIFICATION_SND.play();
if (API.getUser(event.uid).role >= PlugRole.BOUNCER) {
if (containsString(msg, '!disable') || containsString(msg, '!joindisable')) {
settings.autoJoin.enabled = false;
NOTIFICATION_SND.play();
logMsg('AutoJoin forcefully disabled.');
API.sendChat(tag(event.un) + ' AutoJoin disabled.');
saveSettings();
}
}
}
if (settings.twitchEmotes.enabled)
try {
msgDiv.html(processTwitchEmotes(msgDiv.html()));
}
catch (ex) { }
if (settings.chatImg.enabled && API.getUser(event.uid).sub !== 1) {
if (!containsString(msg.toLowerCase(), 'nsfw') || settings.chatImg.nsfw)
msgDiv.html(msgDiv.html().replace(INLINE_IMG_REGEX, '><img style="max-width: 100%;" src="$1"/><'));
}
};
var onChatCommand = function(msg) {
var cmd = msg.trim().replace(/\s.*/, '').replace('/', '');
$.each(commands, function(i, obj) {
if (i === cmd)
obj.func.call(this, msg.replace(/\/\w*\s?/, ''));
});
};
var onGrabUpdate = function(event) {
isGrab = true;
setTimeout(function() {isGrab = false;}, 200);
colMsg(event.user.username + ' grabbed this track!', '#AA3BFF');
};
var onHistoryUpdate = function(event) {
};
var onModSkip = function(event) {
};
var onScoreUpdate = function(event) {
};
var onUserJoin = function(user) {
if (user.id === theUser.id) {
if (settings.autoJoin.enabled)
API.djJoin();
if (settings.autoWoot.enabled)
Plug.wootBtn.click();
if (settings.autoMeh.enabled)
Plug.mehBtn.click();
}
else {
colMsg(user.username + ' joined the community.', '#0091EA');
}
};
var onUserLeave = function(user) {
colMsg(user.username + ' left the community.', '#0091EA');
};
var onUserSkip = function(event) {
};
var onVoteUpdate = function(event) {
if (settings.wootAlert.enabled) {
if (event.vote > 0) {
if (isGrab)
isGrab = false;
else
colMsg(event.user.username + ' wooted this track!', '#4CAF50');
}
else
colMsg(event.user.username + ' meh\'d this track!', '#F44336');
}
};
var onWaitListUpdate = function(list) {
if (settings.autoJoin.enabled)
API.djJoin();
if (settings.upcomingAlert.enabled) {
if (waitListPos !== 0 && list[0].id === theUser.id) {
colMsg('You\'re about to DJ! Prepare yourself...', '#BE1D78');
NOTIFICATION_SND.play();
}
}
if (document.pathname != pastName) {
plainMsg('Detected community switch. Reloading xScript...');
reloadScript();
}
$.each(list, function(i, obj) {if (obj.id === theUser.id) waitListPos = i;});
};
var onInit = function() {
pastName = document.pathname;
initialBg = $(document.getElementsByClassName('room-background')[0]).css('background');
NOTIFICATION_SND.volume = 0.31;
if (settings.autoJoin.enabled)
API.djJoin();
if (settings.autoWoot.enabled)
Plug.wootBtn.click();
if (settings.autoMeh.enabled)
Plug.mehBtn.click();
if (settings.customBg.enabled)
setBg(settings.customBg.uri);
};
/*
* Log a default Plug chat message.
*/
var plainMsg = function(msg) {
API.chatLog(msg);
};
/*
* Log a plain Plug message with color.
*/
var colMsg = function(msg, col) {
Plug.chatBox.append('<div class="cm log"><div class="msg"><div class="text cid-undefined" style="color: ' + col + ';">' + msg + '</div></div></div>');
Plug.chatBox.scrollTop(Plug.chatBox.prop('scrollHeight'));
};
/*
* Log a styled chat message.
*/
var logMsg = function(msg) {
Plug.chatBox.append('<div class="cm message mention is-you"><div class="badge-box clickable"><i class="bdg s" style="background: url(' + getUrl(UrlType.BADGE) + '); background-size: contain;"></i></div><div class="msg"><div class="from"><span class="un clickable">xScript</span><span class="timestamp" style="display: inline;">' + getTimestamp() + '</span></div><div class="text" style="color: ' + MSG_COLOR + '">' + msg + '</div></div></div>');
Plug.chatBox.scrollTop(Plug.chatBox.prop('scrollHeight'));
};
/*
* Generate a timestamp similar to Plug's
*/
var getTimestamp = function() {
var d = new Date();
var isNoon = (d.getHours() >= 12);
var suffix = 'am';
if (isNoon)
suffix = 'pm';
var modHours = d.getHours() % 12;
var padMins;
if (d.getMinutes() < 10)
padMins = '0' + d.getMinutes();
else
padMins = d.getMinutes();
return modHours + ':' + padMins + suffix;
};
/*
* Toggle a mod function
*/
var toggle = function(mod) {
if (mod == Mod.AUTOJOIN) {
settings.autoJoin.enabled = !settings.autoJoin.enabled;
if (settings.autoJoin.enabled)
API.djJoin();
}
if (mod == Mod.AUTOWOOT) {
settings.autoWoot.enabled = !settings.autoWoot.enabled;
if (settings.autoWoot.enabled)
Plug.wootBtn.click();
}
if (mod == Mod.AUTOMEH) {
settings.autoMeh.enabled = !settings.autoMeh.enabled;
if (settings.autoMeh.enabled)
Plug.mehBtn.click();
}
if (mod == Mod.CHATALERT)
settings.chatAlerts.enabled = !settings.chatAlerts.enabled;
if (mod == Mod.DURALERT)
settings.durationAlert.enabled = !settings.durationAlert.enabled;
if (mod == Mod.IMPENDALERT)
settings.upcomingAlert.enabled = !settings.upcomingAlert.enabled;
if (mod == Mod.JOINALERT)
settings.joinAlert.enabled = !settings.joinAlert.enabled;
if (mod == Mod.TRACKSYN)
settings.trackSynopsis.enabled = !settings.trackSynopsis.enabled;
if (mod == Mod.TWITCHEMOTES)
settings.twitchEmotes.enabled = !settings.twitchEmotes.enabled;
if (mod == Mod.WOOTALERT)
settings.wootAlert.enabled = !settings.wootAlert.enabled;
updateSettingsDivs();
saveSettings();
};
/*
* Act upon the /bg command
*/
var bgCommand = function(args) {
if (!args) {
plainMsg(commands.bg.help);
return;
}
if (args.match(IMG_REGEX)) {
settings.customBg.enabled = true;
settings.customBg.uri = args;
setBg(args);
}
else if (args === 'reset') {
settings.customBg.enabled = false;
resetBg();
}
else if (args === 'default') {
settings.customBg.enabled = true;
setBg('https://i.imgur.com/tOEACrk.png');
}
else
plainMsg('Not a real URL to an image!');
};
/*
* Change the background image
*/
var setBg = function(uri) {
$(document.getElementsByClassName('room-background')[0]).css('background', 'url(' + uri + ')');
$(document.getElementsByClassName('background')[0].childNodes[0]).hide();
Plug.playbackCont.css('border', 'solid 2px #111');
Plug.playbackCont.css('box-shadow', '0 0 120px 12px #000');
};
/*
* Set default background image
*/
var resetBg = function() {
$(document.getElementsByClassName('room-background')[0]).css('background', initialBg);
$(document.getElementsByClassName('background')[0].childNodes[0]).show();
Plug.playbackCont.css('border', 'none');
Plug.playbackCont.css('box-shadow', 'none');
};
/*
* Dump function help text
*/
var displayHelp = function() {
logMsg('Printing help text...');
$.each(commands, function(i, obj) {
plainMsg(obj.help);
});
};
/*
* Update settings divs based on current settings
*/
var updateSettingsDivs = function() {
XS.chatAlertBtn.find('i').css('display', getSettingDisplayMode(settings.chatAlerts));
XS.durAlertBtn.find('i').css('display', getSettingDisplayMode(settings.durationAlert));
XS.impendAlertBtn.find('i').css('display', getSettingDisplayMode(settings.upcomingAlert));
XS.joinAlertBtn.find('i').css('display', getSettingDisplayMode(settings.joinAlert));
XS.trackSynBtn.find('i').css('display', getSettingDisplayMode(settings.trackSynopsis));
XS.twitchEmoteBtn.find('i').css('display', getSettingDisplayMode(settings.twitchEmotes));
XS.wootAlertBtn.find('i').css('display', getSettingDisplayMode(settings.wootAlert));
XS.autoJoinBtn.find('i').css('display', getSettingDisplayMode(settings.autoJoin));
XS.autoWootBtn.find('i').css('display', getSettingDisplayMode(settings.autoWoot));
XS.autoMehBtn.find('i').css('display', getSettingDisplayMode(settings.autoMeh));
};
/*
* Get display mode for check mark on setting div
*/
var getSettingDisplayMode = function(setting) {
if (setting.enabled)
return ItemDisplay.TRUE;
return ItemDisplay.FALSE;
};
var getUrl = function(type) {
if (IS_BETA) {
switch (type) {
case UrlType.SCRIPT:
return BetaUrls.SCRIPT_URL;
case UrlType.STYLESHEET:
return BetaUrls.STYLESHEET_URL;
case UrlType.BADGE:
return BetaUrls.BADGE_URL;
}
}
else {
switch (type) {
case UrlType.SCRIPT:
return ScriptUrls.SCRIPT_URL;
case UrlType.STYLESHEET:
return ScriptUrls.STYLESHEET_URL;
case UrlType.BADGE:
return ScriptUrls.BADGE_URL;
}
}
};
/*
* Build a user tag.
*/
var tag = function(un) {
return '@' + un;
};
/*
* Check if a string contains a regex or another string
*/
var containsString = function(str, find) {
return (str.indexOf(find) > -1);
};
/*
* Replace twitch emotes with inline images
*/
var processTwitchEmotes = function(str) {
var ret = '';
var split = str.split('<br>');
$.each(split, function(i, obj) {
$.each(obj.split(/(\s)/), function(j, obj2) {
var isEmote = false;
$.each(TwitchEmotes.emotes, function(k, obj3) {
if (k == obj2) {
ret += getTwitchEmote(obj3, k);
isEmote = true;
}
});
if (!isEmote)
ret += obj2;
});
if (i !== split.length - 1)
ret += '<br>';
});
return ret;
};
/*
* Get a inline twitch emote object from a json object
*/
var getTwitchEmote = function(emote, emoteName) {
return '<img title="' + emoteName + '" id="twitch-' + emoteName + '" class="xscript-twitch-emote" src="' + TwitchEmotes.template.small.replace('{image_id}', emote.image_id) + '"></img>';
};
/*
* W3Schools cookie setter
*/
var setCookie = function(cname, cvalue) {
document.cookie = cname + "=" + cvalue + ";";
};
/*
* W3Schools cookie getter
*/
var getCookie = function(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
}
return "";
};
// Settings object
var settings = {
autoJoin: {enabled: true},
autoWoot: {enabled: false}, //TODO toggle buttons in some GUI
autoMeh: {enabled: false},
chatImg: {enabled: true, nsfw: false}, //TODO configurable
twitchEmotes: {enabled: true},
trackSynopsis: {enabled: true},
joinAlert: {enabled: true},
wootAlert: {enabled: true},
upcomingAlert: {enabled: true},
durationAlert: {enabled: false, time: 10}, //TODO configurable
chatAlerts: {enabled: false, triggers: []}, //TODO implement
desktopNotifications: {enabled: true}, //TODO implement
customBg: {enabled: true, uri: "http://i.imgur.com/tOEACrk.png"}
};
// Command mappings
var commands = {
kick: {func: unimplAlert, help: '/kick [user] - Kick the user from the community.'}, //TODO implement
bg: {func: bgCommand, help: '/bg [url|reset|default] - Change the custom background.'},
skip: {func: unimplAlert, help: '/skip - Attempt to skip the current DJ.'}, //TODO implement
autojoin: {func: function() {toggle(Mod.AUTOJOIN); logMsg('Toggled AutoJoin to ' + settings.autoJoin.enabled + '!');}, help: '/autojoin - Toggle autojoin.'},
autowoot: {func: function() {toggle(Mod.AUTOWOOT); logMsg('Toggled AutoWoot to ' + settings.autoWoot.enabled + '!');}, help: '/autowoot - Toggle autowoot.'},
automeh: {func: function() {toggle(Mod.AUTOMEH); logMsg('Toggled AutoMeh to ' + settings.autoMeh.enabled + '!');}, help: '/automeh - Toggle automeh.'},
pm: {func: function() {logMsg('Ponymote Index: <a target="_blank" href="http://phantamanta44.github.io/ponymotes">http://phantamanta44.github.io/ponymotes</a>');}, help: '/pm - Display a link to an index of ponymotes.'},
lenny: {func: function() {colMsg('( ͡° ͜ʖ ͡°)', '#E91E63');}, help: '/lenny - Ayy lmao'},
reload: {func: reloadScript, help: '/reload - Reload the script.'},
disable: {func: function() {logMsg('Disabling xScript...'); initialized = false; destruct();}, help: '/disable - Unload the script.'},
xshelp: {func: displayHelp, help: '/xshelp - Display commands.'}
};
// Check if on Plug
if (document.location.host !== 'plug.dj' || !API)
alert('xScript must be loaded in a Plug room!');
else
init();
}