Update GoogleTranslateOption.plugin.js

This commit is contained in:
Mirco Wittrien 2019-06-14 20:51:39 +02:00
parent 5ca704cc20
commit 4efb721ee2
1 changed files with 20 additions and 9 deletions

View File

@ -3,7 +3,7 @@
class GoogleTranslateOption { class GoogleTranslateOption {
getName () {return "GoogleTranslateOption";} getName () {return "GoogleTranslateOption";}
getVersion () {return "1.6.9";} getVersion () {return "1.7.0";}
getAuthor () {return "DevilBro";} getAuthor () {return "DevilBro";}
@ -11,7 +11,7 @@ class GoogleTranslateOption {
initConstructor () { initConstructor () {
this.changelog = { this.changelog = {
"fixed":[["New Select Classes","The Dropdown-Select element got new classes on canary, this update will prevent stable from breaking once the class change is pushed to stable"]] "improved":[["API rate limit","Now warns you after you got rate limited by Google for their Translate-API if you used the plugin too frequently. This is not something I can fix"]]
}; };
this.labels = {}; this.labels = {};
@ -454,7 +454,8 @@ class GoogleTranslateOption {
} }
translateText (text, type, callback) { translateText (text, type, callback) {
var toast = null, finishTranslation = (translation, exceptions, input, output, toast) => { var toast = null;
var finishTranslation = (translation, exceptions, input, output, toast) => {
if (translation) translation = this.addExceptions(translation, exceptions); if (translation) translation = this.addExceptions(translation, exceptions);
if (toast) { if (toast) {
clearInterval(toast.interval); clearInterval(toast.interval);
@ -462,6 +463,10 @@ class GoogleTranslateOption {
} }
callback(translation, input, output); callback(translation, input, output);
}; };
var translationError = (exceptions, input, output, toast) => {
BDFDB.showToast("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)]);
@ -491,6 +496,7 @@ class GoogleTranslateOption {
else { else {
require("request")(this.getGoogleTranslateApiURL(input.id, output.id, newtext), (error, response, result) => { require("request")(this.getGoogleTranslateApiURL(input.id, output.id, newtext), (error, response, result) => {
if (!error && result) { if (!error && result) {
try {
result = JSON.parse(result); result = JSON.parse(result);
if (result) { if (result) {
result[0].forEach((array) => {translation += array[0];}); result[0].forEach((array) => {translation += array[0];});
@ -499,6 +505,11 @@ class GoogleTranslateOption {
else translation = text; else translation = text;
finishTranslation(translation, exceptions, input, output, toast); finishTranslation(translation, exceptions, input, output, toast);
} }
catch (err) {
translationError(exceptions, input, output, toast);
}
}
else translationError(exceptions, input, output, toast);
}); });
} }
} }