Update GoogleTranslateOption.plugin.js
This commit is contained in:
parent
9c6c6c661a
commit
d5d03c4fcd
|
@ -27,7 +27,7 @@ var GoogleTranslateOption = (_ => {
|
||||||
return class GoogleTranslateOption {
|
return class GoogleTranslateOption {
|
||||||
getName () {return "GoogleTranslateOption";}
|
getName () {return "GoogleTranslateOption";}
|
||||||
|
|
||||||
getVersion () {return "2.0.4";}
|
getVersion () {return "2.0.5";}
|
||||||
|
|
||||||
getAuthor () {return "DevilBro";}
|
getAuthor () {return "DevilBro";}
|
||||||
|
|
||||||
|
@ -150,6 +150,8 @@ var GoogleTranslateOption = (_ => {
|
||||||
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
|
if (window.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
|
||||||
this.stopping = true;
|
this.stopping = true;
|
||||||
|
|
||||||
|
translating = false;
|
||||||
|
|
||||||
BDFDB.ModuleUtils.forceAllUpdates(this);
|
BDFDB.ModuleUtils.forceAllUpdates(this);
|
||||||
|
|
||||||
BDFDB.PluginUtils.clear(this);
|
BDFDB.PluginUtils.clear(this);
|
||||||
|
@ -172,7 +174,7 @@ var GoogleTranslateOption = (_ => {
|
||||||
let translated = translatedMessages[e.instance.props.message.id];
|
let translated = translatedMessages[e.instance.props.message.id];
|
||||||
let hint = BDFDB.BDUtils.isPluginEnabled("MessageUtilities") ? BDFDB.BDUtils.getPlugin("MessageUtilities").getActiveShortcutString("__Translate_Message") : null;
|
let hint = BDFDB.BDUtils.isPluginEnabled("MessageUtilities") ? BDFDB.BDUtils.getPlugin("MessageUtilities").getActiveShortcutString("__Translate_Message") : null;
|
||||||
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: ["pin", "unpin"]});
|
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: ["pin", "unpin"]});
|
||||||
children.splice(index > -1 ? index + 1: 0, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
|
children.splice(index > -1 ? index + 1 : 0, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuItem, {
|
||||||
label: translated ? this.labels.context_messageuntranslateoption_text : this.labels.context_messagetranslateoption_text,
|
label: translated ? this.labels.context_messageuntranslateoption_text : this.labels.context_messagetranslateoption_text,
|
||||||
id: BDFDB.ContextMenuUtils.createItemId(this.name, translated ? "untranslate-message" : "translate-message"),
|
id: BDFDB.ContextMenuUtils.createItemId(this.name, translated ? "untranslate-message" : "translate-message"),
|
||||||
hint: hint && (_ => {
|
hint: hint && (_ => {
|
||||||
|
@ -203,7 +205,8 @@ var GoogleTranslateOption = (_ => {
|
||||||
if (text) {
|
if (text) {
|
||||||
let translating, foundTranslation, foundInput, foundOutput;
|
let translating, foundTranslation, foundInput, foundOutput;
|
||||||
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: ["devmode-copy-id", "search-google"], group: true});
|
let [children, index] = BDFDB.ContextMenuUtils.findItem(e.returnvalue, {id: ["devmode-copy-id", "search-google"], group: true});
|
||||||
children.splice(index > -1 ? index + 1 : 0, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuPersistingItem, {
|
children.splice(index > -1 ? index + 1 : 0, 0, BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuGroup, {
|
||||||
|
children: BDFDB.ContextMenuUtils.createItem(BDFDB.LibraryComponents.MenuItems.MenuPersistingItem, {
|
||||||
id: BDFDB.ContextMenuUtils.createItemId(this.name, "search-translation"),
|
id: BDFDB.ContextMenuUtils.createItemId(this.name, "search-translation"),
|
||||||
disabled: isTranslating,
|
disabled: isTranslating,
|
||||||
label: this.labels.context_googletranslateoption_text,
|
label: this.labels.context_googletranslateoption_text,
|
||||||
|
@ -231,6 +234,7 @@ var GoogleTranslateOption = (_ => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -256,7 +260,7 @@ var GoogleTranslateOption = (_ => {
|
||||||
}
|
}
|
||||||
|
|
||||||
processChannelTextAreaForm (e) {
|
processChannelTextAreaForm (e) {
|
||||||
if (!BDFDB.ModuleUtils.isPatched(this, e.instance, "handleSendMessage")) BDFDB.ModuleUtils.patch(this, e.instance, "handleSendMessage", {instead: e2 => {
|
BDFDB.ModuleUtils.patch(this, e.instance, "handleSendMessage", {instead: e2 => {
|
||||||
if (translating) {
|
if (translating) {
|
||||||
e2.stopOriginalMethodCall();
|
e2.stopOriginalMethodCall();
|
||||||
this.translateText(e2.methodArguments[0], "message", (translation, input, output) => {
|
this.translateText(e2.methodArguments[0], "message", (translation, input, output) => {
|
||||||
|
@ -352,15 +356,23 @@ var GoogleTranslateOption = (_ => {
|
||||||
for (let i in e.instance.props.channelStream) {
|
for (let i in e.instance.props.channelStream) {
|
||||||
let message = e.instance.props.channelStream[i].content;
|
let message = e.instance.props.channelStream[i].content;
|
||||||
if (message) {
|
if (message) {
|
||||||
|
if (BDFDB.ArrayUtils.is(message.attachments)) this.checkMessage(e.instance.props.channelStream[i], message);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkMessage (stream, message) {
|
||||||
let translation = translatedMessages[message.id];
|
let translation = translatedMessages[message.id];
|
||||||
if (translation) e.instance.props.channelStream[i].content.content = translation.content;
|
if (translation) stream.content.content = translation.content;
|
||||||
else if (oldMessages[message.id] && Object.keys(message).some(key => !BDFDB.equals(oldMessages[message.id][key], message[key]))) {
|
else if (oldMessages[message.id] && Object.keys(message).some(key => !BDFDB.equals(oldMessages[message.id][key], message[key]))) {
|
||||||
e.instance.props.channelStream[i].content.content = oldMessages[message.id].content;
|
stream.content.content = oldMessages[message.id].content;
|
||||||
delete oldMessages[message.id];
|
delete oldMessages[message.id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
processMessageContent (e) {
|
processMessageContent (e) {
|
||||||
if (e.instance.props.message) {
|
if (e.instance.props.message) {
|
||||||
|
@ -592,9 +604,15 @@ var GoogleTranslateOption = (_ => {
|
||||||
}
|
}
|
||||||
|
|
||||||
googleTranslate (data, callback) {
|
googleTranslate (data, callback) {
|
||||||
let googleTranslateWindow = BDFDB.WindowUtils.open(this, this.getGoogleTranslatePageURL(data.input.id, data.output.id, data.text));
|
let googleTranslateWindow = BDFDB.WindowUtils.open(this, this.getGoogleTranslatePageURL(data.input.id, data.output.id, data.text), {
|
||||||
googleTranslateWindow.webContents.on("did-finish-load", _ => {
|
onLoad: _ => {
|
||||||
googleTranslateWindow.webContents.executeJavaScript(`require("electron").ipcRenderer.sendTo(${BDFDB.LibraryRequires.electron.remote.getCurrentWindow().webContents.id}, "GTO-translation", [(document.querySelector(".translation") || {}).innerText, [(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]]);`);
|
googleTranslateWindow.executeJavaScriptSafe(`
|
||||||
|
require("electron").ipcRenderer.sendTo(${BDFDB.LibraryRequires.electron.remote.getCurrentWindow().webContents.id}, "GTO-translation", [
|
||||||
|
(document.querySelector(".translation") || {}).innerText,
|
||||||
|
[(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(10)[1]
|
||||||
|
]);
|
||||||
|
`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
BDFDB.WindowUtils.addListener(this, "GTO-translation", (event, messageData) => {
|
BDFDB.WindowUtils.addListener(this, "GTO-translation", (event, messageData) => {
|
||||||
BDFDB.WindowUtils.close(googleTranslateWindow);
|
BDFDB.WindowUtils.close(googleTranslateWindow);
|
||||||
|
@ -813,8 +831,7 @@ var GoogleTranslateOption = (_ => {
|
||||||
}
|
}
|
||||||
|
|
||||||
getGoogleTranslatePageURL (input, output, text) {
|
getGoogleTranslatePageURL (input, output, text) {
|
||||||
input = BDFDB.LanguageUtils.languages[input] ? input : "auto";
|
return "https://translate.google.com/#" + (BDFDB.LanguageUtils.languages[input] ? input : "auto") + "/" + output + "/" + encodeURIComponent(text);
|
||||||
return "https://translate.google.com/#" + input + "/" + output + "/" + encodeURIComponent(text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getLanguageName (language) {
|
getLanguageName (language) {
|
||||||
|
|
Loading…
Reference in New Issue