add notification alert
When timeout from the net the method searches for notifications from the translator and put them to output
This commit is contained in:
parent
a3ebaf0023
commit
73d1f21bc1
@ -4,11 +4,9 @@ DevTools::DevTools(QObject* parent) :
|
|||||||
QObject(parent),
|
QObject(parent),
|
||||||
idcounter(0),
|
idcounter(0),
|
||||||
idmethod(0),
|
idmethod(0),
|
||||||
pagenavigated(false),
|
|
||||||
translateready(false),
|
|
||||||
status("Stopped"),
|
status("Stopped"),
|
||||||
session(0)
|
session(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevTools::startDevTools(QString path, bool headless, int port)
|
void DevTools::startDevTools(QString path, bool headless, int port)
|
||||||
@ -59,14 +57,14 @@ bool DevTools::startChrome(QString path, bool headless, int port)
|
|||||||
DWORD exitCode = 0;
|
DWORD exitCode = 0;
|
||||||
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) != FALSE) && (exitCode == STILL_ACTIVE))
|
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) != FALSE) && (exitCode == STILL_ACTIVE))
|
||||||
return false;
|
return false;
|
||||||
QString args = "--proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir="
|
QString args = "--proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir="
|
||||||
+ QString::fromStdWString(std::filesystem::current_path())
|
+ QString::fromStdWString(std::filesystem::current_path())
|
||||||
+ "\\devtoolscache --remote-debugging-port="
|
+ "\\devtoolscache --remote-debugging-port="
|
||||||
+ QString::number(port);
|
+ QString::number(port);
|
||||||
if (headless)
|
if (headless)
|
||||||
args += " --headless";
|
args += " --headless";
|
||||||
STARTUPINFOW dummy = { sizeof(dummy) };
|
STARTUPINFOW dummy = { sizeof(dummy) };
|
||||||
if (!CreateProcessW(NULL, (wchar_t*)(path + " " + args).utf16(), nullptr, nullptr,
|
if (!CreateProcessW(NULL, (wchar_t*)(path + " " + args).utf16(), nullptr, nullptr,
|
||||||
FALSE, 0, nullptr, nullptr, &dummy, &processInfo))
|
FALSE, 0, nullptr, nullptr, &dummy, &processInfo))
|
||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
@ -127,8 +125,7 @@ bool DevTools::SendRequest(QString method, QJsonObject params, QJsonObject& root
|
|||||||
long id = idIncrement();
|
long id = idIncrement();
|
||||||
json.insert("id", id);
|
json.insert("id", id);
|
||||||
json.insert("method", method);
|
json.insert("method", method);
|
||||||
if (!params.isEmpty())
|
json.insert("params", params);
|
||||||
json.insert("params", params);
|
|
||||||
QJsonDocument doc(json);
|
QJsonDocument doc(json);
|
||||||
QString message(doc.toJson(QJsonDocument::Compact));
|
QString message(doc.toJson(QJsonDocument::Compact));
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
@ -190,17 +187,27 @@ bool DevTools::isConnected()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DevTools::compareJson(QJsonObject storedparams, QJsonObject params)
|
bool DevTools::compareJson(QJsonValue storedparams, QJsonValue params)
|
||||||
{
|
{
|
||||||
foreach(const QString & key, storedparams.keys())
|
if (storedparams.isObject())
|
||||||
{
|
{
|
||||||
if (storedparams.value(key).isArray())
|
foreach(const QString & key, storedparams.toObject().keys())
|
||||||
return false;
|
{
|
||||||
if (storedparams.value(key) != params.value(key))
|
QJsonValue storedvalue = storedparams.toObject().value(key);
|
||||||
return false;
|
QJsonValue value = params.toObject().value(key);
|
||||||
if (!compareJson(storedparams.value(key).toObject(), params.value(key).toObject()))
|
if (!compareJson(storedvalue, value))
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else if (storedparams.isArray())
|
||||||
|
{
|
||||||
|
for (int i = 0; i < storedparams.toArray().size(); i++)
|
||||||
|
if (!compareJson(storedparams.toArray()[i], params.toArray()[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (storedparams.toVariant() != params.toVariant())
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,9 +230,8 @@ void DevTools::onTextMessageReceived(QString message)
|
|||||||
{
|
{
|
||||||
for (auto iter = mapmethod.cbegin(); iter != mapmethod.cend();)
|
for (auto iter = mapmethod.cbegin(); iter != mapmethod.cend();)
|
||||||
{
|
{
|
||||||
if ((iter->second.value("method") == root.value("method"))
|
if ((iter->second.value("method") == root.value("method"))
|
||||||
&& ((iter->second.value("params").toObject().isEmpty())
|
&& (compareJson(iter->second.value("params"), root.value("params"))))
|
||||||
|| (compareJson(iter->second.value("params").toObject(), root.value("params").toObject()))))
|
|
||||||
{
|
{
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
mapmethod.erase(iter++);
|
mapmethod.erase(iter++);
|
||||||
@ -248,7 +254,7 @@ void DevTools::onTextMessageReceived(QString message)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DevTools::closeDevTools()
|
void DevTools::closeDevTools()
|
||||||
@ -289,4 +295,4 @@ void DevTools::closeDevTools()
|
|||||||
}
|
}
|
||||||
status = "Stopped";
|
status = "Stopped";
|
||||||
emit statusChanged(status);
|
emit statusChanged(status);
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ public:
|
|||||||
~DevTools();
|
~DevTools();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void statusChanged(const QString &);
|
void statusChanged(const QString&);
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void stateChanged(QAbstractSocket::SocketState state);
|
void stateChanged(QAbstractSocket::SocketState state);
|
||||||
@ -28,7 +28,7 @@ public:
|
|||||||
bool checkMethod(long id);
|
bool checkMethod(long id);
|
||||||
int getSession();
|
int getSession();
|
||||||
bool SendRequest(QString method, QJsonObject params, QJsonObject& root);
|
bool SendRequest(QString method, QJsonObject params, QJsonObject& root);
|
||||||
long methodToReceive(QString method, QJsonObject params);
|
long methodToReceive(QString method, QJsonObject params = {});
|
||||||
QString getStatus();
|
QString getStatus();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -37,14 +37,12 @@ private:
|
|||||||
bool GetwebSocketDebuggerUrl(QString& url, int port = 9222);
|
bool GetwebSocketDebuggerUrl(QString& url, int port = 9222);
|
||||||
long idIncrement();
|
long idIncrement();
|
||||||
long idmIncrement();
|
long idmIncrement();
|
||||||
bool compareJson(QJsonObject storedparams, QJsonObject params);
|
bool compareJson(QJsonValue storedparams, QJsonValue params);
|
||||||
int session;
|
int session;
|
||||||
QWebSocket webSocket;
|
QWebSocket webSocket;
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
MapResponse mapqueue;
|
MapResponse mapqueue;
|
||||||
MapMethod mapmethod;
|
MapMethod mapmethod;
|
||||||
bool pagenavigated;
|
|
||||||
bool translateready;
|
|
||||||
long idcounter;
|
long idcounter;
|
||||||
long idmethod;
|
long idmethod;
|
||||||
PROCESS_INFORMATION processInfo;
|
PROCESS_INFORMATION processInfo;
|
||||||
|
@ -15,6 +15,7 @@ const wchar_t* ERROR_START_CHROME = L"Error: failed to start chrome or to connec
|
|||||||
const wchar_t* ERROR_GOT_TIMEOUT = L"Error: timeout (s)";
|
const wchar_t* ERROR_GOT_TIMEOUT = L"Error: timeout (s)";
|
||||||
const wchar_t* ERROR_COMMAND_FAIL = L"Error: command failed";
|
const wchar_t* ERROR_COMMAND_FAIL = L"Error: command failed";
|
||||||
const wchar_t* ERROR_LANGUAGE = L"Error: target languages do not match";
|
const wchar_t* ERROR_LANGUAGE = L"Error: target languages do not match";
|
||||||
|
const wchar_t* ERROR_NOTE = L"Error: notification";
|
||||||
|
|
||||||
QString URL = "https://www.deepl.com/en/translator";
|
QString URL = "https://www.deepl.com/en/translator";
|
||||||
QStringList languages
|
QStringList languages
|
||||||
@ -49,7 +50,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
return { true, text };
|
return { true, text };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (devtools->getStatus() == "Stopped")
|
if (devtools->getStatus() == "Stopped")
|
||||||
{
|
{
|
||||||
return { false, FormatString(L"%s", ERROR_CHROME) };
|
return { false, FormatString(L"%s", ERROR_CHROME) };
|
||||||
@ -88,8 +89,8 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
}
|
}
|
||||||
pageenabled = 1;
|
pageenabled = 1;
|
||||||
}
|
}
|
||||||
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument", {});
|
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument");
|
||||||
long target = devtools->methodToReceive("DOM.attributeModified", { {"value" , "lmt__mobile_share_container"} });
|
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
|
||||||
|
|
||||||
// Navigate to site
|
// Navigate to site
|
||||||
QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext;
|
QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext;
|
||||||
@ -102,12 +103,11 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
timer += 0.1;
|
timer += 0.1;
|
||||||
}
|
}
|
||||||
if (timer >= timer_stop)
|
if (timer >= timer_stop)
|
||||||
{
|
{
|
||||||
return { false, FormatString(L"%s: %d ", ERROR_GOT_TIMEOUT, timer_stop) };
|
return { false, FormatString(L"%s: %d ", ERROR_GOT_TIMEOUT, timer_stop) };
|
||||||
}
|
}
|
||||||
QString OuterHTML("<div></div>");
|
|
||||||
|
|
||||||
// Get document
|
// Get document
|
||||||
if (docfound == -1)
|
if (docfound == -1)
|
||||||
@ -139,9 +139,13 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
timer += 0.1;
|
timer += 0.1;
|
||||||
}
|
}
|
||||||
if (timer >= timer_stop)
|
|
||||||
|
// Catch the translation
|
||||||
|
devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", targetNodeId + 1} }, root);
|
||||||
|
QString OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
||||||
|
if (OuterHTML == "<div></div>")
|
||||||
{
|
{
|
||||||
// Catch notification if timeout
|
// Try to catch the notification
|
||||||
int noteNodeId = -1;
|
int noteNodeId = -1;
|
||||||
if (!(devtools->SendRequest("DOM.querySelector", { {"nodeId", docfound}, {"selector", "div.lmt__system_notification"} }, root))
|
if (!(devtools->SendRequest("DOM.querySelector", { {"nodeId", docfound}, {"selector", "div.lmt__system_notification"} }, root))
|
||||||
|| (root.value("result").toObject().value("nodeId").toInt() == 0))
|
|| (root.value("result").toObject().value("nodeId").toInt() == 0))
|
||||||
@ -150,23 +154,15 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
}
|
}
|
||||||
noteNodeId = root.value("result").toObject().value("nodeId").toInt();
|
noteNodeId = root.value("result").toObject().value("nodeId").toInt();
|
||||||
|
|
||||||
if (devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", noteNodeId + 1} }, root))
|
if (devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", noteNodeId} }, root))
|
||||||
{
|
{
|
||||||
OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
||||||
OuterHTML.remove(QRegExp("<[^>]*>"));
|
|
||||||
OuterHTML = OuterHTML.trimmed();
|
|
||||||
}
|
}
|
||||||
else
|
OuterHTML.remove(QRegExp("<[^>]*>"));
|
||||||
{
|
OuterHTML = OuterHTML.trimmed();
|
||||||
OuterHTML = "Could not get notification";
|
|
||||||
}
|
|
||||||
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
|
|
||||||
|
|
||||||
|
return { false, FormatString(L"%s: %s", ERROR_NOTE, S(OuterHTML)) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catch the translation
|
|
||||||
devtools->SendRequest("DOM.getOuterHTML", { {"nodeId", targetNodeId + 1} }, root);
|
|
||||||
OuterHTML = root.value("result").toObject().value("outerHTML").toString();
|
|
||||||
OuterHTML.remove(QRegExp("<[^>]*>"));
|
OuterHTML.remove(QRegExp("<[^>]*>"));
|
||||||
OuterHTML = OuterHTML.trimmed();
|
OuterHTML = OuterHTML.trimmed();
|
||||||
|
|
||||||
@ -199,4 +195,4 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
{
|
{
|
||||||
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
|
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user