2019-09-20 22:32:52 +02:00
//META{"name":"GoogleTranslateOption","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/GoogleTranslateOption","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/GoogleTranslateOption/GoogleTranslateOption.plugin.js"}*//
2018-10-11 10:21:26 +02:00
class GoogleTranslateOption {
2019-01-11 19:59:30 +01:00
getName ( ) { return "GoogleTranslateOption" ; }
2019-09-27 09:48:32 +02:00
getVersion ( ) { return "1.7.6" ; }
2019-01-26 22:45:19 +01:00
2019-04-08 15:21:20 +02:00
getAuthor ( ) { return "DevilBro" ; }
2019-01-11 19:59:30 +01:00
2019-04-08 15:21:20 +02:00
getDescription ( ) { return "Adds a Google Translate option to your context menu, which shows a preview of the translated text and on click will open the selected text in Google Translate. Also adds a translation button to your textareas, which will automatically translate the text for you before it is being send." ; }
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor ( ) {
2019-04-08 15:21:20 +02:00
this . changelog = {
2019-09-27 09:48:32 +02:00
"improved" : [ [ "Embeds" , "Translating a message now also translates the embed descriptions (usually the maintext of embeds)" ] ]
2019-04-08 15:21:20 +02:00
} ;
2019-09-04 12:34:02 +02:00
2019-01-02 23:28:11 +01:00
this . patchModules = {
2019-10-24 11:47:57 +02:00
"ChannelTextArea" : [ "componentDidMount" , "render" ] ,
2019-01-02 23:28:11 +01:00
"Message" : "componentDidMount" ,
2019-09-11 12:14:43 +02:00
"MessageContent" : "componentDidMount" ,
2019-01-04 21:56:36 +01:00
"StandardSidebarView" : "componentWillUnmount"
2019-01-02 23:28:11 +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 . languages = { } ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . doTranslate = false ;
this . translating = false ;
2019-09-04 12:34:02 +02:00
2019-04-16 00:30:33 +02:00
this . brailleConverter = {
"0" : "⠴" , "1" : "⠂" , "2" : "⠆" , "3" : "⠒" , "4" : "⠲" , "5" : "⠢" , "6" : "⠖" , "7" : "⠶" , "8" : "⠦" , "9" : "⠔" , "!" : "⠮" , "\"" : "⠐" , "#" : "⠼" , "$" : "⠫" , "%" : "⠩" , "&" : "⠯" , "'" : "⠄" , "(" : "⠷" , ")" : "⠾" , "*" : "⠡" , "+" : "⠬" , "," : "⠠" , "-" : "⠤" , "." : "⠨" , "/" : "⠌" , ":" : "⠱" , ";" : "⠰" , "<" : "⠣" , "=" : "⠿" , ">" : "⠜" , "?" : "⠹" , "@" : "⠈" , "a" : "⠁" , "b" : "⠃" , "c" : "⠉" , "d" : "⠙" , "e" : "⠑" , "f" : "⠋" , "g" : "⠛" , "h" : "⠓" , "i" : "⠊" , "j" : "⠚" , "k" : "⠅" , "l" : "⠇" , "m" : "⠍" , "n" : "⠝" , "o" : "⠕" , "p" : "⠏" , "q" : "⠟" , "r" : "⠗" , "s" : "⠎" , "t" : "⠞" , "u" : "⠥" , "v" : "⠧" , "w" : "⠺" , "x" : "⠭" , "y" : "⠽" , "z" : "⠵" , "[" : "⠪" , "\\" : "⠳" , "]" : "⠻" , "^" : "⠘" , "⠁" : "a" , "⠂" : "1" , "⠃" : "b" , "⠄" : "'" , "⠅" : "k" , "⠆" : "2" , "⠇" : "l" , "⠈" : "@" , "⠉" : "c" , "⠊" : "i" , "⠋" : "f" , "⠌" : "/" , "⠍" : "m" , "⠎" : "s" , "⠏" : "p" , "⠐" : "\"" , "⠑" : "e" , "⠒" : "3" , "⠓" : "h" , "⠔" : "9" , "⠕" : "o" , "⠖" : "6" , "⠗" : "r" , "⠘" : "^" , "⠙" : "d" , "⠚" : "j" , "⠛" : "g" , "⠜" : ">" , "⠝" : "n" , "⠞" : "t" , "⠟" : "q" , "⠠" : ", " , "⠡" : "*" , "⠢" : "5" , "⠣" : "<" , "⠤" : "-" , "⠥" : "u" , "⠦" : "8" , "⠧" : "v" , "⠨" : "." , "⠩" : "%" , "⠪" : "[" , "⠫" : "$" , "⠬" : "+" , "⠭" : "x" , "⠮" : "!" , "⠯" : "&" , "⠰" : ";" , "⠱" : ":" , "⠲" : "4" , "⠳" : "\\" , "⠴" : "0" , "⠵" : "z" , "⠶" : "7" , "⠷" : "(" , "⠸" : "_" , "⠹" : "?" , "⠺" : "w" , "⠻" : "]" , "⠼" : "#" , "⠽" : "y" , "⠾" : ")" , "⠿" : "=" , "_" : "⠸"
} ;
2019-09-04 12:34:02 +02:00
2019-04-16 00:30:33 +02:00
this . morseConverter = {
"0" : "− − − − − " , "1" : "·−−−−" , "2" : "··−−−" , "3" : "···−−" , "4" : "····−" , "5" : "·····" , "6" : "−····" , "7" : "−−···" , "8" : "−−−··" , "9" : "−−−−·" , "!" : "−·−·−−" , "\"" : "·−··−·" , "$" : "···−··−" , "&" : "·−···" , "'" : "·−−−−·" , "(" : "−·−−·" , ")" : "−·−−·−" , "+" : "·−·−·" , "," : "−−··−−" , "-" : "−····−" , "." : "·−·−·−" , "/" : "−··−·" , ":" : "−−−···" , ";" : "−·−·−·" , "=" : "−···−" , "?" : "··−−··" , "@" : "·−−·−·" , "a" : "·−" , "b" : "−···" , "c" : "−·−·" , "d" : "−··" , "e" : "·" , "f" : "··−·" , "g" : "−−·" , "h" : "····" , "i" : "··" , "j" : "·−−−" , "k" : "−·−" , "l" : "·−··" , "m" : "− − " , "n" : "−·" , "o" : "− − − " , "p" : "·−−·" , "q" : "−−·−" , "r" : "·−·" , "s" : "···" , "t" : "− " , "u" : "··−" , "v" : "···−" , "w" : "·−−" , "x" : "−··−" , "y" : "−·−−" , "z" : "−−··" , "·" : "e" , "··" : "i" , "···" : "s" , "····" : "h" , "·····" : "5" , "····−" : "4" , "···−" : "v" , "···−··−" : "$" , "···−−" : "3" , "··−" : "u" , "··−·" : "f" , "··−−··" : "?" , "··−−·−" : "_" , "··−−−" : "2" , "·−" : "a" , "·−·" : "r" , "·−··" : "l" , "·−···" : "&" , "·−··−·" : "\"" , "·−·−·" : "+" , "·−·−·−" : "." , "·−−" : "w" , "·−−·" : "p" , "·−−·−·" : "@" , "·−−−" : "j" , "·−−−−" : "1" , "·−−−−·" : "'" , "− " : "t" , "−·" : "n" , "−··" : "d" , "−···" : "b" , "−····" : "6" , "−····−" : "-" , "−···−" : "=" , "−··−" : "x" , "−··−·" : "/" , "−·−" : "k" , "−·−·" : "c" , "−·−·−·" : ";" , "−·−·−−" : "!" , "−·−−" : "y" , "−·−−·" : "(" , "−·−−·−" : ")" , "− − " : "m" , "−−·" : "g" , "−−··" : "z" , "−−···" : "7" , "−−··−−" : "," , "−−·−" : "q" , "− − − " : "o" , "−−−··" : "8" , "−−−···" : ":" , "−−−−·" : "9" , "− − − − − " : "0" , "_" : "··−−·−"
} ;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this . defaults = {
settings : {
2019-10-24 11:47:57 +02:00
addTranslateButton : { value : true , description : "Adds an translate button to the chatbar." } ,
sendOriginalMessage : { value : false , description : "Send the original message together with the translation." }
2018-10-11 10:21:26 +02:00
} ,
choices : {
2019-10-24 11:47:57 +02:00
inputContext : { value : "auto" , direction : "input" , place : "Context" , description : "Input Language in selected Messages:" } ,
outputContext : { value : "$discord" , direction : "output" , place : "Context" , description : "Output Language in selected Messages:" } ,
inputMessage : { value : "auto" , direction : "input" , place : "Message" , description : "Input Language in your Message:" } ,
outputMessage : { value : "$discord" , direction : "output" , place : "Message" , description : "Output Language in your Message:" }
2018-10-11 10:21:26 +02:00
}
} ;
this . css = `
2019-10-24 11:47:57 +02:00
. translate - button . translating - active $ { BDFDB . dotCN . textareaicon } {
2018-12-11 12:55:15 +01:00
color : # F04747 ! important ;
2018-10-11 10:21:26 +02:00
}
. reverse - button {
2019-10-24 11:47:57 +02:00
margin - right : 5 px ;
2018-10-11 10:21:26 +02:00
opacity : 0.2 ;
transition : all 200 ms ease ;
}
$ { BDFDB . dotCN . themedark } . reverse - button {
fill : # fff ;
}
$ { BDFDB . dotCN . themelight } . reverse - button {
fill : # 4 f545c ;
}
. reverse - button : hover {
cursor : pointer ;
opacity : 1 ;
}
2019-09-27 09:48:32 +02:00
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messagebody + BDFDB . dotCN . messagemarkup } ,
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messageaccessory + BDFDB . dotCN . embeddescription } {
2019-08-28 14:02:30 +02:00
font - size : 0 ! important ;
2019-09-27 09:48:32 +02:00
line - height : 0 ! important ;
2019-08-28 14:02:30 +02:00
}
2019-09-11 12:14:43 +02:00
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messagebody + BDFDB . dotCN . messagemarkup } > . GTO - translation {
font - size : 1 rem ! important ;
2019-09-27 09:48:32 +02:00
line - height : 1.375 ! important ;
2019-08-28 14:02:30 +02:00
}
2019-09-27 09:48:32 +02:00
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messageaccessory + BDFDB . dotCN . embeddescription } > . GTO - translation {
font - size : 0.875 rem ! important ;
line - height : 1 rem ! important ;
}
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messagebody + BDFDB . dotCN . messagemarkup } > : not ( . GTO - translation ) $ { BDFDB . notCN . messageheadercompact + BDFDB . notCN . messageedited } ,
$ { BDFDB . dotCN . messagegroup } . GTO - translated - message $ { BDFDB . dotCNS . messageaccessory + BDFDB . dotCN . embeddescription } > : not ( . GTO - translation ) $ { BDFDB . notCN . messageedited } {
2019-08-28 14:02:30 +02:00
display : none ! important ;
2018-10-11 10:21:26 +02:00
} ` ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getSettingsPanel ( ) {
2019-01-22 11:28:32 +01:00
if ( ! global . BDFDB || typeof BDFDB != "object" || ! BDFDB . loaded || ! this . started ) return ;
2019-10-24 11:47:57 +02:00
let settings = BDFDB . DataUtils . get ( this , "settings" ) ;
let settingsitems = [ ] ;
settingsitems = settingsitems . concat ( this . createSelects ( false ) ) ;
for ( let key in settings ) settingsitems . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsSwitch , {
className : BDFDB . disCN . marginbottom8 ,
plugin : this ,
keys : [ "settings" , key ] ,
label : this . defaults . settings [ key ] . description ,
value : settings [ key ]
} ) ) ;
return BDFDB . PluginUtils . createSettingsPanel ( this , settingsitems ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
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-01-17 23:48:29 +01:00
else if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) this . initialize ( ) ;
2018-10-11 10:21:26 +02:00
this . startTimeout = setTimeout ( ( ) => { this . initialize ( ) ; } , 30000 ) ;
}
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-01-26 22:45:19 +01:00
2019-09-24 11:00:04 +02:00
this . setLanguages ( ) ;
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB . ModuleUtils . forceAllUpdates ( this ) ;
2018-10-11 10:21:26 +02:00
}
2019-10-22 18:55:25 +02: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-17 23:48:29 +01:00
if ( global . BDFDB && typeof BDFDB === "object" && BDFDB . loaded ) {
2019-10-22 11:37:23 +02:00
this . stopping = true ;
2019-09-11 12:14:43 +02:00
document . querySelectorAll ( BDFDB . dotCN . messagegroup + " .GTO-translated-message" ) . forEach ( message => {
2018-10-11 10:21:26 +02:00
this . resetMessage ( message ) ;
} ) ;
2019-10-24 11:47:57 +02:00
BDFDB . ModuleUtils . forceAllUpdates ( this , "ChannelTextArea" ) ;
2019-01-26 22:45:19 +01:00
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-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
onMessageContextMenu ( instance , menu , returnvalue ) {
if ( instance . props && instance . props . message && instance . props . channel && instance . props . target && ! menu . querySelector ( ` ${ this . name } -contextMenuItem ` ) ) {
2019-01-02 23:28:11 +01:00
let { messagediv , pos } = this . getMessageAndPos ( instance . props . target ) ;
if ( ! messagediv || pos == - 1 ) return ;
2019-10-23 11:10:01 +02:00
let translated = BDFDB . DOMUtils . containsClass ( messagediv , "GTO-translated-message" ) ;
2019-10-22 19:38:25 +02:00
let [ children , index ] = BDFDB . ReactUtils . findChildren ( returnvalue , { name : "MessagePinItem" } ) ;
2019-10-22 18:55:25 +02:00
const translateUntranslateItem = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItem , {
2019-09-11 12:14:43 +02:00
label : translated ? this . labels . context _messageuntranslateoption _text : this . labels . context _messagetranslateoption _text ,
2019-10-23 11:10:01 +02:00
hint : BDFDB . BDUtils . isPluginEnabled ( "MessageUtilities" ) ? BDFDB . BDUtils . getPlugin ( "MessageUtilities" ) . getActiveShortcutString ( "__Translate_Message" ) : null ,
2019-09-11 12:14:43 +02:00
className : ` BDFDB-contextMenuItem ${ this . name } -contextMenuItem ${ this . name } - ${ translated ? "untranslate" : "translate" } -contextMenuItem ` ,
action : e => {
BDFDB . closeContextMenu ( menu ) ;
this . translateMessage ( instance . props . message , instance . props . target , instance . props . channel ) ;
}
2019-01-11 19:59:30 +01:00
} ) ;
2019-09-11 12:14:43 +02:00
if ( index > - 1 ) children . splice ( index , 0 , translateUntranslateItem ) ;
else children . push ( translateUntranslateItem ) ;
2019-01-02 23:28:11 +01:00
let text = document . getSelection ( ) . toString ( ) ;
2019-01-11 19:59:30 +01:00
if ( text ) {
2019-10-23 11:10:01 +02:00
let GSRstring = BDFDB . ReactUtils . getValue ( BDFDB . BDUtils . getPlugin ( "GoogleSearchReplace" , true ) , "labels.context_googlesearchreplace_text" ) ;
2019-10-22 19:38:25 +02:00
let [ children2 , index2 ] = BDFDB . ReactUtils . findChildren ( returnvalue , { name : "SearchWithGoogle" , props : GSRstring ? [ [ "label" , GSRstring ] ] : null } ) ;
2019-09-11 12:14:43 +02:00
var foundtranslation , foundinput , foundoutput ;
2019-10-22 18:55:25 +02:00
const searchTranslationItem = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItem , {
2019-09-11 12:14:43 +02:00
label : this . labels . context _googletranslateoption _text ,
className : ` BDFDB-contextMenuItem ${ this . name } -contextMenuItem ${ this . name } -searchtranslation-contextMenuItem ` ,
action : e => {
2019-10-23 11:10:01 +02:00
var item = BDFDB . DOMUtils . getParent ( BDFDB . dotCN . contextmenuitem , e . target ) ;
2019-09-11 12:14:43 +02:00
if ( item ) {
var createTooltip = ( ) => {
2019-10-22 18:55:25 +02:00
BDFDB . TooltipUtils . create ( item , ` From ${ foundinput . name } : \n ${ text } \n \n To ${ foundoutput . name } : \n ${ foundtranslation } ` , { type : "right" , selector : "googletranslate-tooltip" } ) ;
2019-09-11 12:14:43 +02:00
} ;
if ( foundtranslation && foundinput && foundoutput ) {
if ( document . querySelector ( ".googletranslate-tooltip" ) ) {
2019-03-01 21:26:41 +01:00
BDFDB . closeContextMenu ( menu ) ;
2019-09-11 12:14:43 +02:00
window . open ( this . getGoogleTranslatePageURL ( foundinput . id , foundoutput . id , text ) , "_blank" ) ;
}
else createTooltip ( ) ;
2019-01-02 23:28:11 +01:00
}
2019-09-11 12:14:43 +02:00
else this . translateText ( text , "context" , ( translation , input , output ) => {
if ( translation ) {
foundtranslation = translation , foundinput = input , foundoutput = output ;
createTooltip ( ) ;
}
} ) ;
}
}
} ) ;
if ( index2 > - 1 ) children2 . splice ( index2 , 0 , searchTranslationItem ) ;
else children2 . push ( searchTranslationItem ) ;
2018-10-11 10:21:26 +02:00
}
}
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
onMessageOptionPopout ( instance , popout , returnvalue ) {
if ( instance . props . message && instance . props . channel && instance . props . target && ! popout . querySelector ( ` ${ this . name } -popoutMenuItem ` ) ) {
let { messagediv , pos } = this . getMessageAndPos ( instance . props . target ) ;
if ( ! messagediv || pos == - 1 ) return ;
2019-10-23 11:10:01 +02:00
let translated = BDFDB . DOMUtils . containsClass ( messagediv , "GTO-translated-message" ) ;
2019-10-22 19:38:25 +02:00
let [ children , index ] = BDFDB . ReactUtils . findChildren ( returnvalue , { props : [ [ "label" , [ BDFDB . LanguageUtils . LanguageStrings . PIN , BDFDB . LanguageUtils . LanguageStrings . UNPIN ] ] ] } ) ;
2019-10-22 18:55:25 +02:00
const translateUntranslateItem = BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ContextMenuItem , {
2019-09-11 12:14:43 +02:00
label : this . labels [ translated ? "popout_untranslateoption_text" : "popout_translateoption_text" ] ,
className : ` ${ BDFDB . disCN . optionpopoutitem } BDFDB-popoutMenuItem ${ this . name } -popoutMenuItem ${ this . name } - ${ translated ? "untranslate" : "translate" } -popoutMenuItem ` ,
action : e => {
this . translateMessage ( instance . props . message , instance . props . target , instance . props . channel ) ;
instance . props . onClose ( ) ;
}
} ) ;
children . splice ( index + 1 , 0 , translateUntranslateItem ) ;
}
}
2019-10-24 11:47:57 +02:00
processChannelTextArea ( instance , wrapper , returnvalue , methodnames ) {
if ( instance . props . type != "normal" || instance . props . disabled ) return ;
if ( methodnames . includes ( "componentDidMount" ) && wrapper ) {
let textarea = wrapper . querySelector ( BDFDB . dotCN . textarea ) ;
2019-01-02 23:28:11 +01:00
if ( textarea ) {
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . add ( this , textarea , "input" , ( ) => {
2019-01-11 19:59:30 +01:00
if ( this . doTranslate ) {
this . doTranslate = false ;
if ( document . activeElement == textarea ) {
var text = textarea . value ;
textarea . focus ( ) ;
textarea . selectionStart = 0 ;
textarea . selectionEnd = text . length ;
document . execCommand ( "insertText" , false , "" ) ;
this . translateText ( text , "message" , ( translation , input , output ) => {
2019-10-22 23:04:35 +02:00
translation = ! translation ? text : ( BDFDB . DataUtils . get ( this , "settings" , "sendOriginalMessage" ) ? text + "\n\n" + translation : translation ) ;
2018-10-11 10:21:26 +02:00
textarea . focus ( ) ;
2019-01-11 19:59:30 +01:00
document . execCommand ( "insertText" , false , translation + " " ) ;
BDFDB . triggerSend ( textarea ) ;
} ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-11 19:59:30 +01:00
}
} ) ;
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . add ( this , textarea , "keydown" , e => {
2019-01-11 19:59:30 +01:00
if ( textarea . value && this . translating && ! e . shiftKey && e . which == 13 && ! wrapper . querySelector ( BDFDB . dotCN . autocomplete ) ) {
this . doTranslate = true ;
textarea . dispatchEvent ( new Event ( "input" ) ) ;
}
} ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-10-24 11:47:57 +02:00
else if ( methodnames . includes ( "render" ) ) {
let [ children , index ] = BDFDB . ReactUtils . findChildren ( returnvalue , { props : [ [ "className" , BDFDB . disCN . textareapickerbuttons ] ] } ) ;
if ( ! this . stopping && index > - 1 && children [ index ] . props && children [ index ] . props . children ) {
children [ index ] . props . children . unshift ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . PopoutContainer , {
2019-10-24 14:17:33 +02:00
className : BDFDB . disCN . textareapickerbuttoncontainer ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . ChannelTextAreaButton , {
className : [ "translate-button" , this . translating ? "translating-active" : null , BDFDB . disCN . textareapickerbutton ] . filter ( n => n ) . join ( " " ) ,
iconSVG : ` <svg version="1.1" xmlns="http://www.w3.org/2000/svg" class=" ${ BDFDB . disCN . textareaicon } " viewBox="0 0 22 22" fill="currentColor"><path d="M 19.794, 3.299 H 9.765 L 8.797, 0 h -6.598 C 0.99, 0, 0, 0.99, 0, 2.199 V 16.495 c 0, 1.21, 0.99, 2.199, 2.199, 2.199 H 9.897 l 1.1, 3.299 H 19.794 c 1.21, 0, 2.199 -0.99, 2.199 -2.199 V 5.498 C 21.993, 4.289, 21.003, 3.299, 19.794, 3.299 z M 5.68, 13.839 c -2.48, 0 -4.492 -2.018 -4.492 -4.492 s 2.018 -4.492, 4.492 -4.492 c 1.144, 0, 2.183, 0.407, 3.008, 1.171 l 0.071, 0.071 l -1.342, 1.298 l -0.066 -0.06 c -0.313 -0.297 -0.858 -0.643 -1.671 -0.643 c -1.441, 0 -2.612, 1.193 -2.612, 2.661 c 0, 1.468, 1.171, 2.661, 2.612, 2.661 c 1.507, 0, 2.161 -0.962, 2.337 -1.606 h -2.43 v -1.704 h 4.344 l 0.016, 0.077 c 0.044, 0.231, 0.06, 0.434, 0.06, 0.665 C 10.001, 12.036, 8.225, 13.839, 5.68, 13.839 z M 11.739, 9.979 h 4.393 c 0, 0 -0.374, 1.446 -1.715, 3.008 c -0.588 -0.676 -0.995 -1.336 -1.254 -1.864 h -1.089 L 11.739, 9.979 z M 13.625, 13.839 l -0.588, 0.583 l -0.72 -2.452 C 12.685, 12.63, 13.13, 13.262, 13.625, 13.839 z M 20.893, 19.794 c 0, 0.605 -0.495, 1.1 -1.1, 1.1 H 12.096 l 2.199 -2.199 l -0.896 -3.041 l 1.012 -1.012 l 2.953, 2.953 l 0.803 -0.803 l -2.975 -2.953 c 0.99 -1.138, 1.759 -2.474, 2.106 -3.854 h 1.397 V 8.841 H 14.697 v -1.144 h -1.144 v 1.144 H 11.398 l -1.309 -4.443 H 19.794 c 0.605, 0, 1.1, 0.495, 1.1, 1.1 V 19.794 z"/></svg> `
} ) ,
width : 400 ,
padding : 10 ,
animation : BDFDB . LibraryComponents . PopoutContainer . Animation . SCALE ,
position : BDFDB . LibraryComponents . PopoutContainer . Positions . TOP ,
align : BDFDB . LibraryComponents . PopoutContainer . Align . RIGHT ,
2019-10-24 11:47:57 +02:00
onClose : instance => {
let channelTextareaButtonIns = BDFDB . ReactUtils . findOwner ( instance , { name : "ChannelTextAreaButton" } ) ;
if ( channelTextareaButtonIns ) {
channelTextareaButtonIns . props . isActive = false ;
BDFDB . ReactUtils . forceUpdate ( channelTextareaButtonIns ) ;
}
} ,
onContextMenu : _ => {
this . translating = ! this . translating ;
let channelTextareaButtonIns = BDFDB . ReactUtils . findOwner ( instance , { name : "ChannelTextAreaButton" } ) ;
if ( channelTextareaButtonIns ) {
channelTextareaButtonIns . props . className = [ "translate-button" , this . translating ? "translating-active" : null , BDFDB . disCN . textareapickerbutton ] . filter ( n => n ) . join ( " " ) ;
BDFDB . ReactUtils . forceUpdate ( channelTextareaButtonIns ) ;
}
} ,
renderPopout : instance => {
let channelTextareaButtonIns = BDFDB . ReactUtils . findOwner ( instance , { name : "ChannelTextAreaButton" } ) ;
if ( channelTextareaButtonIns ) {
channelTextareaButtonIns . props . isActive = true ;
BDFDB . ReactUtils . forceUpdate ( channelTextareaButtonIns ) ;
}
let popoutelements = [ ] ;
popoutelements . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex , {
className : BDFDB . disCNS . margintop8 + BDFDB . disCN . marginbottom8 ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsLabel , {
label : ` Words starting with "!" will be ignored `
} )
} ) ) ;
popoutelements . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormDivider , {
className : BDFDB . disCN . marginbottom8
} ) ) ,
popoutelements = popoutelements . concat ( this . createSelects ( true ) ) ;
popoutelements . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SettingsItem , {
type : "Switch" ,
className : BDFDB . disCN . marginbottom20 ,
label : "Translate:" ,
value : this . translating ,
onChange : value => {
this . translating = value ;
if ( channelTextareaButtonIns ) {
channelTextareaButtonIns . props . className = [ "translate-button" , this . translating ? "translating-active" : null , BDFDB . disCN . textareapickerbutton ] . filter ( n => n ) . join ( " " ) ;
BDFDB . ReactUtils . forceUpdate ( channelTextareaButtonIns ) ;
}
}
} ) ) ;
return popoutelements ;
2019-10-24 14:17:33 +02:00
}
2019-10-24 11:47:57 +02:00
} ) ) ;
}
}
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processMessage ( instance , wrapper , returnvalue ) {
2019-10-22 18:55:25 +02:00
if ( instance . props && typeof instance . props . renderButtons == "function" && ! wrapper . querySelector ( BDFDB . dotCN . optionpopoutbutton ) && BDFDB . ReactUtils . getValue ( instance , "props.message.author.id" ) != 1 ) {
2019-01-02 23:28:11 +01:00
let buttonwrap = wrapper . querySelector ( BDFDB . dotCN . messagebuttoncontainer ) ;
if ( buttonwrap ) {
2019-10-23 11:10:01 +02:00
let optionPopoutButton = BDFDB . DOMUtils . create ( ` <div tabindex="0" class=" ${ BDFDB . disCN . optionpopoutbutton } " aria-label="More Options" role="button"><svg name="OverflowMenu" class=" ${ BDFDB . disCN . optionpopoutbuttonicon } " aria-hidden="false" width="24" height="24" viewBox="0 0 24 24"><g fill="none" fill-rule="evenodd"><path d="M24 0v24H0V0z"></path><path fill="currentColor" d="M12 16c1.1045695 0 2 .8954305 2 2s-.8954305 2-2 2-2-.8954305-2-2 .8954305-2 2-2zm0-6c1.1045695 0 2 .8954305 2 2s-.8954305 2-2 2-2-.8954305-2-2 .8954305-2 2-2zm0-6c1.1045695 0 2 .8954305 2 2s-.8954305 2-2 2-2-.8954305-2-2 .8954305-2 2-2z"></path></g></svg></div> ` ) ;
2019-01-11 19:59:30 +01:00
optionPopoutButton . addEventListener ( "click" , ( ) => { BDFDB . createMessageOptionPopout ( optionPopoutButton ) ; } ) ;
buttonwrap . appendChild ( optionPopoutButton ) ;
2018-10-11 10:21:26 +02:00
}
}
}
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
processMessageContent ( instance , wrapper , returnvalue ) {
if ( instance . props && instance . props . message && instance . props . channel ) {
2019-10-23 11:10:01 +02:00
let messagediv = BDFDB . DOMUtils . getParent ( ".GTO-translated-message" , wrapper ) ;
if ( messagediv && ! wrapper . querySelector ( ".GTO-translation" ) ) BDFDB . DOMUtils . removeClass ( messagediv , "GTO-translated-message" ) ;
2019-01-02 23:28:11 +01:00
}
}
2019-10-24 11:47:57 +02:00
processStandardSidebarView ( instance , wrapper , returnvalue ) {
if ( this . SettingsUpdated ) {
delete this . SettingsUpdated ;
this . setLanguages ( ) ;
BDFDB . ModuleUtils . forceAllUpdates ( this , "ChannelTextArea" ) ;
}
}
createSelects ( inPopout ) {
let selects = [ ] ;
for ( let key in this . defaults . choices ) {
let isOutput = this . defaults . choices [ key ] . direction == "output" ;
selects . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormItem , {
title : this . defaults . choices [ key ] . description ,
titlechildren : isOutput ? BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Button , {
look : BDFDB . LibraryComponents . Button . Looks . BLANK ,
size : BDFDB . LibraryComponents . Button . Sizes . NONE ,
onClick : e => {
let place = this . defaults . choices [ key ] . place ;
let input = this . getLanguageChoice ( "output" , place ) ;
let output = this . getLanguageChoice ( "input" , place ) ;
output = output == "auto" ? "en" : output ;
BDFDB . DataUtils . save ( input , this , "choices" , "input" + place ) ;
BDFDB . DataUtils . save ( output , this , "choices" , "output" + place ) ;
let containerIns = BDFDB . ReactUtils . findOwner ( e . _targetInst , { name : [ "BDFDB_Popout" , "BDFDB_SettingsPanel" ] , up : true } ) ;
if ( containerIns ) {
let allSelectIns = BDFDB . ReactUtils . findOwner ( containerIns , { name : "BDFDB_Select" , all : true , noCopies : true } ) ;
for ( let i in allSelectIns ) {
let selectIns = allSelectIns [ i ] ;
if ( selectIns && selectIns . props && selectIns . props . id && this . defaults . choices [ selectIns . props . id ] . place == place ) {
selectIns . props . value = this . defaults . choices [ selectIns . props . id ] . direction == "input" ? output : input ;
BDFDB . ReactUtils . forceUpdate ( selectIns ) ;
}
}
}
this . setLanguages ( ) ;
} ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . SvgIcon , {
iconSVG : ` <svg class="reverse-button" version="1.1" xmlns="http://www.w3.org/2000/svg" width="21" height="21" fill="currentColor"><path d="M 0, 10.515 c 0, 2.892, 1.183, 5.521, 3.155, 7.361 L 0, 21.031 h 7.887 V 13.144 l -2.892, 2.892 C 3.549, 14.722, 2.629, 12.75, 2.629, 10.515 c 0 -3.418, 2.235 -6.309, 5.258 -7.492 v -2.629 C 3.418, 1.577, 0, 5.652, 0, 10.515 z M 21.031, 0 H 13.144 v 7.887 l 2.892 -2.892 C 17.482, 6.309, 18.402, 8.281, 18.402, 10.515 c 0, 3.418 -2.235, 6.309 -5.258, 7.492 V 20.768 c 4.469 -1.183, 7.887 -5.258, 7.887 -10.121 c 0 -2.892 -1.183 -5.521 -3.155 -7.361 L 21.031, 0 z"/></svg> `
} )
} ) : null ,
className : BDFDB . disCN . marginbottom8 ,
children : BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Select , {
menuPlacement : inPopout ? BDFDB . LibraryComponents . Select . MenuPlacements . TOP : BDFDB . LibraryComponents . Select . MenuPlacements . BOTTOM ,
value : this . getLanguageChoice ( key ) ,
id : key ,
options : BDFDB . ObjectUtils . toArray ( BDFDB . ObjectUtils . map ( isOutput ? BDFDB . ObjectUtils . filter ( this . languages , lang => lang . id != "auto" ) : this . languages , ( lang , id ) => { return { value : id , label : lang . name } } ) ) ,
searchable : true ,
optionRenderer : lang => {
return BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex , {
align : BDFDB . LibraryComponents . Flex . Align . CENTER ,
children : [
BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . Flex . Child , {
grow : 1 ,
children : lang . label
} ) ,
inPopout ? BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FavButton , {
isFavorite : this . languages [ lang . value ] . fav == 0 ,
onClick : value => {
if ( value ) BDFDB . DataUtils . save ( true , this , "favorites" , lang . value ) ;
else BDFDB . DataUtils . remove ( this , "favorites" , lang . value ) ;
this . setLanguages ( ) ;
}
} ) : null
]
} ) ;
} ,
onChange : lang => {
BDFDB . DataUtils . save ( lang . value , this , "choices" , key ) ;
}
} )
} ) ) ;
if ( isOutput ) selects . push ( BDFDB . ReactUtils . createElement ( BDFDB . LibraryComponents . FormComponents . FormDivider , {
className : BDFDB . disCN . marginbottom8
} ) ) ;
}
return selects ;
}
setLanguages ( ) {
this . languages = Object . assign ( { } ,
{ "auto" : { name : "Auto" , id : "auto" , integrated : false , dic : false } } ,
BDFDB . LanguageUtils . languages ,
{ "binary" : { name : "Binary" , id : "binary" , integrated : false , dic : false } } ,
{ "braille" : { name : "Braille 6-dot" , id : "braille" , integrated : false , dic : false } } ,
{ "morse" : { name : "Morse" , id : "morse" , integrated : false , dic : false } }
) ;
let favorites = BDFDB . DataUtils . load ( this , "favorites" ) ;
for ( let id in this . languages ) this . languages [ id ] . fav = favorites [ id ] != undefined ? 0 : 1 ;
this . languages = BDFDB . ObjectUtils . sort ( this . languages , "fav" ) ;
}
getLanguageChoice ( direction , place ) {
this . setLanguages ( ) ;
var type = place === undefined ? direction : direction . toLowerCase ( ) + place . charAt ( 0 ) . toUpperCase ( ) + place . slice ( 1 ) . toLowerCase ( ) ;
var choice = BDFDB . DataUtils . get ( this , "choices" , type ) ;
choice = this . languages [ choice ] ? choice : Object . keys ( this . languages ) [ 0 ] ;
choice = type . indexOf ( "output" ) > - 1 && choice == "auto" ? "en" : choice ;
return choice ;
}
2019-01-26 22:45:19 +01:00
2019-01-02 23:28:11 +01:00
getMessageAndPos ( target ) {
2019-10-23 11:10:01 +02:00
let messagediv = BDFDB . DOMUtils . getParent ( BDFDB . dotCN . messagegroup + "> [aria-disabled]" , target ) || BDFDB . DOMUtils . getParent ( BDFDB . dotCN . messagegroup + "> * > [aria-disabled]" , target ) ;
2019-09-11 12:14:43 +02:00
let pos = messagediv ? Array . from ( messagediv . parentElement . childNodes ) . filter ( n => n . nodeType != Node . TEXT _NODE ) . indexOf ( messagediv ) : - 1 ;
2019-01-02 23:28:11 +01:00
return { messagediv , pos } ;
}
2019-01-26 22:45:19 +01:00
2019-01-02 23:28:11 +01:00
translateMessage ( message , target , channel ) {
if ( ! message || ! target ) return ;
let { messagediv , pos } = this . getMessageAndPos ( target ) ;
if ( ! messagediv || pos == - 1 ) return ;
2019-09-11 12:14:43 +02:00
channel = channel ? channel : BDFDB . LibraryModules . ChannelStore . getChannel ( message . channel _id ) ;
2019-08-28 14:02:30 +02:00
if ( ! messagediv . querySelector ( BDFDB . dotCN . messageedited + ".GTO-translated" ) ) {
2019-01-02 23:28:11 +01:00
var markup = messagediv . querySelector ( BDFDB . dotCN . messagemarkup ) ;
2019-09-27 09:48:32 +02:00
var accessory = messagediv . querySelector ( BDFDB . dotCN . messageaccessory ) ;
2019-10-22 19:49:57 +02:00
var embeddescriptions = messagediv . querySelectorAll ( BDFDB . dotCN . embeddescription ) ;
2019-01-02 23:28:11 +01:00
var fakemarkup = markup . cloneNode ( true ) ;
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . remove ( fakemarkup . querySelectorAll ( BDFDB . dotCNC . messageheadercompact + BDFDB . dotCN . messageedited ) ) ;
2019-09-27 09:48:32 +02:00
let string = fakemarkup . innerHTML ;
if ( embeddescriptions . length ) for ( let embeddescription of embeddescriptions ) {
string += "\n__________________ __________________ __________________\n" ;
string += embeddescription . innerHTML ; ;
}
this . translateText ( string , "context" , ( translation , input , output ) => {
2019-01-02 23:28:11 +01:00
if ( translation ) {
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . addClass ( messagediv , "GTO-translated-message" ) ;
2019-09-27 09:48:32 +02:00
let translations = translation . split ( "\n__________________ __________________ __________________\n" ) ;
2019-08-28 14:02:30 +02:00
let compactheader = markup . querySelector ( BDFDB . dotCN . messageheadercompact ) ;
2019-10-23 11:10:01 +02:00
markup . insertBefore ( BDFDB . DOMUtils . create ( ` <label class="GTO-translation"> ${ translations . shift ( ) . replace ( /\n/g , "BDFDB_GTO_PLACEHOLDER" ) . replace ( /\s/g , " " ) . replace ( /BDFDB_GTO_PLACEHOLDER/g , "\n" ) . replace ( /> *(\n*) *</g , ">$1<" ) . replace ( /> +/g , "> " ) . replace ( / +</g , " <" ) . replace ( /> *([^ ]*) *<\//g , ">$1</" ) . trim ( ) } <time class=" ${ BDFDB . disCN . messageedited } GTO-translated">( ${ this . labels . translated _watermark _text } )</time></label> ` ) , compactheader ? compactheader . nextSibling : markup . firstChild ) ;
2019-09-27 09:48:32 +02:00
if ( embeddescriptions . length ) for ( let embeddescription of embeddescriptions ) {
2019-10-23 11:10:01 +02:00
embeddescription . insertBefore ( BDFDB . DOMUtils . create ( ` <label class="GTO-translation"> ${ translations . shift ( ) . trim ( ) } <time class=" ${ BDFDB . disCN . messageedited } GTO-translated">( ${ this . labels . translated _watermark _text } )</time></label> ` ) , embeddescription . firstChild ) ;
2019-09-27 09:48:32 +02:00
}
2019-10-22 18:55:25 +02:00
BDFDB . ListenerUtils . addToChildren ( messagediv , "mouseenter" , BDFDB . dotCN . messageedited + ".GTO-translated" , e => {
BDFDB . TooltipUtils . create ( e . currentTarget , ` <div>From: ${ input . name } </div><div>To: ${ output . name } </div> ` , { html : true , type : "top" , selector : "translation-tooltip" } ) ;
2019-01-11 19:59:30 +01:00
} ) ;
2019-01-02 23:28:11 +01:00
}
} ) ;
}
else this . resetMessage ( messagediv ) ;
}
2019-01-26 22:45:19 +01:00
2019-01-02 23:28:11 +01:00
resetMessage ( messagediv ) {
2019-10-23 11:10:01 +02:00
BDFDB . DOMUtils . remove ( messagediv . querySelectorAll ( ".GTO-translation" ) ) ;
BDFDB . DOMUtils . removeClass ( messagediv , "GTO-translated-message" ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
translateText ( text , type , callback ) {
2019-06-14 20:51:39 +02:00
var toast = null ;
var finishTranslation = ( translation , exceptions , input , output , toast ) => {
2019-01-02 23:28:11 +01:00
if ( translation ) translation = this . addExceptions ( translation , exceptions ) ;
2019-04-16 00:30:33 +02:00
if ( toast ) {
clearInterval ( toast . interval ) ;
toast . close ( ) ;
}
2018-10-11 10:21:26 +02:00
callback ( translation , input , output ) ;
} ;
2019-08-28 14:02:30 +02:00
var translationError = ( exceptions , input , output , toast , test ) => {
2019-10-22 18:55:25 +02:00
BDFDB . NotificationUtils . toast ( "Could not translate message, you most likely got rate limited by Google for today due to too frequent usage of their Translate-API." , { type : "error" , timeout : 15000 } ) ;
2019-06-14 20:51:39 +02:00
finishTranslation ( null , exceptions , input , output , toast ) ;
} ;
2019-01-02 23:28:11 +01:00
var [ newtext , exceptions , translate ] = this . removeExceptions ( text . trim ( ) , type ) ;
2018-10-11 10:21:26 +02:00
var input = Object . assign ( { } , this . languages [ this . getLanguageChoice ( "input" , type ) ] ) ;
var output = Object . assign ( { } , this . languages [ this . getLanguageChoice ( "output" , type ) ] ) ;
var translation = "" ;
if ( translate ) {
2019-10-22 18:55:25 +02:00
toast = BDFDB . NotificationUtils . toast ( "Translating. Please wait" , { timeout : 0 } ) ;
2018-10-11 10:21:26 +02:00
toast . interval = setInterval ( ( ) => {
toast . textContent = toast . textContent . indexOf ( "....." ) > - 1 ? "Translating. Please wait" : toast . textContent + "." ;
} , 500 ) ;
2019-04-16 00:30:33 +02:00
var specialcase = this . checkForSpecialCase ( newtext , input ) ;
if ( specialcase ) {
input . name = specialcase . name ;
switch ( specialcase . id ) {
case "binary" : newtext = this . binary2string ( newtext ) ; break ;
case "braille" : newtext = this . braille2string ( newtext ) ; break ;
case "morse" : newtext = this . morse2string ( newtext ) ; break ;
2019-04-13 07:09:34 +02:00
}
2019-04-16 00:30:33 +02:00
}
if ( output . id == "binary" || output . id == "braille" || output . id == "morse" ) {
switch ( output . id ) {
case "binary" : newtext = this . string2binary ( newtext ) ; break ;
case "braille" : newtext = this . string2braille ( newtext ) ; break ;
case "morse" : newtext = this . string2morse ( newtext ) ; break ;
2019-04-13 07:09:34 +02:00
}
2019-04-16 00:30:33 +02:00
finishTranslation ( newtext , exceptions , input , output , toast ) ;
2018-10-11 10:21:26 +02:00
}
else {
2019-09-11 12:14:43 +02:00
BDFDB . LibraryRequires . request ( this . getGoogleTranslateApiURL ( input . id , output . id , newtext ) , ( error , response , result ) => {
2019-04-08 15:21:20 +02:00
if ( ! error && result ) {
2019-06-14 20:51:39 +02:00
try {
result = JSON . parse ( result ) ;
if ( result ) {
result [ 0 ] . forEach ( ( array ) => { translation += array [ 0 ] ; } ) ;
if ( ! specialcase && this . languages [ result [ 2 ] ] ) input . name = this . languages [ result [ 2 ] ] . name ;
}
else translation = text ;
finishTranslation ( translation , exceptions , input , output , toast ) ;
}
catch ( err ) {
2019-08-28 14:02:30 +02:00
translationError ( exceptions , input , output , toast , "a" ) ;
2019-04-16 00:30:33 +02:00
}
2019-04-08 15:21:20 +02:00
}
2019-08-28 14:02:30 +02:00
else translationError ( exceptions , input , output , toast , "b" ) ;
2019-04-08 15:21:20 +02:00
} ) ;
2018-10-11 10:21:26 +02:00
}
}
else {
translation = text ;
2019-01-02 23:28:11 +01:00
finishTranslation ( translation , exceptions , input , output , toast ) ;
2018-10-11 10:21:26 +02:00
}
}
2019-09-04 12:34:02 +02:00
2019-04-16 00:30:33 +02:00
checkForSpecialCase ( text , input ) {
if ( input . id == "binary" || input . id == "braille" || input . id == "morse" ) return input ;
else if ( input . id == "auto" ) {
if ( /^[0-1]*$/ . test ( text . replace ( /\s/g , "" ) ) ) {
return { id : "binary" , name : "Binary" } ;
}
else if ( /^[⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿]*$/ . test ( text . replace ( /\s/g , "" ) ) ) {
return { id : "braille" , name : "Braille 6-dot" } ;
}
else if ( /^[/|·−._-]*$/ . test ( text . replace ( /\s/g , "" ) ) ) {
return { id : "morse" , name : "Morse" } ;
}
}
2019-09-04 12:34:02 +02:00
return null ;
2019-04-16 00:30:33 +02:00
}
string2binary ( string ) {
var binary = "" ;
for ( let character of string ) binary += parseInt ( character . charCodeAt ( 0 ) . toString ( 2 ) ) . toPrecision ( 8 ) . split ( "." ) . reverse ( ) . join ( "" ) . toString ( ) + " " ;
return binary ;
}
string2braille ( string ) {
var braille = "" ;
for ( let character of string ) braille += this . brailleConverter [ character . toLowerCase ( ) ] ? this . brailleConverter [ character . toLowerCase ( ) ] : character ;
return braille ;
}
string2morse ( string ) {
string = string . replace ( / /g , "%%%%%%%%%%" ) ;
var morse = "" ;
for ( let character of string ) morse += ( this . morseConverter [ character . toLowerCase ( ) ] ? this . morseConverter [ character . toLowerCase ( ) ] : character ) + " " ;
morse = morse . split ( "\n" ) ;
for ( let i in morse ) morse [ i ] = morse [ i ] . trim ( ) ;
return morse . join ( "\n" ) . replace ( /% % % % % % % % % % /g , "/ " ) ;
}
binary2string ( binary ) {
var string = "" ;
binary = binary . replace ( /\n/g , "00001010" ) . replace ( /\r/g , "00001101" ) . replace ( /\t/g , "00001001" ) . replace ( /\s/g , "" ) ;
if ( /^[0-1]*$/ . test ( binary ) ) {
var eightdigits = "" ;
var counter = 0 ;
for ( var digit of binary ) {
eightdigits += digit ;
counter ++ ;
if ( counter > 7 ) {
string += String . fromCharCode ( parseInt ( eightdigits , 2 ) . toString ( 10 ) ) ;
eightdigits = "" ;
counter = 0 ;
}
}
}
2019-10-22 18:55:25 +02:00
else BDFDB . NotificationUtils . toast ( "Invalid binary format. Only use 0s and 1s." , { type : "error" } ) ;
2019-04-16 00:30:33 +02:00
return string ;
}
braille2string ( braille ) {
var string = "" ;
for ( let character of braille ) string += this . brailleConverter [ character . toLowerCase ( ) ] ? this . brailleConverter [ character . toLowerCase ( ) ] : character ;
return string ;
}
morse2string ( morse ) {
var string = "" ;
for ( let word of morse . replace ( /[_-]/g , "− " ) . replace ( /\./g , "·" ) . replace ( /\r|\t/g , "" ) . split ( /\/|\||\n/g ) ) {
for ( let characterstr of word . trim ( ) . split ( " " ) ) string += this . morseConverter [ characterstr ] ? this . morseConverter [ characterstr ] : characterstr ;
string += " " ;
}
return string . trim ( ) ;
}
2019-01-26 22:45:19 +01:00
2019-01-02 23:28:11 +01:00
addExceptions ( string , exceptions ) {
for ( let i in exceptions ) string = string . replace ( "a" + i + "_______" , exceptions [ i ] . indexOf ( "!" ) == 0 ? exceptions [ i ] . slice ( 1 ) : exceptions [ i ] ) ;
2018-10-11 10:21:26 +02:00
return string ;
}
2019-01-26 22:45:19 +01:00
2019-01-02 23:28:11 +01:00
removeExceptions ( string , type ) {
var exceptions = { } , newString = [ ] , count = 0 ;
if ( type == "context" ) {
let text = [ ] , i = 0 ;
string . split ( "" ) . forEach ( chara => {
if ( chara == "<" && text [ i ] ) i ++ ;
2019-09-04 12:34:02 +02:00
text [ i ] = text [ i ] ? text [ i ] + chara : chara ;
2019-01-02 23:28:11 +01:00
if ( chara == ">" ) i ++ ;
} ) ;
for ( let j in text ) {
if ( text [ j ] . indexOf ( "<" ) == 0 ) {
newString . push ( "a" + count + "_______" ) ;
exceptions [ count ] = text [ j ] ;
count ++ ;
}
else newString . push ( text [ j ] ) ;
2018-10-11 10:21:26 +02:00
}
2019-01-02 23:28:11 +01:00
}
else {
string . split ( " " ) . forEach ( word => {
if ( word . indexOf ( "<@!" ) == 0 || word . indexOf ( ":" ) == 0 || word . indexOf ( "@" ) == 0 || word . indexOf ( "#" ) == 0 || ( word . indexOf ( "!" ) == 0 && word . length > 1 ) ) {
newString . push ( "a" + count + "_______" ) ;
exceptions [ count ] = word ;
count ++ ;
}
else newString . push ( word ) ;
} ) ;
}
return [ newString . join ( " " ) , exceptions , newString . length - count != 0 ] ;
2018-10-11 10:21:26 +02:00
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getGoogleTranslateApiURL ( input , output , text ) {
2019-10-22 18:55:25 +02:00
input = BDFDB . LanguageUtils . languages [ input ] ? input : "auto" ;
2018-10-11 10:21:26 +02:00
return "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + input + "&tl=" + output + "&dt=t&ie=UTF-8&oe=UTF-8&q=" + encodeURIComponent ( text ) ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
getGoogleTranslatePageURL ( input , output , text ) {
2019-10-22 18:55:25 +02:00
input = BDFDB . LanguageUtils . languages [ input ] ? input : "auto" ;
2018-10-11 10:21:26 +02:00
return "https://translate.google.com/#" + input + "/" + output + "/" + encodeURIComponent ( text ) ;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
setLabelsByLanguage ( ) {
2019-10-24 11:47:57 +02:00
switch ( BDFDB . LanguageUtils . getLanguage ( ) . id ) {
2018-10-11 10:21:26 +02:00
case "hr" : //croatian
return {
context _messagetranslateoption _text : "Prijevod poruke" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Prijenos poruke" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Traži prijevod" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Prevesti" ,
popout _untranslateoption _text : "Prevesti natrag" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "preveo"
} ;
case "da" : //danish
return {
context _messagetranslateoption _text : "Oversæt Besked" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Oversæt Besked tilbage" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Søg oversættelse" ,
popout _translateoption _text : "Oversætte" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Oversæt tilbage" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "oversat"
} ;
case "de" : //german
return {
context _messagetranslateoption _text : "Nachricht übersetzen" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Nachricht unübersetzen" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Suche Übersetzung" ,
popout _translateoption _text : "Übersetzen" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Unübersetzen" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "übersetzt"
} ;
case "es" : //spanish
return {
context _messagetranslateoption _text : "Traducir mensaje" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Traducir mensaje de vuelta" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Buscar traducción" ,
popout _translateoption _text : "Traducir" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Traducir de vuelta" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "traducido"
} ;
case "fr" : //french
return {
context _messagetranslateoption _text : "Traduire le message" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Traduire le message en retour" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Rechercher une traduction" ,
popout _translateoption _text : "Traduire" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Traduire en arrière" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "traduit"
} ;
case "it" : //italian
return {
2019-01-02 23:28:11 +01:00
context _messagetranslateoption _text : "Tradurre il messaggio" ,
context _messageuntranslateoption _text : "Tradurre il messaggio indietro" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Cerca la traduzione" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Traduci" ,
popout _untranslateoption _text : "Traduci indietro" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "tradotto"
} ;
case "nl" : //dutch
return {
context _messagetranslateoption _text : "Vertaal bericht" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Vertaal bericht terug" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Zoek vertaling" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Vertaal" ,
popout _untranslateoption _text : "Vertaal terug" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "vertaalde"
} ;
case "no" : //norwegian
return {
context _messagetranslateoption _text : "Oversett melding" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Oversett melding tilbake" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Søk oversettelse" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Oversett" ,
popout _untranslateoption _text : "Oversett tilbake" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "oversatt"
} ;
case "pl" : //polish
return {
context _messagetranslateoption _text : "Przetłumacz wiadomość" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Przetłumacz wiadomość z powrotem" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Wyszukaj tłumaczenie" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Przetłumacz" ,
popout _untranslateoption _text : "Przetłumacz ponownie" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "przetłumaczony"
} ;
case "pt-BR" : //portuguese (brazil)
return {
context _messagetranslateoption _text : "Traduzir mensagem" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Traduzir mensagem de volta" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Pesquisar tradução" ,
popout _translateoption _text : "Traduzir" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Traduzir de volta" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "traduzido"
} ;
case "fi" : //finnish
return {
context _messagetranslateoption _text : "Käännä viesti" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Käännä viesti takaisin" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Etsi käännös" ,
popout _translateoption _text : "Kääntää" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Käännä takaisin" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "käännetty"
} ;
case "sv" : //swedish
return {
context _messagetranslateoption _text : "Översätt meddelande" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Översätt meddelandet tillbaka" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Sök översättning" ,
popout _translateoption _text : "Översätt" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Översätt tillbaka" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "översatt"
} ;
case "tr" : //turkish
return {
context _messagetranslateoption _text : "Mesajı çevir" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "İletiyi geri çevir" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Arama tercümesi" ,
popout _translateoption _text : "Çevirmek" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Geri çevir" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "tercüme"
} ;
case "cs" : //czech
return {
2019-01-02 23:28:11 +01:00
context _messagetranslateoption _text : "Přeposlat zprávu" ,
context _messageuntranslateoption _text : "Přeposlat zprávu zpátky" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Hledat překlad" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Přeposlat" ,
popout _untranslateoption _text : "Přeposlat zpět" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "přeloženo"
} ;
case "bg" : //bulgarian
return {
2019-01-02 23:28:11 +01:00
context _messagetranslateoption _text : "Преведете на съобщението" ,
context _messageuntranslateoption _text : "Преведете съобщението обратно" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Търсене на превод" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Превод" ,
popout _untranslateoption _text : "Превод обратно" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "преведена"
} ;
case "ru" : //russian
return {
context _messagetranslateoption _text : "Перевести сообщение" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Перевести сообщение обратно" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Поиск перевода" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "Перевести" ,
popout _untranslateoption _text : "Перевести обратно" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "переведенный"
} ;
case "uk" : //ukrainian
return {
context _messagetranslateoption _text : "Перекласти повідомлення" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Перекласти повідомлення назад" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Пошук перекладу" ,
popout _translateoption _text : "Перекласти" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Перекласти назад" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "перекладений"
} ;
case "ja" : //japanese
return {
context _messagetranslateoption _text : "メッセージを翻訳する" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "メッセージを翻訳する" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "翻訳の検索" ,
popout _translateoption _text : "翻訳" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "翻訳する" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "翻訳された"
} ;
case "zh-TW" : //chinese (traditional)
return {
context _messagetranslateoption _text : "翻譯消息" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "翻譯消息" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "搜索翻譯" ,
popout _translateoption _text : "翻譯" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "翻譯回來" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "翻譯"
} ;
case "ko" : //korean
return {
context _messagetranslateoption _text : "메시지 번역" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "메시지 번역 뒤로" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "검색 번역" ,
2019-01-02 23:28:11 +01:00
popout _translateoption _text : "다시" ,
popout _untranslateoption _text : "다시 번역" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "번역 된"
} ;
default : //default: english
return {
context _messagetranslateoption _text : "Translate Message" ,
2019-01-02 23:28:11 +01:00
context _messageuntranslateoption _text : "Untranslate Message" ,
2018-10-11 10:21:26 +02:00
context _googletranslateoption _text : "Search translation" ,
popout _translateoption _text : "Translate" ,
2019-01-02 23:28:11 +01:00
popout _untranslateoption _text : "Untranslate" ,
2018-10-11 10:21:26 +02:00
translated _watermark _text : "translated"
} ;
}
}
}