2020-02-27 08:44:03 +01:00
//META{"name":"ChatFilter","authorId":"278543574059057154","invite":"Jx3TjNS","donate":"https://www.paypal.me/MircoWittrien","patreon":"https://www.patreon.com/MircoWittrien","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/ChatFilter","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/ChatFilter/ChatFilter.plugin.js"}*//
2018-10-11 10:21:26 +02:00
2020-02-09 14:38:05 +01:00
var ChatFilter = ( _ => {
2020-07-02 11:43:05 +02:00
var oldBlockedMessages , oldCensoredMessages , words ;
2020-06-08 20:08:01 +02:00
var settings = { } , replaces = { } , configs = { } ;
2020-02-09 14:38:05 +01:00
return class ChatFilter {
getName ( ) { return "ChatFilter" ; }
2019-01-17 23:48:29 +01:00
2020-08-29 15:57:35 +02:00
getVersion ( ) { return "3.4.4" ; }
2019-01-17 23:48:29 +01:00
2020-02-09 14:38:05 +01:00
getAuthor ( ) { return "DevilBro" ; }
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
getDescription ( ) { return "Allows the user to censor words or block complete messages based on words in the chatwindow." ; }
2019-09-04 12:34:02 +02:00
2020-02-09 14:38:05 +01:00
constructor ( ) {
2020-08-15 14:24:48 +02:00
this . changelog = {
2020-08-29 15:57:35 +02:00
"improved" : [ [ "Spaces and RegExp" , "Now supports spaces and regexp" ] ]
2020-08-15 14:24:48 +02:00
} ;
2020-02-09 14:38:05 +01:00
this . patchedModules = {
after : {
2020-08-15 14:24:48 +02:00
Messages : "type" ,
2020-02-09 14:38:05 +01:00
Message : "default" ,
MessageContent : "type"
}
} ;
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
initConstructor ( ) {
this . css = `
$ { BDFDB . dotCN . message + BDFDB . dotCNS . _chatfilterblocked + BDFDB . dotCN . messagemarkup } {
color : $ { BDFDB . DiscordConstants . Colors . STATUS _RED } ;
}
` ;
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
this . defaults = {
configs : {
empty : { value : false , description : "Allows the replacevalue to be empty (ignoring the default)" } ,
case : { value : false , description : "Handle the wordvalue case sensitive" } ,
2020-08-29 15:57:35 +02:00
exact : { value : true , description : "Handle the wordvalue as an exact word and not as part of a word" } ,
regex : { value : false , description : "Handle the wordvalue as a RegExp string" }
2020-01-17 13:12:34 +01:00
} ,
2020-02-09 14:38:05 +01:00
replaces : {
2020-07-02 10:42:56 +02:00
blocked : { value : "~~BLOCKED~~" , description : "Default replaceword for blocked messages:" } ,
censored : { value : "$!%&%!&" , description : "Default replaceword for censored messages:" }
2020-01-17 13:12:34 +01:00
} ,
2020-02-09 14:38:05 +01:00
settings : {
2020-07-02 10:42:56 +02:00
addContextMenu : { value : true , description : "Add a contextmenu entry to faster add new blocked/censored words:" }
2020-02-09 14:38:05 +01:00
}
} ;
}
2018-10-11 10:21:26 +02:00
2020-02-09 14:38:05 +01:00
getSettingsPanel ( collapseStates = { } ) {
if ( ! window . BDFDB || typeof BDFDB != "object" || ! BDFDB . loaded || ! this . started ) return ;
2020-09-04 11:25:48 +02:00
settings = BDFDB . DataUtils . get ( this , "settings" ) ;
replaces = BDFDB . DataUtils . get ( this , "replaces" ) ;
configs = BDFDB . DataUtils . get ( this , "configs" ) ;
2020-03-28 07:55:39 +01:00
let settingsPanel , settingsItems = [ ] ;
2020-02-09 14:38:05 +01:00
2020-03-28 07:55:39 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2020-02-09 14:38:05 +01:00
title : "Settings" ,
collapseStates : collapseStates ,
children : Object . keys ( settings ) . map ( key => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
className : BDFDB . disCN . marginbottom8 ,
type : "Switch" ,
plugin : this ,
keys : [ "settings" , key ] ,
label : this . defaults . settings [ key ] . description ,
value : settings [ key ]
2020-08-29 15:57:35 +02:00
} ) ) . concat ( Object . keys ( replaces ) . map ( rType => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSaveItem , {
2020-02-09 14:38:05 +01:00
className : BDFDB . disCN . marginbottom8 ,
type : "TextInput" ,
plugin : this ,
2020-08-29 15:57:35 +02:00
keys : [ "replaces" , rType ] ,
label : this . defaults . replaces [ rType ] . description ,
value : replaces [ rType ] ,
placeholder : this . defaults . replaces [ rType ] . value
2020-02-09 14:38:05 +01:00
} ) ) )
} ) ) ;
let values = { wordvalue : "" , replacevalue : "" , choice : "blocked" } ;
2020-03-28 07:55:39 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2020-07-02 10:42:56 +02:00
title : ` Add new blocked/censored word ` ,
2020-02-09 14:38:05 +01:00
collapseStates : collapseStates ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Button" ,
2020-07-02 10:42:56 +02:00
label : "Pick a wordvalue and replacevalue:" ,
2020-02-09 14:38:05 +01:00
key : "ADDBUTTON" ,
disabled : ! Object . keys ( values ) . every ( valuename => values [ valuename ] ) ,
children : BDFDB . LanguageUtils . LanguageStrings . ADD ,
onClick : _ => {
this . saveWord ( values ) ;
2020-03-28 07:55:39 +01:00
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
2020-02-09 14:38:05 +01:00
}
} ) ,
this . createInputs ( values )
] . flat ( 10 ) . filter ( n => n )
} ) ) ;
2020-08-29 15:57:35 +02:00
for ( let rType in replaces ) if ( ! BDFDB . ObjectUtils . isEmpty ( words [ rType ] ) ) settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
title : ` Added ${ rType } words ` ,
2020-02-09 14:38:05 +01:00
collapseStates : collapseStates ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsList , {
settings : Object . keys ( this . defaults . configs ) ,
2020-08-29 15:57:35 +02:00
data : Object . keys ( words [ rType ] ) . map ( ( wordvalue , i ) => Object . assign ( { } , words [ rType ] [ wordvalue ] , {
2020-02-09 14:38:05 +01:00
key : wordvalue ,
label : wordvalue
} ) ) ,
renderLabel : data => BDFDB . ReactUtils . createElement ( "div" , {
style : { width : "100%" } ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
value : data . label ,
placeholder : data . label ,
size : BDFDB . LibraryComponents . TextInput . Sizes . MINI ,
maxLength : 100000000000000000000 ,
onChange : value => {
2020-08-29 15:57:35 +02:00
words [ rType ] [ value ] = words [ rType ] [ data . label ] ;
delete words [ rType ] [ data . label ] ;
2020-02-09 14:38:05 +01:00
data . label = value ;
BDFDB . DataUtils . save ( words , this , "words" ) ;
}
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
value : data . replace ,
placeholder : data . replace ,
size : BDFDB . LibraryComponents . TextInput . Sizes . MINI ,
maxLength : 100000000000000000000 ,
onChange : value => {
2020-08-29 15:57:35 +02:00
words [ rType ] [ data . label ] . replace = value ;
2020-02-09 14:38:05 +01:00
BDFDB . DataUtils . save ( words , this , "words" ) ;
}
} )
]
} ) ,
onCheckboxChange : ( value , instance ) => {
2020-08-29 15:57:35 +02:00
words [ rType ] [ instance . props . cardId ] [ instance . props . settingId ] = value ;
2020-02-09 14:38:05 +01:00
BDFDB . DataUtils . save ( words , this , "words" ) ;
} ,
onRemove : ( e , instance ) => {
2020-08-29 15:57:35 +02:00
delete words [ rType ] [ instance . props . cardId ] ;
2020-02-09 14:38:05 +01:00
BDFDB . DataUtils . save ( words , this , "words" ) ;
2020-03-28 07:55:39 +01:00
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
2020-02-09 14:38:05 +01:00
}
} )
} ) ) ;
2020-03-28 07:55:39 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2020-02-09 14:38:05 +01:00
title : "Remove All" ,
collapseStates : collapseStates ,
2020-08-29 15:57:35 +02:00
children : Object . keys ( replaces ) . map ( rType => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
2020-02-09 14:38:05 +01:00
type : "Button" ,
className : BDFDB . disCN . marginbottom8 ,
color : BDFDB . LibraryComponents . Button . Colors . RED ,
2020-08-29 15:57:35 +02:00
label : ` Remove all ${ rType } words ` ,
2020-02-09 14:38:05 +01:00
onClick : _ => {
2020-08-29 15:57:35 +02:00
BDFDB . ModalUtils . confirm ( this , ` Are you sure you want to remove all ${ rType } words? ` , _ => {
words [ rType ] = { } ;
BDFDB . DataUtils . remove ( this , "words" , rType ) ;
2020-03-28 07:55:39 +01:00
BDFDB . PluginUtils . refreshSettingsPanel ( this , settingsPanel , collapseStates ) ;
2020-02-09 14:38:05 +01:00
} ) ;
} ,
children : BDFDB . LanguageUtils . LanguageStrings . REMOVE
} ) )
} ) ) ;
2020-03-28 07:55:39 +01:00
settingsItems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . CollapseContainer , {
2020-02-09 14:38:05 +01:00
title : "Config Guide" ,
collapseStates : collapseStates ,
2020-08-29 15:57:35 +02:00
children : [
"Case: Will block/censor words while comparing lowercase/uppercase. apple => apple, not APPLE or AppLe" ,
"Not Case: Will block/censor words while ignoring lowercase/uppercase. apple => apple, APPLE and AppLe" ,
"Exact: Will block/censor words that are exactly the selected word. apple => apple, not applepie or pineapple" ,
"Not Exact: Will block/censor all words containing the selected word. apple => apple, applepie and pineapple" ,
"Empty: Ignores the default and set replace word and removes the word/message instead." ,
[
"Regex: Will treat the entered wordvalue as a regular expression. " ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Anchor , { href : "https://regexr.com/" , children : BDFDB . LanguageUtils . LanguageStrings . HELP + "?" } )
] ,
] . map ( string => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormText , {
2020-02-09 14:38:05 +01:00
type : BDFDB . LibraryComponents . FormComponents . FormTextTypes . DESCRIPTION ,
children : string
} ) )
} ) ) ;
2020-03-28 07:55:39 +01:00
return settingsPanel = BDFDB . PluginUtils . createSettingsPanel ( this , settingsItems ) ;
2020-02-09 14:38:05 +01:00
}
2018-10-11 10:21:26 +02:00
2020-04-11 19:32:58 +02:00
// Legacy
2020-08-21 16:17:47 +02:00
load ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) BDFDB . PluginUtils . load ( this ) ;
}
2020-02-09 14:38:05 +01:00
start ( ) {
if ( ! window . BDFDB ) window . BDFDB = { myPlugins : { } } ;
if ( window . BDFDB && window . BDFDB . myPlugins && typeof window . BDFDB . myPlugins == "object" ) window . BDFDB . myPlugins [ this . getName ( ) ] = this ;
let libraryScript = document . querySelector ( "head script#BDFDBLibraryScript" ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
libraryScript . addEventListener ( "load" , _ => { this . initialize ( ) ; } ) ;
document . head . appendChild ( libraryScript ) ;
}
else if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
this . startTimeout = setTimeout ( _ => {
try { return this . initialize ( ) ; }
catch ( err ) { console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not initiate plugin! " + err ) ; }
} , 30000 ) ;
2018-10-11 10:21:26 +02:00
}
2020-02-09 14:38:05 +01:00
initialize ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
if ( this . started ) return ;
BDFDB . PluginUtils . init ( this ) ;
words = BDFDB . DataUtils . load ( this , "words" ) ;
2020-08-29 15:57:35 +02:00
for ( let rType in this . defaults . replaces ) if ( ! BDFDB . ObjectUtils . is ( words [ rType ] ) ) words [ rType ] = { } ;
2020-02-09 14:38:05 +01:00
2020-06-08 20:07:08 +02:00
this . forceUpdateAll ( ) ;
2020-02-09 14:38:05 +01:00
}
else console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not load BD functions!" ) ;
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
stop ( ) {
if ( window . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
this . stopping = true ;
2020-06-08 20:07:08 +02:00
this . forceUpdateAll ( ) ;
2019-02-06 16:21:07 +01:00
2020-02-09 14:38:05 +01:00
BDFDB . PluginUtils . clear ( this ) ;
}
2018-10-11 10:21:26 +02:00
}
2019-10-22 11:37:23 +02:00
2020-04-11 19:32:58 +02:00
// Begin of own functions
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
onSettingsClosed ( ) {
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
2020-06-08 20:07:08 +02:00
this . forceUpdateAll ( ) ;
2020-02-09 14:38:05 +01:00
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
onNativeContextMenu ( e ) {
if ( e . instance . props . value && e . instance . props . value . trim ( ) ) {
2020-06-08 20:07:08 +02:00
if ( ( e . instance . props . type == "NATIVE_TEXT" || e . instance . props . type == "CHANNEL_TEXT_AREA" ) && settings . addContextMenu ) this . injectItem ( e , e . instance . props . value . trim ( ) ) ;
2020-02-09 14:38:05 +01:00
}
2019-12-10 13:59:44 +01:00
}
2020-02-09 14:38:05 +01:00
onSlateContextMenu ( e ) {
let text = document . getSelection ( ) . toString ( ) . trim ( ) ;
2020-06-08 20:07:08 +02:00
if ( text && settings . addContextMenu ) this . injectItem ( e , text ) ;
2019-02-06 16:21:07 +01:00
}
2020-02-09 14:38:05 +01:00
onMessageContextMenu ( e ) {
let text = document . getSelection ( ) . toString ( ) . trim ( ) ;
2020-06-08 20:07:08 +02:00
if ( text && settings . addContextMenu ) this . injectItem ( e , text ) ;
2020-02-09 14:38:05 +01:00
}
injectItem ( e , text ) {
2020-05-20 14:40:43 +02:00
let [ children , index ] = BDFDB . ContextMenuUtils . findItem ( e . returnvalue , { id : "devmode-copy-id" , group : true } ) ;
2020-05-20 11:55:46 +02:00
children . splice ( index > - 1 ? index : children . length , 0 , BDFDB . ContextMenuUtils . createItem ( BDFDB . LibraryComponents . MenuItems . MenuGroup , {
children : BDFDB . ContextMenuUtils . createItem ( BDFDB . LibraryComponents . MenuItems . MenuItem , {
2020-02-09 14:38:05 +01:00
label : "Add to ChatFilter" ,
2020-05-19 15:48:04 +02:00
id : BDFDB . ContextMenuUtils . createItemId ( this . name , "add-filter" ) ,
2020-02-09 14:38:05 +01:00
action : _ => {
this . openAddModal ( text . replace ( /\n/g , "\\n" ) . replace ( /\r/g , "\\r" ) . replace ( /\t/g , "\\t" ) ) ;
}
} )
} ) ) ;
}
2019-02-06 16:21:07 +01:00
2020-02-09 14:38:05 +01:00
processMessages ( e ) {
2020-08-15 14:24:48 +02:00
e . returnvalue . props . children . props . channelStream = [ ] . concat ( e . returnvalue . props . children . props . channelStream ) ;
for ( let i in e . returnvalue . props . children . props . channelStream ) {
let message = e . returnvalue . props . children . props . channelStream [ i ] . content ;
2020-07-02 11:43:05 +02:00
if ( message ) {
2020-08-15 14:24:48 +02:00
if ( BDFDB . ArrayUtils . is ( message . attachments ) ) this . checkMessage ( e . returnvalue . props . children . props . channelStream [ i ] , message ) ;
2020-07-02 11:43:05 +02:00
else if ( BDFDB . ArrayUtils . is ( message ) ) for ( let j in message ) {
let childMessage = message [ j ] . content ;
if ( childMessage && BDFDB . ArrayUtils . is ( childMessage . attachments ) ) this . checkMessage ( message [ j ] , childMessage ) ;
}
2020-01-17 13:12:34 +01:00
}
2020-07-02 11:43:05 +02:00
}
}
checkMessage ( stream , message ) {
let { blocked , censored , content } = this . parseMessage ( message ) ;
if ( blocked ) {
if ( ! oldBlockedMessages [ message . id ] ) oldBlockedMessages [ message . id ] = new BDFDB . DiscordObjects . Message ( message ) ;
stream . content . content = content ;
stream . content . embeds = [ ] ;
}
else if ( oldBlockedMessages [ message . id ] && Object . keys ( message ) . some ( key => ! BDFDB . equals ( oldBlockedMessages [ message . id ] [ key ] , message [ key ] ) ) ) {
stream . content . content = oldBlockedMessages [ message . id ] . content ;
stream . content . embeds = oldBlockedMessages [ message . id ] . embeds ;
delete oldBlockedMessages [ message . id ] ;
}
if ( censored ) {
if ( ! oldCensoredMessages [ message . id ] ) oldCensoredMessages [ message . id ] = new BDFDB . DiscordObjects . Message ( message ) ;
stream . content . content = content ;
}
else if ( oldCensoredMessages [ message . id ] && Object . keys ( message ) . some ( key => ! BDFDB . equals ( oldCensoredMessages [ message . id ] [ key ] , message [ key ] ) ) ) {
stream . content . content = oldCensoredMessages [ message . id ] . content ;
delete oldCensoredMessages [ message . id ] ;
2020-02-09 14:38:05 +01:00
}
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
processMessage ( e ) {
2020-09-11 19:31:36 +02:00
let message = BDFDB . ObjectUtils . get ( e , "instance.props.childrenMessageContent.props.message" ) ;
2020-02-09 14:38:05 +01:00
if ( message ) {
2020-07-02 11:43:05 +02:00
if ( oldBlockedMessages [ message . id ] ) e . returnvalue . props . className = BDFDB . DOMUtils . formatClassName ( e . returnvalue . props . className , BDFDB . disCN . _chatfilterblocked ) ;
else if ( oldCensoredMessages [ message . id ] ) e . returnvalue . props . className = BDFDB . DOMUtils . formatClassName ( e . returnvalue . props . className , BDFDB . disCN . _chatfiltercensored ) ;
2020-02-09 14:38:05 +01:00
}
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
processMessageContent ( e ) {
if ( e . instance . props . message ) {
2020-07-02 11:43:05 +02:00
if ( oldBlockedMessages [ e . instance . props . message . id ] ) e . returnvalue . props . children . push ( this . createStamp ( oldBlockedMessages [ e . instance . props . message . id ] . content , "blocked" ) ) ;
else if ( oldCensoredMessages [ e . instance . props . message . id ] ) e . returnvalue . props . children . push ( this . createStamp ( oldCensoredMessages [ e . instance . props . message . id ] . content , "censored" ) ) ;
2020-02-09 14:38:05 +01:00
}
}
createStamp ( tooltipText , label ) {
return BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TooltipContainer , {
text : tooltipText ,
tooltipConfig : { style : "max-width: 400px" } ,
children : BDFDB . ReactUtils . createElement ( "time" , {
className : BDFDB . DOMUtils . formatClassName ( BDFDB . disCN . messageedited , BDFDB . disCN [ ` _chatfilter ${ label } stamp ` ] ) ,
children : ` ( ${ label } ) `
} )
} ) ;
}
2019-01-26 22:45:19 +01:00
2020-07-02 11:43:05 +02:00
parseMessage ( message ) {
let blocked = false , censored = false , content = ( oldBlockedMessages [ message . id ] || oldCensoredMessages [ message . id ] || { } ) . content || message . content ;
if ( content && typeof content == "string" ) {
2020-02-09 14:38:05 +01:00
let blockedReplace ;
for ( let bWord in words . blocked ) {
blockedReplace = words . blocked [ bWord ] . empty ? "" : ( words . blocked [ bWord ] . replace || replaces . blocked ) ;
let reg = this . createReg ( bWord , words . blocked [ bWord ] ) ;
2020-08-29 15:57:35 +02:00
if ( words . blocked [ bWord ] . regex || bWord . indexOf ( " " ) > - 1 ) {
if ( this . testWord ( content , reg ) ) blocked = true ;
}
else for ( let word of content . replace ( /([\n\t\r])/g , " $1 " ) . split ( " " ) ) {
if ( this . testWord ( word , reg ) ) {
blocked = true ;
break ;
}
}
2018-10-11 10:21:26 +02:00
if ( blocked ) break ;
}
2020-02-09 14:38:05 +01:00
if ( blocked ) return { blocked , censored , content : blockedReplace } ;
2018-10-11 10:21:26 +02:00
else {
2020-08-29 15:57:35 +02:00
content = content . replace ( /([\n\t\r])/g , " $1 " ) ;
2020-02-09 14:38:05 +01:00
for ( let cWord in words . censored ) {
let censoredReplace = words . censored [ cWord ] . empty ? "" : ( words . censored [ cWord ] . replace || replaces . censored ) ;
let reg = this . createReg ( cWord , words . censored [ cWord ] ) ;
2020-08-29 15:57:35 +02:00
let newString = [ ] ;
if ( words . censored [ cWord ] . regex || cWord . indexOf ( " " ) > - 1 ) {
if ( this . testWord ( content , reg ) ) {
censored = true ;
newString = [ content . replace ( reg , censoredReplace ) ] ;
}
else newString = [ content ] ;
}
else for ( let word of content . split ( " " ) ) {
2020-02-13 11:25:13 +01:00
if ( this . testWord ( word , reg ) ) {
2018-10-23 15:18:15 +02:00
censored = true ;
2020-08-29 15:57:35 +02:00
newString . push ( censoredReplace ) ;
2018-10-11 10:21:26 +02:00
}
2020-08-29 15:57:35 +02:00
else newString . push ( word ) ;
}
content = newString . join ( " " ) ;
2018-10-11 10:21:26 +02:00
}
2020-08-29 15:57:35 +02:00
content = content . replace ( / ([\n\t\r]) /g , "$1" ) ;
2018-10-11 10:21:26 +02:00
}
}
2020-02-09 14:38:05 +01:00
return { blocked , censored , content } ;
2018-10-11 10:21:26 +02:00
}
2020-02-13 11:25:13 +01:00
testWord ( word , reg ) {
let nativeEmoji = BDFDB . LibraryModules . EmojiUtils . translateSurrogatesToInlineEmoji ( word ) ;
if ( nativeEmoji != word ) return this . regTest ( nativeEmoji , reg ) ;
else {
2020-02-13 12:42:47 +01:00
let customEmoji = ( /<a{0,1}(:.*:)[0-9]{7,}>/i . exec ( word ) || [ ] ) [ 1 ] ;
2020-02-13 11:25:13 +01:00
if ( customEmoji ) return this . regTest ( customEmoji , reg ) ;
else return this . regTest ( word , reg ) ;
}
}
regTest ( word , reg ) {
let wordWithoutSpecial = word . replace ( /[\?\¿\!\¡\.\"]/g , "" ) ;
return word && reg . test ( word ) || wordWithoutSpecial && reg . test ( wordWithoutSpecial ) ;
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
createReg ( word , config ) {
2020-08-29 15:57:35 +02:00
let escapedWord = config . regex ? word : BDFDB . StringUtils . regEscape ( word ) ;
return new RegExp ( BDFDB . StringUtils . htmlEscape ( config . exact ? "^" + escapedWord + "$" : escapedWord ) , ` ${ config . case ? "" : "i" } ${ config . exact ? "" : "g" } ` ) ;
2020-02-09 14:38:05 +01:00
}
2019-01-26 22:45:19 +01:00
2020-02-09 14:38:05 +01:00
openAddModal ( wordvalue ) {
let values = { wordvalue , replacevalue : "" , choice : "blocked" } ;
BDFDB . ModalUtils . open ( this , {
size : "MEDIUM" ,
header : "Add to ChatAliases" ,
subheader : "" ,
children : [
this . createInputs ( values ) ,
BDFDB . ArrayUtils . remove ( Object . keys ( this . defaults . configs ) , "file" ) . map ( key => BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Switch" ,
className : BDFDB . disCN . marginbottom8 + " input-config" + key ,
label : this . defaults . configs [ key ] . description ,
value : this . defaults . configs [ key ] . value
} ) )
] . flat ( 10 ) . filter ( n => n ) ,
buttons : [ {
key : "ADDBUTTON" ,
disabled : ! values . wordvalue ,
contents : BDFDB . LanguageUtils . LanguageStrings . ADD ,
color : "BRAND" ,
close : true ,
click : modal => {
let configs = { } ;
for ( let key in this . defaults . configs ) {
let configinput = modal . querySelector ( ` .input-config ${ key } ${ BDFDB . dotCN . switchinner } ` ) ;
if ( configinput ) configs [ key ] = configinput . checked ;
}
this . saveWord ( values , configs ) ;
2020-01-17 13:12:34 +01:00
}
2020-02-09 14:38:05 +01:00
} ]
} ) ;
}
createInputs ( values ) {
2020-07-02 10:42:56 +02:00
return [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormItem , {
title : "Block/Censor:" ,
className : BDFDB . disCN . marginbottom8 + " input-wordvalue" ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
key : "WORDVALUE" ,
value : values . wordvalue ,
placeholder : values . wordvalue ,
errorMessage : ! values . wordvalue && "Choose a wordvalue" || words [ values . choice ] [ values . wordvalue ] && ` Wordvalue already used, saving will overwrite old ${ values . choice } word ` ,
onChange : ( value , instance ) => {
values . wordvalue = value . trim ( ) ;
if ( ! values . wordvalue ) instance . props . errorMessage = "Choose a wordvalue" ;
else if ( words [ values . choice ] [ values . wordvalue ] ) instance . props . errorMessage = ` Wordvalue already used, saving will overwrite old ${ values . choice } word ` ;
else delete instance . props . errorMessage ;
let addButtonIns = BDFDB . ReactUtils . findOwner ( BDFDB . ReactUtils . findOwner ( instance , { name : [ "BDFDB_Modal" , "BDFDB_SettingsPanel" ] , up : true } ) , { key : "ADDBUTTON" } ) ;
if ( addButtonIns ) {
addButtonIns . props . disabled = ! values . wordvalue ;
BDFDB . ReactUtils . forceUpdate ( addButtonIns ) ;
}
}
} )
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormItem , {
title : "With:" ,
className : BDFDB . disCN . marginbottom8 + " input-replacevalue" ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . TextInput , {
value : values . replacevalue ,
placeholder : values . replacevalue ,
autoFocus : true ,
onChange : ( value , instance ) => {
values . replacevalue = value . trim ( ) ;
}
} )
} ) ,
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . RadioGroup , {
className : BDFDB . disCN . marginbottom8 ,
value : values . choice ,
options : [ { value : "blocked" , name : "Block" } , { value : "censored" , name : "Censor" } ] ,
2020-02-09 14:38:05 +01:00
onChange : ( value , instance ) => {
2020-07-02 10:42:56 +02:00
values . choice = value . value ;
let wordvalueInputIns = BDFDB . ReactUtils . findOwner ( BDFDB . ReactUtils . findOwner ( instance , { name : [ "BDFDB_Modal" , "BDFDB_SettingsPanel" ] , up : true } ) , { key : "WORDVALUE" } ) ;
if ( wordvalueInputIns ) {
if ( ! values . wordvalue ) wordvalueInputIns . props . errorMessage = "Choose a wordvalue" ;
else if ( words [ values . choice ] [ values . wordvalue ] ) wordvalueInputIns . props . errorMessage = ` Wordvalue already used, saving will overwrite old ${ values . choice } word ` ;
else delete wordvalueInputIns . props . errorMessage ;
BDFDB . ReactUtils . forceUpdate ( wordvalueInputIns ) ;
2020-02-09 14:38:05 +01:00
}
2020-01-17 13:12:34 +01:00
}
2020-02-09 14:38:05 +01:00
} )
2020-07-02 10:42:56 +02:00
] ;
2020-02-09 14:38:05 +01:00
}
2019-02-06 16:21:07 +01:00
2020-06-08 20:08:01 +02:00
saveWord ( values , wordConfigs = configs ) {
2020-02-09 14:38:05 +01:00
if ( ! values . wordvalue || ! values . choice ) return ;
values . wordvalue = values . wordvalue . trim ( ) ;
values . replacevalue = values . replacevalue . trim ( ) ;
if ( ! BDFDB . ObjectUtils . is ( words [ values . choice ] ) ) words [ values . choice ] = { } ;
words [ values . choice ] [ values . wordvalue ] = {
replace : values . replacevalue ,
2020-06-08 20:08:01 +02:00
empty : wordConfigs . empty ,
case : wordConfigs . case ,
exact : values . wordvalue . indexOf ( " " ) > - 1 ? false : wordConfigs . exact ,
2020-02-09 14:38:05 +01:00
regex : false
} ;
BDFDB . DataUtils . save ( words , this , "words" ) ;
}
2020-06-08 20:07:08 +02:00
forceUpdateAll ( ) {
settings = BDFDB . DataUtils . get ( this , "settings" ) ;
replaces = BDFDB . DataUtils . get ( this , "replaces" ) ;
2020-06-08 20:08:01 +02:00
configs = BDFDB . DataUtils . get ( this , "configs" ) ;
2020-06-08 20:07:08 +02:00
2020-07-02 11:43:05 +02:00
oldBlockedMessages = { } ;
oldCensoredMessages = { } ;
2020-06-08 20:07:08 +02:00
2020-09-11 19:31:36 +02:00
BDFDB . PatchUtils . forceAllUpdates ( this ) ;
2020-08-15 14:24:48 +02:00
BDFDB . MessageUtils . rerenderAll ( ) ;
2020-06-08 20:07:08 +02:00
}
2019-02-06 16:21:07 +01:00
}
2020-07-26 17:02:25 +02:00
} ) ( ) ;
module . exports = ChatFilter ;