2019-09-20 22:32:52 +02:00
//META{"name":"SendLargeMessages","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/SendLargeMessages","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/SendLargeMessages/SendLargeMessages.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class SendLargeMessages {
2019-01-17 23:48:29 +01:00
getName ( ) { return "SendLargeMessages" ; }
2019-12-18 11:49:24 +01:00
getVersion ( ) { return "1.5.7" ; }
2019-01-17 23:48:29 +01:00
getAuthor ( ) { return "DevilBro" ; }
getDescription ( ) { return "Opens a popout when your message is too large, which allows you to automatically send the message in several smaller messages." ; }
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor ( ) {
2019-02-28 20:09:44 +01:00
this . changelog = {
2019-12-18 11:49:24 +01:00
"fixed" : [ [ "Switching" , "Plugin acting weird after switching channels" ] , [ "New WYSIWYG Textarea" , "Fixed for the new WYSIWYG Textarea that is hidden by experiments" ] ] ,
2019-12-17 14:51:58 +01:00
"improved" : [ [ "Sending Messages" , "The plugin no longer needs the modal to send multiple messages, you can just write larger messages in the channel textarea and it will automatically split it up before sending it" ] ]
2019-02-28 20:09:44 +01:00
} ;
2019-09-04 12:34:02 +02:00
2019-11-14 17:56:26 +01:00
this . patchedModules = {
2019-12-17 14:51:58 +01:00
before : {
ChannelTextArea : "render"
} ,
2019-11-14 17:56:26 +01:00
after : {
2019-12-17 14:51:58 +01:00
ChannelTextArea : "render"
2019-11-14 17:56:26 +01:00
}
2018-12-27 08:57:16 +01:00
} ;
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor ( ) {
2018-10-11 10:21:26 +02:00
this . messageDelay = 1000 ; //changing at own risk, might result in bans or mutes
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . css = `
2019-01-24 13:37:08 +01:00
. $ { this . name } - modal textarea {
2019-11-11 11:11:33 +01:00
height : 50 vh ;
2018-10-11 10:21:26 +02:00
} ` ;
}
//legacy
load ( ) { }
start ( ) {
2019-02-04 09:13:15 +01:00
if ( ! global . BDFDB ) global . BDFDB = { myPlugins : { } } ;
if ( global . BDFDB && global . BDFDB . myPlugins && typeof global . BDFDB . myPlugins == "object" ) global . BDFDB . myPlugins [ this . getName ( ) ] = this ;
2019-05-26 13:55:26 +02:00
var libraryScript = document . querySelector ( 'head script#BDFDBLibraryScript' ) ;
if ( ! libraryScript || ( performance . now ( ) - libraryScript . getAttribute ( "date" ) ) > 600000 ) {
2018-10-11 10:21:26 +02:00
if ( libraryScript ) libraryScript . remove ( ) ;
libraryScript = document . createElement ( "script" ) ;
2019-05-26 13:55:26 +02:00
libraryScript . setAttribute ( "id" , "BDFDBLibraryScript" ) ;
2018-10-11 10:21:26 +02:00
libraryScript . setAttribute ( "type" , "text/javascript" ) ;
2019-10-18 10:56:41 +02:00
libraryScript . setAttribute ( "src" , "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js" ) ;
2019-01-17 23:48:29 +01:00
libraryScript . setAttribute ( "date" , performance . now ( ) ) ;
2019-05-26 13:55:26 +02:00
libraryScript . addEventListener ( "load" , ( ) => { this . initialize ( ) ; } ) ;
2018-10-11 10:21:26 +02:00
document . head . appendChild ( libraryScript ) ;
2019-05-26 13:55:26 +02:00
}
else if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
2019-11-01 10:27:07 +01:00
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
}
initialize ( ) {
2019-01-17 23:48:29 +01:00
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-01-22 11:05:54 +01:00
if ( this . started ) return ;
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . init ( this ) ;
2019-11-11 11:11:33 +01:00
2019-10-22 18:55:25 +02:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console . error ( ` %c[ ${ this . getName ( ) } ]%c ` , "color: #3a71c1; font-weight: 700;" , "" , "Fatal Error: Could not load BD functions!" ) ;
2018-10-11 10:21:26 +02:00
}
stop ( ) {
2019-01-26 22:45:19 +01:00
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-10-22 11:37:23 +02:00
this . stopping = true ;
2019-11-11 11:11:33 +01:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2019-10-22 18:55:25 +02:00
BDFDB . PluginUtils . clear ( this ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-11-11 11:11:33 +01:00
processChannelTextArea ( e ) {
2019-12-17 14:51:58 +01:00
if ( e . instance . props && e . instance . props . type && e . instance . props . type == BDFDB . DiscordConstants . TextareaTypes . NORMAL ) {
e . instance . props . shouldUploadLongMessages = false ;
if ( e . returnvalue ) {
if ( ! BDFDB . ModuleUtils . isPatched ( this , e . instance , "handleSubmit" ) ) BDFDB . ModuleUtils . patch ( this , e . instance , "handleSubmit" , { instead : e2 => {
2019-12-18 11:49:24 +01:00
let parsedLength = BDFDB . StringUtils . getParsedLength ( e2 . methodArguments [ 0 ] ) ;
2019-12-17 14:51:58 +01:00
if ( parsedLength > 2000 ) {
e2 . stopOriginalMethodCall ( ) ;
2019-12-18 11:49:24 +01:00
let messages = this . formatText ( e2 . methodArguments [ 0 ] , Math . sqrt ( Math . pow ( parsedLength - e2 . methodArguments [ 0 ] . length , 2 ) ) > Math . max ( parsedLength , e2 . methodArguments [ 0 ] . length ) / 20 ) ;
2019-12-17 14:51:58 +01:00
messages . filter ( n => n ) . forEach ( ( message , i ) => {
BDFDB . TimeUtils . timeout ( _ => {
e2 . originalMethod ( message ) ;
if ( i >= messages . length - 1 ) BDFDB . NotificationUtils . toast ( this . labels . toast _allsent _text , { type : "success" } ) ;
} , this . messageDelay * i ) ;
2019-11-11 11:11:33 +01:00
} ) ;
}
2019-12-17 14:51:58 +01:00
else e2 . callOriginalMethodAfterwards ( ) ;
} } , true ) ;
}
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-12-17 14:51:58 +01:00
formatText ( text , parse ) {
2019-06-20 10:07:17 +02:00
text = text . replace ( /\t/g , " " ) ;
2018-12-27 08:57:16 +01:00
let longwords = text . match ( /[\S]{1800,}/gm ) ;
2019-12-17 14:51:58 +01:00
if ( longwords ) for ( let longword of longwords ) {
2018-10-11 10:21:26 +02:00
let count1 = 0 ;
let shortwords = [ ] ;
2019-12-17 14:51:58 +01:00
longword . split ( "" ) . forEach ( c => {
if ( shortwords [ count1 ] && shortwords [ count1 ] . length >= 1800 ) count1 ++ ;
shortwords [ count1 ] = shortwords [ count1 ] ? shortwords [ count1 ] + c : c ;
2018-10-11 10:21:26 +02:00
} ) ;
text = text . replace ( longword , shortwords . join ( " " ) ) ;
}
2018-12-27 08:57:16 +01:00
let messages = [ ] ;
let count2 = 0 ;
2018-10-11 10:21:26 +02:00
text . split ( " " ) . forEach ( ( word ) => {
2019-12-17 14:51:58 +01:00
if ( messages [ count2 ] && ( parse ? BDFDB . StringUtils . getParsedLength ( messages [ count2 ] + "" + word ) : ( messages [ count2 ] + "" + word ) . length ) > 1900 ) count2 ++ ;
2018-10-11 10:21:26 +02:00
messages [ count2 ] = messages [ count2 ] ? messages [ count2 ] + " " + word : word ;
} ) ;
2019-01-26 22:45:19 +01:00
2018-12-27 08:57:16 +01:00
let insertCodeBlock = null , insertCodeLine = null ;
for ( let j = 0 ; j < messages . length ; j ++ ) {
2018-10-11 10:21:26 +02:00
if ( insertCodeBlock ) {
messages [ j ] = insertCodeBlock + messages [ j ] ;
insertCodeBlock = null ;
}
else if ( insertCodeLine ) {
messages [ j ] = insertCodeLine + messages [ j ] ;
insertCodeLine = null ;
}
2019-01-26 22:45:19 +01:00
2018-12-27 08:57:16 +01:00
let codeBlocks = messages [ j ] . match ( /`{3,}[\S]*\n|`{3,}/gm ) ;
let codeLines = messages [ j ] . match ( /[^`]{0,1}`{1,2}[^`]|[^`]`{1,2}[^`]{0,1}/gm ) ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
if ( codeBlocks && codeBlocks . length % 2 == 1 ) {
messages [ j ] = messages [ j ] + "```" ;
insertCodeBlock = codeBlocks [ codeBlocks . length - 1 ] + "\n" ;
}
else if ( codeLines && codeLines . length % 2 == 1 ) {
insertCodeLine = codeLines [ codeLines . length - 1 ] . replace ( /[^`]/g , "" ) ;
messages [ j ] = messages [ j ] + insertCodeLine ;
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
return messages ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
setLabelsByLanguage ( ) {
2019-10-22 19:38:25 +02:00
switch ( BDFDB . LanguageUtils . getLanguage ( ) . id ) {
2018-10-11 10:21:26 +02:00
case "hr" : //croatian
return {
toast _allsent _text : "Sve veliku poslane." ,
modal _messages _warning : "Nemojte slati previše veliku!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Pošalji veliku poruku:"
2018-10-11 10:21:26 +02:00
} ;
case "da" : //danish
return {
toast _allsent _text : "Alle beskeder sendes." ,
modal _messages _warning : "Send ikke for mange beskeder!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Send stor besked:"
2018-10-11 10:21:26 +02:00
} ;
case "de" : //german
return {
toast _allsent _text : "Alle Nachrichten versendet." ,
modal _messages _warning : "Schicke nicht zu viele Nachrichten!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Große Nachricht senden:"
2018-10-11 10:21:26 +02:00
} ;
case "es" : //spanish
return {
toast _allsent _text : "Todos los mensajes enviados." ,
modal _messages _warning : "¡No envíe demasiados mensajes!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Enviar mensaje grande:"
2018-10-11 10:21:26 +02:00
} ;
case "fr" : //french
return {
toast _allsent _text : "Tous les messages envoyés" ,
modal _messages _warning : "N'envoyez pas trop de messages!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Envoyer un gros message:"
2018-10-11 10:21:26 +02:00
} ;
case "it" : //italian
return {
toast _allsent _text : "Tutti i messaggi inviati." ,
modal _messages _warning : "Non inviare troppi messaggi!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Invia grande messaggio:"
2018-10-11 10:21:26 +02:00
} ;
case "nl" : //dutch
return {
toast _allsent _text : "Alle berichten verzonden." ,
modal _messages _warning : "Stuur niet te veel berichten!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Stuur een groot bericht:"
2018-10-11 10:21:26 +02:00
} ;
case "no" : //norwegian
return {
toast _allsent _text : "Alle meldinger sendt." ,
modal _messages _warning : "Ikke send for mange meldinger!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Send stor melding:"
2018-10-11 10:21:26 +02:00
} ;
case "pl" : //polish
return {
toast _allsent _text : "Wszystkie wiadomości zostały wysłane." ,
modal _messages _warning : "Nie wysyłaj zbyt wielu wiadomości!" ,
2019-09-11 12:14:43 +02:00
modal _header _text : "Wyślij dużą wiadomość:"
2018-10-11 10:21:26 +02:00
} ;
case "pt-BR" : //portuguese (brazil)
return {
toast _allsent _text : "Todas as mensagens enviadas." ,
modal _messages _warning : "Não envie muitas mensagens!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Enviar mensagem grande:"
2018-10-11 10:21:26 +02:00
} ;
case "fi" : //finnish
return {
toast _allsent _text : "Kaikki lähetetyt viestit." ,
modal _messages _warning : "Älä lähetä liian monta viestiä!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Lähetä suuri viesti:"
2018-10-11 10:21:26 +02:00
} ;
case "sv" : //swedish
return {
toast _allsent _text : "Alla meddelanden skickade." ,
modal _messages _warning : "Skicka inte för många meddelanden!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Skicka stort meddelande:"
2018-10-11 10:21:26 +02:00
} ;
case "tr" : //turkish
return {
toast _allsent _text : "Tüm mesajlar gönderildi." ,
modal _messages _warning : "Çok fazla mesaj göndermeyin!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Büyük mesaj gönder:"
2018-10-11 10:21:26 +02:00
} ;
case "cs" : //czech
return {
toast _allsent _text : "Všechny zprávy byly odeslány." ,
modal _messages _warning : "Neposílejte příliš mnoho zpráv!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Odeslat velkou zprávu:"
2018-10-11 10:21:26 +02:00
} ;
case "bg" : //bulgarian
return {
toast _allsent _text : "Всички изпратени съобщения." ,
modal _messages _warning : "Н е изпращайте твърде много съобщения!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Изпратете голямо съобщение:"
2018-10-11 10:21:26 +02:00
} ;
case "ru" : //russian
return {
toast _allsent _text : "В с е отправленные сообщения." ,
modal _messages _warning : "Н е отправляйте слишком много сообщений!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Отправить сообщение:"
2018-10-11 10:21:26 +02:00
} ;
case "uk" : //ukrainian
return {
toast _allsent _text : "В с і повідомлення надіслано." ,
modal _messages _warning : "Н е надсилайте надто багато повідомлень!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Надіслати велике повідомлення:"
2018-10-11 10:21:26 +02:00
} ;
case "ja" : //japanese
return {
toast _allsent _text : "すべてのメッセージが送信されました。" ,
modal _messages _warning : "あまりにも多くのメッセージを送信しないでください!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "大きなメッセージを送信する:"
2018-10-11 10:21:26 +02:00
} ;
case "zh-TW" : //chinese (traditional)
return {
toast _allsent _text : "發送的所有消息。" ,
modal _messages _warning : "不要發送太多信息!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "發送大信息:"
2018-10-11 10:21:26 +02:00
} ;
case "ko" : //korean
return {
toast _allsent _text : "모든 메시지가 전송되었습니다." ,
modal _messages _warning : "너무 많은 메시지를 보내지 마십시오!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "큰 메시지 보내기:"
2018-10-11 10:21:26 +02:00
} ;
default : //default: english
return {
toast _allsent _text : "All messages sent." ,
modal _messages _warning : "Do not send too many messages!" ,
2019-11-11 11:11:33 +01:00
modal _header _text : "Send large message:"
2018-10-11 10:21:26 +02:00
} ;
}
}
}