Update GoogleTranslateOption.plugin.js

This commit is contained in:
Mirco Wittrien 2019-12-07 11:56:11 +01:00
parent 194e18524f
commit 7ab0a7cbfe
1 changed files with 22 additions and 30 deletions

View File

@ -3,7 +3,7 @@
class GoogleTranslateOption { class GoogleTranslateOption {
getName () {return "GoogleTranslateOption";} getName () {return "GoogleTranslateOption";}
getVersion () {return "1.7.7";} getVersion () {return "1.7.8";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
@ -11,12 +11,12 @@ class GoogleTranslateOption {
constructor () { constructor () {
this.changelog = { this.changelog = {
"improved":[["Right Click","Fixed issue where right click would not quick enable/disable translating"]] "fixed":[["Google API","Google ultimately removed their free Google Translate Endpoint, killing any chances of using a free translation API that doesn't have a requests per month limit. I switched to emulating the translate webpage in an invisible browserwindow, sadly this is far slower than the old method, but at least it still works"],["New Chatbar still broken","<span style='color: rgb(241, 71, 71);'>the plugin still fails to translate outgoing messages if your client is using the new WYSIWYG chatinput, this bug will be fixed at a later date</span>"]]
}; };
this.patchedModules = { this.patchedModules = {
after: { after: {
ChannelTextArea: ["componentDidMount","render"], ChannelTextArea: ["render", "componentDidMount"],
Message: "componentDidMount", Message: "componentDidMount",
MessageContent: "componentDidMount" MessageContent: "componentDidMount"
} }
@ -193,7 +193,7 @@ class GoogleTranslateOption {
}; };
if (foundtranslation && foundinput && foundoutput) { if (foundtranslation && foundinput && foundoutput) {
if (document.querySelector(".googletranslate-tooltip")) { if (document.querySelector(".googletranslate-tooltip")) {
BDFDB.ContextMenuUtils.close(menu); BDFDB.ContextMenuUtils.close(e.instance);
BDFDB.DiscordUtils.openLink(this.getGoogleTranslatePageURL(foundinput.id, foundoutput.id, text), BDFDB.DataUtils.get(this, "settings", "useChromium")); BDFDB.DiscordUtils.openLink(this.getGoogleTranslatePageURL(foundinput.id, foundoutput.id, text), BDFDB.DataUtils.get(this, "settings", "useChromium"));
} }
else createTooltip(); else createTooltip();
@ -368,7 +368,7 @@ class GoogleTranslateOption {
menuPlacement: inPopout ? BDFDB.LibraryComponents.Select.MenuPlacements.TOP : BDFDB.LibraryComponents.Select.MenuPlacements.BOTTOM, menuPlacement: inPopout ? BDFDB.LibraryComponents.Select.MenuPlacements.TOP : BDFDB.LibraryComponents.Select.MenuPlacements.BOTTOM,
value: this.getLanguageChoice(key), value: this.getLanguageChoice(key),
id: 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}})), 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:this.getLanguageName(lang)}})),
searchable: true, searchable: true,
optionRenderer: lang => { optionRenderer: lang => {
return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, { return BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {
@ -455,7 +455,7 @@ class GoogleTranslateOption {
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); 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);
} }
BDFDB.ListenerUtils.addToChildren(messagediv, "mouseenter", BDFDB.dotCN.messageedited + ".GTO-translated", e => { 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"}); BDFDB.TooltipUtils.create(e.currentTarget, `<div>From: ${this.getLanguageName(input)}</div><div>To: ${this.getLanguageName(output)}</div>`, {html:true, type:"top", selector:"translation-tooltip", style: "max-width: 400px"});
}); });
} }
}); });
@ -478,10 +478,6 @@ class GoogleTranslateOption {
} }
callback(translation, input, output); callback(translation, input, output);
}; };
var translationError = (exceptions, input, output, toast, test) => {
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});
finishTranslation(null, exceptions, input, output, toast);
};
var [newtext, exceptions, translate] = this.removeExceptions(text.trim(), type); var [newtext, exceptions, translate] = this.removeExceptions(text.trim(), type);
var input = Object.assign({}, this.languages[this.getLanguageChoice("input", type)]); var input = Object.assign({}, this.languages[this.getLanguageChoice("input", type)]);
var output = Object.assign({}, this.languages[this.getLanguageChoice("output", type)]); var output = Object.assign({}, this.languages[this.getLanguageChoice("output", type)]);
@ -509,22 +505,18 @@ class GoogleTranslateOption {
finishTranslation(newtext, exceptions, input, output, toast); finishTranslation(newtext, exceptions, input, output, toast);
} }
else { else {
BDFDB.LibraryRequires.request(this.getGoogleTranslateApiURL(input.id, output.id, newtext), (error, response, result) => { let googleTranslateWindow = BDFDB.WindowUtils.open(this, this.getGoogleTranslatePageURL(input.id, output.id, newtext));
if (!error && result) { googleTranslateWindow.webContents.on("did-finish-load", _ => {
try { googleTranslateWindow.webContents.executeJavaScript(`require("electron").ipcRenderer.sendTo(${BDFDB.LibraryRequires.electron.remote.getCurrentWindow().webContents.id}, "GTO-translation", [(document.querySelector(".translation span") || {}).innerHTML, [(new RegExp("{code:'([^']*)',name:'" + [(new RegExp((window.source_language_detected || "").replace("%1$s", "([A-z]{2,})"), "g")).exec(document.body.innerHTML)].flat()[1] +"'}", "g")).exec(document.body.innerHTML)].flat()[1]]);`);
result = JSON.parse(result); });
if (result) { BDFDB.WindowUtils.addListener(this, "GTO-translation", (event, data) => {
result[0].forEach((array) => {translation += array[0];}); googleTranslateWindow.close();
if (!specialcase && this.languages[result[2]]) input.name = this.languages[result[2]].name; BDFDB.WindowUtils.removeListener(this, "GTO-translation");
} if (!specialcase && data[1] && this.languages[data[1]]) {
else translation = text; input.name = this.languages[data[1]].name;
finishTranslation(translation, exceptions, input, output, toast); input.ownlang = this.languages[data[1]].ownlang;
}
catch (err) {
translationError(exceptions, input, output, toast, "a");
}
} }
else translationError(exceptions, input, output, toast, "b"); finishTranslation(data[0], exceptions, input, output, toast);
}); });
} }
} }
@ -642,15 +634,15 @@ class GoogleTranslateOption {
return [newString.join(" "), exceptions, newString.length-count != 0]; return [newString.join(" "), exceptions, newString.length-count != 0];
} }
getGoogleTranslateApiURL (input, output, text) {
input = BDFDB.LanguageUtils.languages[input] ? input : "auto";
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);
}
getGoogleTranslatePageURL (input, output, text) { getGoogleTranslatePageURL (input, output, text) {
input = BDFDB.LanguageUtils.languages[input] ? input : "auto"; input = BDFDB.LanguageUtils.languages[input] ? input : "auto";
return "https://translate.google.com/#" + input + "/" + output + "/" + encodeURIComponent(text); return "https://translate.google.com/#" + input + "/" + output + "/" + encodeURIComponent(text);
} }
getLanguageName (language) {
if (language.name.startsWith("Discord")) return language.name.slice(0, -1) + (language.ownlang && this.languages[language.id].name != language.ownlang ? ` / ${language.ownlang}` : "") + ")";
else return language.name + (language.ownlang && language.name != language.ownlang ? ` / ${language.ownlang}` : "");
}
setLabelsByLanguage () { setLabelsByLanguage () {
switch (BDFDB.LanguageUtils.getLanguage().id) { switch (BDFDB.LanguageUtils.getLanguage().id) {