mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 17:04:12 +08:00
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,8 +4,6 @@ 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)
|
||||||
{
|
{
|
||||||
@ -127,7 +125,6 @@ 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));
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,8 +231,7 @@ 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++);
|
||||||
|
@ -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
|
||||||
@ -88,7 +89,7 @@ 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
|
||||||
@ -107,7 +108,6 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
|||||||
{
|
{
|
||||||
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.remove(QRegExp("<[^>]*>"));
|
||||||
OuterHTML = OuterHTML.trimmed();
|
OuterHTML = OuterHTML.trimmed();
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user