2020-10-14 03:37:03 +03:00
|
|
|
|
#include "qtcommon.h"
|
|
|
|
|
#include "extension.h"
|
|
|
|
|
#include "devtools.h"
|
|
|
|
|
|
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
extern Synchronized<std::wstring> translateTo;
|
|
|
|
|
|
|
|
|
|
bool useCache = true, autostartchrome = false, headlesschrome = true;
|
|
|
|
|
int maxSentenceSize = 500, chromeport = 9222;
|
|
|
|
|
|
|
|
|
|
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
|
2020-10-15 02:32:58 +03:00
|
|
|
|
const wchar_t* ERROR_CHROME = L"Error: chrome not started";
|
|
|
|
|
const wchar_t* ERROR_START_CHROME = L"Error: failed to start chrome or to connect to it";
|
|
|
|
|
const wchar_t* ERROR_GOT_TIMEOUT = L"Error: timeout (s)";
|
|
|
|
|
const wchar_t* ERROR_COMMAND_FAIL = L"Error: command failed";
|
|
|
|
|
const wchar_t* ERROR_LANGUAGE = L"Error: target languages do not match";
|
2020-10-15 23:23:41 +03:00
|
|
|
|
const wchar_t* ERROR_NOTE = L"Error: notification";
|
2020-10-15 02:32:58 +03:00
|
|
|
|
|
2020-10-14 03:37:03 +03:00
|
|
|
|
QString URL = "https://www.deepl.com/en/translator";
|
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"Chinese (simplified): zh",
|
|
|
|
|
"Dutch: nl",
|
|
|
|
|
"English: en",
|
|
|
|
|
"French: fr",
|
|
|
|
|
"German: de",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
|
|
|
|
"Polish: pl",
|
|
|
|
|
"Portuguese: pt",
|
|
|
|
|
"Russian: ru",
|
|
|
|
|
"Spanish: es",
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-18 16:16:06 +03:00
|
|
|
|
int docfound = -1, targetNodeId = -1, session = -1, pageenabled = -1, useragentflag = -1;
|
2020-10-21 00:10:58 +03:00
|
|
|
|
long update = -1, callnumber = 0;
|
|
|
|
|
std::vector<long> callqueue;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devtools)
|
|
|
|
|
{
|
|
|
|
|
QString qtext = S(text);
|
2020-10-20 02:08:59 +03:00
|
|
|
|
qtext.remove(QString(12288)); // japanese space (no need for translator)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Remove quotes
|
2020-10-20 02:08:59 +03:00
|
|
|
|
bool checkquote = false;
|
|
|
|
|
if ((qtext.front() == QString(12300) && qtext.back() == QString(12301)) // japanese quotation marks
|
|
|
|
|
|| (qtext.front() == "\"" && qtext.back() == "\""))
|
|
|
|
|
{
|
|
|
|
|
checkquote = true;
|
|
|
|
|
qtext.remove(0, 1);
|
|
|
|
|
qtext.chop(1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Check specific cases (sentence has only one japanese symbol or consists of ellipsis)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
int count = qtext.count(QString(8230)); // ellipsis
|
|
|
|
|
if (count == qtext.length()
|
|
|
|
|
|| (count == (qtext.length() - 1) && qtext.back() == QString(12290))) // japanese end of a sentence
|
|
|
|
|
{
|
|
|
|
|
return { true, text };
|
|
|
|
|
}
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (count == (qtext.length() - 1))
|
|
|
|
|
{
|
|
|
|
|
qtext.remove(QString(8230));
|
|
|
|
|
qtext += QString(12290) + QString(8230); // add the end symbol for correct translation
|
|
|
|
|
}
|
2020-10-15 23:23:41 +03:00
|
|
|
|
|
2020-10-20 02:08:59 +03:00
|
|
|
|
// Put quotes back
|
|
|
|
|
if (checkquote)
|
|
|
|
|
{
|
|
|
|
|
qtext = "\"" + qtext + "\"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check status
|
2020-10-15 02:32:58 +03:00
|
|
|
|
if (devtools->getStatus() == "Stopped")
|
|
|
|
|
{
|
|
|
|
|
return { false, FormatString(L"%s", ERROR_CHROME) };
|
|
|
|
|
}
|
2020-10-20 02:08:59 +03:00
|
|
|
|
if (devtools->getStatus().startsWith("Fail") || devtools->getStatus().startsWith("Unconnected"))
|
2020-10-15 02:32:58 +03:00
|
|
|
|
{
|
|
|
|
|
return { false, FormatString(L"%s", ERROR_START_CHROME) };
|
|
|
|
|
}
|
|
|
|
|
if (session != devtools->getSession())
|
|
|
|
|
{
|
|
|
|
|
session = devtools->getSession();
|
|
|
|
|
docfound = -1;
|
|
|
|
|
targetNodeId = -1;
|
|
|
|
|
pageenabled = -1;
|
2020-10-18 16:16:06 +03:00
|
|
|
|
useragentflag = -1;
|
2020-10-20 02:08:59 +03:00
|
|
|
|
update = -1;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
callnumber = 0;
|
2020-10-15 02:32:58 +03:00
|
|
|
|
}
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Remove tags and reduce the number of ellipsis for correct translation
|
2020-10-20 02:08:59 +03:00
|
|
|
|
qtext.remove(QRegExp("<[^>]*>"));
|
|
|
|
|
qtext.replace(QRegExp("(" + QString(8230) + ")+"), " " + QString(8230));
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
|
|
|
|
// Enable page feedback
|
2020-10-20 02:08:59 +03:00
|
|
|
|
QJsonObject root;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
int errorcode = 0;
|
2020-10-15 02:32:58 +03:00
|
|
|
|
if (pageenabled == -1)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-15 02:32:58 +03:00
|
|
|
|
if (!devtools->SendRequest("Page.enable", {}, root))
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 1;
|
|
|
|
|
else
|
|
|
|
|
pageenabled = 1;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
}
|
2020-10-18 16:16:06 +03:00
|
|
|
|
|
|
|
|
|
// Change user-agent if in headless mode
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (useragentflag == -1 && errorcode == 0)
|
2020-10-18 16:16:06 +03:00
|
|
|
|
{
|
|
|
|
|
QString useragent = devtools->getUserAgent();
|
|
|
|
|
if (!useragent.isEmpty())
|
|
|
|
|
{
|
2020-10-20 02:08:59 +03:00
|
|
|
|
useragent.replace("HeadlessChrome", "Chrome");
|
2020-10-18 16:16:06 +03:00
|
|
|
|
if (!devtools->SendRequest("Network.setUserAgentOverride", { {"userAgent", useragent} }, root))
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 1;
|
|
|
|
|
else
|
|
|
|
|
useragentflag = 1;
|
2020-10-18 16:16:06 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Increase queue counter and wait until previous calls are done
|
2020-10-21 00:10:58 +03:00
|
|
|
|
float timer = 0;
|
|
|
|
|
int timer_stop = 10;
|
|
|
|
|
long calltag = ++callnumber;
|
|
|
|
|
callqueue.insert(callqueue.begin(), calltag);
|
2020-10-22 23:15:45 +03:00
|
|
|
|
while (errorcode == 0 && callqueue.back() != calltag && timer < 2 * timer_stop)
|
2020-10-21 00:10:58 +03:00
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
timer += 0.1;
|
|
|
|
|
}
|
|
|
|
|
if (timer >= timer_stop)
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 5;
|
|
|
|
|
|
|
|
|
|
// Set methods to receive
|
2020-10-15 23:23:41 +03:00
|
|
|
|
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument");
|
|
|
|
|
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
|
2020-10-20 02:08:59 +03:00
|
|
|
|
if (update == -1)
|
|
|
|
|
update = devtools->methodToReceive("DOM.documentUpdated");
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Navigate to site and wait until it is loaded
|
2020-10-14 03:37:03 +03:00
|
|
|
|
QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (errorcode == 0 && !devtools->SendRequest("Page.navigate", { {"url", fullurl} }, root))
|
|
|
|
|
errorcode = 1;
|
2020-10-21 00:10:58 +03:00
|
|
|
|
timer = 0;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
while (errorcode == 0 && !devtools->checkMethod(navigate) && timer < timer_stop)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
timer += 0.1;
|
|
|
|
|
}
|
|
|
|
|
if (timer >= timer_stop)
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 2;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2020-10-20 02:08:59 +03:00
|
|
|
|
// Check if document is outdated
|
|
|
|
|
if (devtools->checkMethod(update))
|
|
|
|
|
{
|
|
|
|
|
docfound = -1;
|
|
|
|
|
targetNodeId = -1;
|
|
|
|
|
update = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get document
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (docfound == -1 && errorcode == 0)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
|
|
|
|
if (!devtools->SendRequest("DOM.getDocument", {}, root))
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 1;
|
|
|
|
|
else
|
|
|
|
|
docfound = root.value("result").toObject().value("root").toObject().value("nodeId").toInt();
|
2020-10-20 02:08:59 +03:00
|
|
|
|
}
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2020-10-20 02:08:59 +03:00
|
|
|
|
// Get target selector
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (targetNodeId == -1 && errorcode == 0)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
|
|
|
|
if (!devtools->SendRequest("DOM.querySelector", { {"nodeId", docfound}, {"selector", "textarea.lmt__target_textarea"} }, root)
|
|
|
|
|
|| root.value("result").toObject().value("nodeId").toInt() == 0)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-20 02:08:59 +03:00
|
|
|
|
docfound = -1;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 1;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
}
|
2020-10-22 23:15:45 +03:00
|
|
|
|
else
|
|
|
|
|
targetNodeId = root.value("result").toObject().value("nodeId").toInt();
|
2020-10-20 02:08:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:15:45 +03:00
|
|
|
|
// Wait for the translation to appear on the web page
|
2020-10-20 02:08:59 +03:00
|
|
|
|
timer = 0;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
while (errorcode == 0 && !devtools->checkMethod(target) && timer < timer_stop)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
timer += 0.1;
|
|
|
|
|
}
|
2020-10-21 00:10:58 +03:00
|
|
|
|
|
2020-10-20 02:08:59 +03:00
|
|
|
|
// Catch the translation
|
2020-10-22 23:15:45 +03:00
|
|
|
|
QString OuterHTML;
|
|
|
|
|
if (errorcode == 0 && !devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", targetNodeId + 1} }, root))
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
|
|
|
|
targetNodeId = -1;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
2020-10-20 02:08:59 +03:00
|
|
|
|
}
|
|
|
|
|
if (OuterHTML == "<div></div>")
|
|
|
|
|
{
|
|
|
|
|
// Try to catch the notification
|
|
|
|
|
int noteNodeId = -1;
|
2020-10-22 23:15:45 +03:00
|
|
|
|
if (errorcode == 0 && !devtools->SendRequest("DOM.querySelector", { {"nodeId", docfound}, {"selector", "div.lmt__system_notification"} }, root)
|
2020-10-20 02:08:59 +03:00
|
|
|
|
|| root.value("result").toObject().value("nodeId").toInt() == 0)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 2;
|
2020-10-20 02:08:59 +03:00
|
|
|
|
}
|
2020-10-22 23:15:45 +03:00
|
|
|
|
else
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
2020-10-22 23:15:45 +03:00
|
|
|
|
noteNodeId = root.value("result").toObject().value("nodeId").toInt();
|
|
|
|
|
if (errorcode == 0 && devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", noteNodeId} }, root))
|
|
|
|
|
{
|
|
|
|
|
OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
|
|
|
|
}
|
|
|
|
|
errorcode = 3;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
}
|
2020-10-20 02:08:59 +03:00
|
|
|
|
}
|
|
|
|
|
OuterHTML.remove(QRegExp("<[^>]*>"));
|
|
|
|
|
OuterHTML = OuterHTML.trimmed();
|
|
|
|
|
|
|
|
|
|
// Check if the translator output language does not match the selected language
|
2020-10-22 23:15:45 +03:00
|
|
|
|
QString targetlang;
|
|
|
|
|
if (errorcode == 0 && devtools->SendRequest("DOM.getAttributes", { {"nodeId", targetNodeId} }, root))
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
2020-10-22 23:15:45 +03:00
|
|
|
|
QJsonArray attributes = root.value("result").toObject().value("attributes").toArray();
|
2020-10-20 02:08:59 +03:00
|
|
|
|
for (size_t i = 0; i < attributes.size(); i++)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-20 02:08:59 +03:00
|
|
|
|
if (attributes[i].toString() == "lang")
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-22 23:15:45 +03:00
|
|
|
|
targetlang = attributes[i + 1].toString().mid(0, 2);
|
2020-10-20 02:08:59 +03:00
|
|
|
|
if (targetlang != S(translateTo.Copy()))
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2020-10-22 23:15:45 +03:00
|
|
|
|
errorcode = 4;
|
2020-10-14 03:37:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 23:15:45 +03:00
|
|
|
|
|
|
|
|
|
callqueue.pop_back();
|
|
|
|
|
if (errorcode == 0)
|
|
|
|
|
return { true, S(OuterHTML) };
|
|
|
|
|
else if (errorcode == 1)
|
|
|
|
|
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
|
|
|
|
|
else if (errorcode == 2)
|
|
|
|
|
return { false, FormatString(L"%s: %d", ERROR_GOT_TIMEOUT, timer_stop) };
|
|
|
|
|
else if (errorcode == 3)
|
|
|
|
|
return { false, FormatString(L"%s: %s", ERROR_NOTE, S(OuterHTML)) };
|
|
|
|
|
else if (errorcode == 4)
|
|
|
|
|
return { false, FormatString(L"%s (%s): %s", ERROR_LANGUAGE, S(targetlang), S(OuterHTML)) };
|
|
|
|
|
else if (errorcode == 5)
|
|
|
|
|
return { false, FormatString(L"%s: %d", ERROR_GOT_TIMEOUT, 2*timer_stop) };
|
|
|
|
|
else
|
|
|
|
|
return { false, FormatString(L"%s", TRANSLATION_ERROR) };
|
2020-10-15 23:23:41 +03:00
|
|
|
|
}
|