-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathchat_filter.user.js
1306 lines (1062 loc) · 37.1 KB
/
chat_filter.user.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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Twitch Plays Pokemon Chat Filter
// @namespace https://github.com/jpgohlke/twitch-chat-filter
// @description Hide input commands from the chat.
// @include /^https?://(www|beta)\.twitch\.tv\/(twitchplayspokemon(/(chat.*)?)?|chat\/.*channel=twitchplayspokemon.*)$/
// @version 3.7
// @updateURL https://jpgohlke.github.io/twitch-chat-filter/chat_filter.meta.js
// @downloadURL https://jpgohlke.github.io/twitch-chat-filter/chat_filter.user.js
// @grant none
// @run-at document-end
// ==/UserScript==
/*
* 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.
*/
/*
* chat_filter.user.js
*
* Feel free to review/compress it yourself; good internet security is important!
* Passes http://www.jshint.com on default settings
* Contributors:
* /u/RenaKunisaki
* /u/smog_alado
* /u/SRS-SRSLY
* /u/schrobby
* /u/red_agent
* /u/DeathlyDeep
* /u/jeff_gohlke
* /u/yankjenets
* /u/MKody
* /u/feha
* /u/jakery2
* /u/redopium
* /u/codefusion
* /u/Zephymastyx
* /u/anonveggy
* /u/rctgamer3
* /u/BBQCalculator
* /u/Soulweaver91
*/
// ******************
// CODING GUIDELINES
// ******************
// - Make sure that the code passes JSHint (http://www.jshint.com)
// - Write all code inside the wrapper IIFE to avoid creating global variables.
// - Constants and global variables are UPPER_CASE.
/* jshint
lastsemic:true,
eqeqeq:true,
sub:true
*/
/* global
$: false,
localStorage: false,
require,
Twitch: false,
*/
(function(code){
"use strict";
// ----------------------------
// Greasemonkey support
// ----------------------------
// Greasemonkey userscripts run in a separate environment and cannot use global
// variables from the page directly. Vecause of this, we package all out code inside
// a script tag and have it run in the context of the main page.
// TODO: is there a way to get better error messages? It won't show any line numbers.
var s = document.createElement('script');
s.appendChild(document.createTextNode(
'(' + code.toString() + '());'
));
document.body.appendChild(s);
}(function(){
"use strict";
if (!window.$) { return; }
var TCF_VERSION = "3.7";
var TCF_INFO = "TPP Chat Filter version " + TCF_VERSION + " loaded. Please report bugs and suggestions to https://github.com/jpgohlke/twitch-chat-filter";
// ============================
// Array Helpers
// ============================
function forEach(xs, f){
for(var i=0; i<xs.length; i++){
f(xs[i], i, xs);
}
}
function any(xs, pred){
for(var i=0; i<xs.length; i++){
if(pred(xs[i])) return true;
}
return false;
}
function all(xs, pred){
for(var i=0; i<xs.length; i++){
if(!pred(xs[i])) return false;
}
return true;
}
function forIn(obj, f){
for(var k in obj){
if(Object.prototype.hasOwnProperty.call(obj, k)){
f(k, obj[k]);
}
}
}
function str_contains(string, pattern){
string = string.toLowerCase();
return (string.indexOf(pattern.toLowerCase()) >= 0);
}
// ============================
// Initialization code
// ============================
var tcf_initializers = [];
function add_initializer(init){
tcf_initializers.push(init);
}
function run_initializers(){
forEach(tcf_initializers, function(init){
init();
});
}
// ============================
// Configuration Settings
// ============================
var REQUIRED_SETTING_PARAMS = [
'name', // Unique identifier for the setting,
// used to store it persistently or to generate CSS classes
'comment', // Short description of the setting
'category', // What menu to put this setting under
'defaultValue' // Can be either boolean or list of strings.
];
var OPTIONAL_SETTING_PARAMS = [
'longComment', // Longer description that shows when you hover over.
'message_filter', // When active, filter new chat messages using this predicate
'message_css', // When active, modify the existing chat lines with these CSS rules.
'message_rewriter' // When active, replace the text of the message with the result of this function
];
function Setting(kv){
// Check for required parameters and typos:
forEach(REQUIRED_SETTING_PARAMS, function(param){
if(!(param in kv)){
throw new Error("Missing param " + param);
}
});
forIn(kv, function(param){
if(
REQUIRED_SETTING_PARAMS.indexOf(param) < 0 &&
OPTIONAL_SETTING_PARAMS.indexOf(param) < 0
){
throw new Error("Unexpected param " + param);
}
});
// Initialize members
var that = this;
forIn(kv, function(key, val){
that[key] = val;
});
this._value = null;
this._observers = [];
}
Setting.prototype.getValue = function(){
if(this._value !== null){
return this._value;
}else{
return this.defaultValue;
}
};
Setting.prototype.setValue = function(value){
var oldValue = this.getValue();
this._value = value;
var newValue = this.getValue();
forEach(this._observers, function(obs){
obs(newValue, oldValue);
});
};
Setting.prototype.reset = function(){
this.setValue(null);
};
Setting.prototype.observe = function(onChange){
this._observers.push(onChange);
};
Setting.prototype.forceObserverUpdate = function(){
var value = this.getValue();
forEach(this._observers, function(obs){
obs(value, value);
});
};
var TCF_SETTINGS_LIST = [];
var TCF_SETTINGS_MAP = {};
var TCF_FILTERS = [];
var TCF_REWRITERS = [];
var TCF_STYLERS = [];
function add_setting(kv){
var setting = new Setting(kv);
TCF_SETTINGS_LIST.push(setting);
TCF_SETTINGS_MAP[setting.name] = setting;
if(setting.message_filter ){ TCF_FILTERS.push(setting); }
if(setting.message_css ){ TCF_STYLERS.push(setting); }
if(setting.message_rewriter){ TCF_REWRITERS.push(setting); }
}
function get_setting_value(name){
return TCF_SETTINGS_MAP[name].getValue();
}
// ----------------------------
// Persistence
// ----------------------------
var STORAGE_KEY = "tpp-chat-filter-settings";
var LEGACY_FILTERS_KEY = "tpp-custom-filter-active";
var LEGACY_PHRASES_KEY = "tpp-custom-filter-phrases";
function get_local_storage_item(key){
var item = localStorage.getItem(key);
return (item ? JSON.parse(item) : null);
}
function set_local_storage_item(key, value){
localStorage.setItem(key, JSON.stringify(value));
}
function get_old_saved_settings(){
//For compatibility with older versions of the script.
var persisted = {};
var old_filters = get_local_storage_item(LEGACY_FILTERS_KEY);
if(old_filters){
forIn(TCF_SETTINGS_MAP, function(name){
forEach(["filters", "rewriters", "stylers"], function(category){
if(old_filters[category].indexOf(name) >= 0){
persisted[name] = true;
}
});
});
}
var old_banned_phrases = get_local_storage_item(LEGACY_PHRASES_KEY);
if(old_banned_phrases){
persisted['TppBanCustomWords'] = true;
persisted['TppBannedWords'] = old_banned_phrases;
}
return persisted;
}
function load_settings(){
var persisted;
if(window.localStorage){
persisted = get_local_storage_item(STORAGE_KEY) || get_old_saved_settings();
}else{
persisted = {};
}
forIn(TCF_SETTINGS_MAP, function(name, setting){
if(name in persisted){
setting.setValue(persisted[name]);
}else{
setting.setValue(null);
}
});
}
function save_settings(){
if(!window.localStorage) return;
var persisted = {};
forIn(TCF_SETTINGS_MAP, function(name, setting){
if(setting._value !== null){
persisted[name] = setting._value;
}
});
set_local_storage_item(STORAGE_KEY, persisted);
localStorage.removeItem(LEGACY_FILTERS_KEY);
localStorage.removeItem(LEGACY_PHRASES_KEY);
}
add_initializer(function(){
forEach(TCF_SETTINGS_LIST, function(setting){
setting.observe(function(){
save_settings();
});
});
});
// ============================
// UI
// ============================
var CHAT_ROOM_SELECTOR = '.chat-room';
var CHAT_MESSAGE_SELECTOR = '.message';
var CHAT_FROM_SELECTOR = '.from';
var CHAT_LINE_SELECTOR = '.chat-line';
var CHAT_TEXTAREA_SELECTOR = ".chat-interface textarea";
var CHAT_BUTTON_SELECTOR = "button.send-chat-button";
function add_custom_css(parts){
$('head').append('<style>' + parts.join("") + '</style>');
}
// ============================
// Features
// ============================
// In this part we define all the settings and filters that we support
// and all code that needs to run when the script gets initialized.
// ---------------------------
// Command Filter
// ---------------------------
var TPP_COMMANDS = [
"left", "right", "up", "down",
"start", "select",
"a", "b",
"l", "r",
"democracy", "anarchy", "wait",
"move", "switch", "run", "item"
];
var EDIT_DISTANCE_TRESHOLD = 2;
// Adapted from https://gist.github.com/andrei-m/982927
// Compute the edit distance between the two given strings
function min_edit(a, b) {
if(a.length === 0) return b.length;
if(b.length === 0) return a.length;
var matrix = [];
var i,j;
// increment along the first column of each row
for(i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// increment each column in the first row
for(j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for(i = 1; i <= b.length; i++) {
for(j = 1; j <= a.length; j++) {
if(b.charAt(i-1) === a.charAt(j-1)){
matrix[i][j] = matrix[i-1][j-1];
} else {
matrix[i][j] = 1 + Math.min(
matrix[i-1][j-1], // substitution
matrix[i][j-1] , // insertion
matrix[i-1][j] // deletion
);
}
}
}
return matrix[b.length][a.length];
}
function word_is_command(word){
return any(TPP_COMMANDS, function(cmd){
return min_edit(cmd.toLowerCase(), word.toLowerCase()) <= EDIT_DISTANCE_TRESHOLD;
});
}
function message_is_command(message){
//Touch pad coordinates
if(/^([0-9]+),([0-9]+)$/.test(message.replace(/\s/g, ""))){ return true }
// Military mode: item command - https://redd.it/45t454/
if(/^\s*item[a-z0-9]*\s*$/i.test(message)){ return true }
// Button presses
return all(message.split(/\s+/), function(word){
if(word.length <= 0){ return true }
//For compatibility with possible changes the streamer might introduce in the future,
//a command is considered to be a sequence of command words separated by some non-word separators
var commands = word.match(/(?:([a-z]+)[^a-z]{0,2})+/ig);
return commands && all(commands, function(cmd){
var segments = cmd.match(/[a-z]+/ig);
return all(segments, word_is_command);
});
});
}
add_setting({
name: 'TppFilterCommand',
comment: "Emulator commands",
longComment: TPP_COMMANDS.join(", "),
category: 'filters_category',
defaultValue: true,
message_filter: message_is_command
});
// ---------------------------
// Misty meme
// ---------------------------
// Score-based filter for "Guys, we need to beat Misty" spam.
var MISTY_SUBSTRINGS = [
"misty",
"whitney",
"milk",
"guys",
"we have to",
"we need to",
"beat"
];
function message_is_misty(message) {
var misty_score = 0;
forEach(MISTY_SUBSTRINGS, function(s){
if(str_contains(message, s)){
misty_score++;
}
});
return (misty_score >= 2);
}
add_setting({
name: 'TppFilterMisty',
comment: 'Misty meme',
longComment : "Guys we need to milk Witney",
category: 'filters_category',
defaultValue: true,
message_filter: message_is_misty
});
// ---------------------------
// Hitler drawings
// ---------------------------
function message_is_drawing(message){
var nonASCII = 0;
for(var i = 0; i < message.length; i++) {
var c = message.charCodeAt(i);
if(9600 <= c && c <= 9632){
nonASCII++;
}
}
return (nonASCII > 3);
}
add_setting({
name: 'TppFilterAscii',
comment: "Blocky drawings",
longComment: "Stuff like this: \u2591\u2591\u2591\u2591\u2592\u2592\u2592\u2592\u258C \u2580\u2592\u2580\u2590\u2584\u2588",
category: 'filters_category',
defaultValue: true,
message_filter: message_is_drawing
});
// ---------------------------
// Cyrillic
// ---------------------------
// Some people use cyrillic characters to write spam that gets past the other filters.
function message_is_cyrillic(message){
//Some people use cyrillic characters to write spam that gets past the filter.
return /[\u0400-\u04FF]/.test(message);
}
add_setting({
name: 'TppFilterCyrillic',
comment: 'Cyrillic',
longComment : "Cyrillic characters in copypastas confuse our other filters",
category: 'filters_category',
defaultValue: true,
message_filter: message_is_cyrillic
});
// ---------------------------
// Dongers
// ---------------------------
//typical unicodes of dongers (mostly eyes)
var DONGER_CODES = [3720, 9685, 664, 8362, 3232, 176, 8248, 8226, 7886, 3237];
function message_is_donger(message){
var donger_count = 0;
for(var i = 0; i < message.length; i++) {
var c = message.charCodeAt(i);
if(DONGER_CODES.indexOf(c) >= 0) {
donger_count++;
}
}
return (donger_count > 1);
}
add_setting({
name: 'TppFilterDonger',
comment: "Dongers",
longComment: "\u30FD\u0F3C\u0E88\u0644\u035C\u0E88\u0F3D\uFF89",
category: 'filters_category',
defaultValue: false,
message_filter: message_is_donger
});
// ---------------------------
// One-word messages
// ---------------------------
function message_is_small(message){
return message.split(/\s/g).length <= 1;
}
add_setting({
name: 'TppFilterSmall',
comment: "One-word messages",
category: 'filters_category',
defaultValue: false,
message_filter: message_is_small
});
// ---------------------------
// Walls of text
// ---------------------------
// For messages that fill up more than 4 lines
function message_is_too_long(message){
return (message.length >= 200);
}
add_setting({
name: 'TppFilterLong',
comment: 'Overly long messages',
longComment: "Hide messages over 200 characters (around 4 lines)",
category: 'filters_category',
defaultValue: false,
message_filter: message_is_too_long
});
// ---------------------------
// Pokemon Stadium betting
// ---------------------------
// Filter betting commands for the parallel pokemon stadium betting game
function message_is_bet(message){
return /^\s*\!/.test(message);
}
add_setting({
name: 'TppFilterBets',
comment: "Stadium bets",
longComment: "Any message starting with a \"!\". ex.: \"!bet 100 blue\"",
category: 'filters_category',
defaultValue: true,
message_filter: message_is_bet
});
// ---------------------------
// Pokemon Stadium bank bot
// ---------------------------
// Filter bank bot messages for the parallel pokemon stadium betting game
var logged_in_user_name = null;
add_initializer(function(){
if(Twitch){
logged_in_user_name = Twitch.user.displayName();
}
});
function message_is_bank_bot(message, from){
if(from.toLowerCase() === 'tppbankbot'){
if(logged_in_user_name){
// Filter messages not mentioning logged in user
return message.toLowerCase().indexOf('@'+logged_in_user_name.toLowerCase()) < 0;
} else {
// Filter all messages
return true;
}
}
return false;
}
add_setting({
name: 'TppFilterBankBot',
comment: "Stadium bank bot",
longComment: "Messages from the bank bot about other players' balances",
category: 'filters_category',
defaultValue: true,
message_filter: message_is_bank_bot
});
// ---------------------------
// Copy-paste rewriter
// ---------------------------
// Replace repetitive text with only one instance of it.
// Useful for when people do ctrl-c ctrl-v ctrl-v ctrl-v
// in order to increase the size of the message.
function rewrite_copy_paste(message){
return message.replace(/(.{4}.*?)(\s*?\1)+/g, "$1");
}
add_setting({
name: 'TppRewriteDuplicates',
comment: "Copy pasted repetitions",
category: 'rewriters_category',
defaultValue: true,
message_rewriter: rewrite_copy_paste
});
// ---------------------------
// Zalgo text
// ---------------------------
//removes unicode characters that are used to cover multiple lines (Oops I spilled my drink)
function mop_up_drinks(message){
return message.replace(/[\u0300-\u036F]/g, '');
}
add_setting({
name: 'TppMopUpDrinks',
comment: "Mop up spilled drinks",
category: 'rewriters_category',
defaultValue: true,
message_rewriter: mop_up_drinks
});
// ---------------------------
// Lowercase converter
// ---------------------------
add_setting({
name: 'TppConvertAllcaps',
comment: "Lowercase everything",
longComment: null,
category: 'visual_category',
defaultValue: true,
message_css: CHAT_MESSAGE_SELECTOR + "{text-transform:lowercase !important;}"
});
// ---------------------------
// Hide emoticons
// ---------------------------
var emoticon_regexes = [];
add_initializer(function(){
if(Twitch){
Twitch.api.get("chat/emoticons").then(function(data){
forEach(data.emoticons, function(d){
var regex = d.regex;
if(regex.match(/^\w+$/)){
regex = '\\b' + regex + '\\b';
}
emoticon_regexes.push(new RegExp(regex, 'g'));
});
});
}
});
function message_is_only_emoticons(message){
//Detect if a message would look empty if we got rid of all emoticons.
var withoutEmoticons = message;
forEach(emoticon_regexes, function(regexp){
withoutEmoticons = withoutEmoticons.replace(regexp, "");
});
return (/^\s*$/.test(withoutEmoticons));
}
add_setting({
name: 'TppHideEmoticons',
comment: "Hide emoticons",
category: 'visual_category',
defaultValue: false,
message_css: CHAT_MESSAGE_SELECTOR + " .emoticon{display:none !important;}",
message_filter: message_is_only_emoticons
});
// ---------------------------
// Uncolor messages
// ---------------------------
add_setting({
name: 'TppNoColor',
comment: "Uncolor messages",
longComment: 'Remove color from messages created with the /me command',
category: 'visual_category',
defaultValue: false,
message_css: CHAT_MESSAGE_SELECTOR + " {color:inherit !important;}"
});
// ---------------------------
// Banned Words
// ---------------------------
function message_contains_banned_word(message){
var shouldBan = get_setting_value('TppBanCustomWords');
var bannedWords = get_setting_value('TppBannedWords');
return shouldBan && any(bannedWords, function(banned){
return str_contains(message, banned);
});
}
add_setting({
name: 'TppBanCustomWords',
comment: "Activate custom banlist",
longComment: "",
category: 'customs_category',
defaultValue: false,
message_css: "#menu-TppBannedWords { display:inherit; }"
});
add_initializer(function(){
add_custom_css([
"#menu-TppBannedWords { display:none; }"
]);
});
add_setting({
name: 'TppBannedWords',
comment: "Banned Phrases",
longComment: "If the custom banlist is activated, these messages will be hidden",
category: 'customs_category',
defaultValue: [],
message_filter: message_contains_banned_word
});
// ============================
// Settings Control Panel
// ============================
//var SETTINGS_BUTTON_SELECTOR = "button.settings";
var SETTINGS_MENU_SELECTOR = ".chat-settings";
add_initializer(function(){
add_custom_css([
".chat-room { z-index: inherit !important; }",
".chat-settings { z-index: 100 !important; }",
".chat-settings { max-height: 500px; }",
".chat-settings label { font-weight: inherit; }",
".custom_list_menu li {background: #bbb; display: block; list-style: none; margin: 1px 0; padding: 0 2px}",
".custom_list_menu li a {float: right;}"
]);
var settingsMenu = $(SETTINGS_MENU_SELECTOR);
function addBooleanSetting(menuSection, option){
menuSection.append(
$('<label>').attr('for', option.name).attr('title', option.longComment || "")
.append( $('<input type="checkbox">').attr('id', option.name) )
.append( document.createTextNode(' ' + option.comment) )
);
var checkbox = $('#' + option.name);
checkbox.on('change', function(){
option.setValue( $(this).prop("checked") );
});
option.observe(function(newValue){
checkbox.prop('checked', newValue);
});
}
function addListSetting(menuSection, option){
menuSection
.append(
$('<label>').attr('for', option.name).attr('title', option.longComment || "")
.append( document.createTextNode('Add ' + option.comment) )
.append( $('<input type="text">').attr('id', option.name).css('width', '100%') )
).append(
$('<button>').attr('id', 'show-' + option.name)
.append( document.createTextNode('Show ') )
.append( $('<span>').attr('id', 'num-banned-' + option.name) )
.append( document.createTextNode(' ' + option.comment) )
).append(
$('<button>').attr('id', 'hide-' + option.name)
.append( document.createTextNode('Hide ' + option.comment) )
).append(
$('<div class="custom_list_menu">').attr('id', 'list-' + option.name)
).append(
$('<button>').attr('id', 'clear-' + option.name)
.append( document.createTextNode('Clear ' + option.comment) )
);
function add_list_item(item){
var arr = option.getValue().slice();
if(arr.indexOf(item) < 0){
arr.push(item);
option.setValue(arr);
}
}
function remove_list_item(i){
var arr = option.getValue().slice();
arr.splice(i, 1);
option.setValue(arr);
}
function hide_inner_list(){
$('#show-'+option.name).show();
$('#hide-'+option.name).hide();
$('#clear-'+option.name).hide();
$('#list-'+option.name).hide();
}
function show_inner_list(){
$('#show-'+option.name).hide();
$('#hide-'+option.name).show();
$('#clear-'+option.name).show();
$('#list-'+option.name).show();
}
hide_inner_list();
option.observe(function(newValue){
$('#num-banned-'+option.name).text(newValue.length);
var innerList = $('#list-' + option.name);
innerList.empty();
forEach(newValue, function(word, i){
innerList.append(
$("<li>")
.text(word)
.append(
$('<a href="#">')
.text("[X]")
.click(function(){ remove_list_item(i) })
)
);
});
});
//Add new banned item when user hits enter
$('#' + option.name).keyup(function(e){
var item = $(this).val().trim();
if(e.keyCode === 13 && item !== ""){
add_list_item(item);
$(this).val('');
}
});
//open the list of banned items
$('#show-' + option.name).click(function(e){
e.preventDefault();
show_inner_list();
});
//close the list of banned items
$('#hide-' + option.name).click(function(e){
e.preventDefault();
hide_inner_list();
});
//empty the banned list completely
$('#clear-' + option.name).click(function(e){
e.preventDefault();
option.setValue([]);
});
}
function addMenuSection(name){
$('<div class="list-header"/>')
.text(name)
.appendTo(settingsMenu);
var section = $('<div class="chat-menu-content">')
.appendTo(settingsMenu);
return section;
}
function addCategoryToSection(menuSection, category){
forEach(TCF_SETTINGS_LIST, function(option){
if(option.category !== category) return;
var p = $('<p>')
.attr('id', 'menu-'+option.name)
.addClass('dropmenu_action')
.appendTo(menuSection);
var typ = typeof(option.defaultValue);
if(typ === 'boolean'){
addBooleanSetting(p, option);
}else if(typ === 'object'){
addListSetting(p, option);
}else{
throw new Error("Unrecognized setting " + typ);
}
});
}
var filter_sec = addMenuSection("Hide");
addCategoryToSection(filter_sec, 'filters_category');
var rewrite_sec = addMenuSection("Automatically rewrite");
addCategoryToSection(rewrite_sec, 'rewriters_category');
var visual_sec = addMenuSection("Visual tweaks");
addCategoryToSection(visual_sec, 'visual_category');
var custom_sec = addMenuSection("Custom Banlist");
addCategoryToSection(custom_sec, 'customs_category');
var misc_sec = addMenuSection("Misc");
misc_sec.append(
$('<button>Reset TPP filter settings</a>')
.click(function(){
if(confirm("This will reset all Twitch Chat Filter settings to their default values and will delete all custom banned phrases. Are you sure you want to continue?")){
forEach(TCF_SETTINGS_LIST, function(setting){
setting.reset();
});
}
})
);