diff --git a/chat_filter.user.js b/chat_filter.user.js index a646745..d64f752 100644 --- a/chat_filter.user.js +++ b/chat_filter.user.js @@ -106,6 +106,8 @@ var BANNED_WORDS = [ "imgur.com/4jlbxid.jpg" ]; +var CUSTOM_BANNED_PHRASES = localStorage.getItem("tpp-custom-filter-phrases") ? JSON.parse(localStorage.getItem("tpp-custom-filter-phrases")) : []; + var MINIMUM_DISTANCE_ERROR = 2; // Number of insertions / deletions / substitutions away from a blocked word. var MAXIMUM_NON_ASCII_CHARACTERS = 2; // For donger smilies, etc var MINIMUM_MESSAGE_WORDS = 2; // For Kappas and other short messages. @@ -227,6 +229,18 @@ function message_is_spam(message) { return false; } +function message_is_banned_by_user(message) { + message = message.toLowerCase(); + + for(var i=0; i < CUSTOM_BANNED_PHRASES.length; i++){ + if(message.indexOf(CUSTOM_BANNED_PHRASES[i]) != -1){ + return true; + } + } + + return false; +} + function is_whitelisted_url(url){ //This doesnt actually parse the URLs but it //should do the job when it comes to filtering. @@ -332,7 +346,14 @@ var filters = [ comment: 'Cyrillic characters', isActive: true, predicate: message_is_cyrillic - } + }, + + { name: 'TppFilterCustom', + comment: 'Add custom filter', + isActive: false, + predicate: message_is_banned_by_user + }, + ]; @@ -356,6 +377,15 @@ var stylers = [ }, ]; +//Text fields for custom user banned phrases +var text_fields = [ + { name: 'phrases', + comment: "Add a banned phrase", + element: CUSTOM_BANNED_PHRASES, + item_name: "phrase(s)", + }, +]; + function passes_active_filters(message){ for(var i=0; i < filters.length; i++){ var filter = filters[i]; @@ -386,7 +416,10 @@ function initialize_ui(){ var controlButton, controlPanel; var customCssParts = [ chatListSelector+" .TppFiltered {display:none;}", - chatListSelector+".allcaps_filtered "+chatMessageSelector+"{text-transform:lowercase;}" + chatListSelector+".allcaps_filtered "+chatMessageSelector+"{text-transform:lowercase;}", + ".custom_list_menu {background: #aaa; border:1px solid #000; position: absolute; right: 2px; bottom: 2px; padding: 10px; display: none;}", + "#chat_filter_dropmenu a {color: #00f;}", + ".tpp-custom-filter {position: relative;}", ]; if(NEW_TWITCH_CHAT){ @@ -456,18 +489,109 @@ function initialize_ui(){ .prop('checked', option.isActive); } + // Add an text field for custom filters + function add_text_field(section, option){ + //add required html + section + .append('
' + + ' '); + + //Add new banned item when user hits enter + $('#' + option.name) + .keyup(function(e){ + if(e.keyCode == 13 && $('#' + option.name).val().trim() != ""){ + add_item($('#' + option.name).val()); + $('#' + option.name).val(''); + } + }); + + //open the list of banned items + $('#show-' + option.name).click(function(e){ + e.preventDefault(); + $('#list-' + option.name).show(); + }); + + //close the list of banned items + $('#close-' + option.name).click(function(e){ + e.preventDefault(); + $('#list-' + option.name).hide(); + }); + + //empty the banned list completely + $('#clear-' + option.name).click(function(e){ + e.preventDefault(); + option.element.length = 0; + localStorage.setItem("tpp-custom-filter-" + option.name, JSON.stringify(option.element)); + update_chat_with_filter(); + $('#num-banned-' + option.name).text(option.element.length); + $('#list-' + option.name + ' .list-inner').empty(); + }); + + //add a new item to the banned items list + function add_item(new_word){ + if(option.element.indexOf(new_word) != -1){ return false; } + option.element.push(new_word); + localStorage.setItem("tpp-custom-filter-" + option.name, JSON.stringify(option.element)); + update_chat_with_filter(); + $('#num-banned-' + option.name).text(option.element.length); + add_item_to_ui(new_word); + } + + function add_item_to_ui(new_word){ + $('#list-' + option.name + ' .list-inner') + .append('' + new_word + ' [X]