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),
|
||||
idcounter(0),
|
||||
idmethod(0),
|
||||
pagenavigated(false),
|
||||
translateready(false),
|
||||
status("Stopped"),
|
||||
session(0)
|
||||
{
|
||||
@ -127,7 +125,6 @@ bool DevTools::SendRequest(QString method, QJsonObject params, QJsonObject& root
|
||||
long id = idIncrement();
|
||||
json.insert("id", id);
|
||||
json.insert("method", method);
|
||||
if (!params.isEmpty())
|
||||
json.insert("params", params);
|
||||
QJsonDocument doc(json);
|
||||
QString message(doc.toJson(QJsonDocument::Compact));
|
||||
@ -190,17 +187,27 @@ bool DevTools::isConnected()
|
||||
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())
|
||||
return false;
|
||||
if (storedparams.value(key) != params.value(key))
|
||||
return false;
|
||||
if (!compareJson(storedparams.value(key).toObject(), params.value(key).toObject()))
|
||||
foreach(const QString & key, storedparams.toObject().keys())
|
||||
{
|
||||
QJsonValue storedvalue = storedparams.toObject().value(key);
|
||||
QJsonValue value = params.toObject().value(key);
|
||||
if (!compareJson(storedvalue, value))
|
||||
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;
|
||||
}
|
||||
|
||||
@ -224,8 +231,7 @@ void DevTools::onTextMessageReceived(QString message)
|
||||
for (auto iter = mapmethod.cbegin(); iter != mapmethod.cend();)
|
||||
{
|
||||
if ((iter->second.value("method") == root.value("method"))
|
||||
&& ((iter->second.value("params").toObject().isEmpty())
|
||||
|| (compareJson(iter->second.value("params").toObject(), root.value("params").toObject()))))
|
||||
&& (compareJson(iter->second.value("params"), root.value("params"))))
|
||||
{
|
||||
mutex.lock();
|
||||
mapmethod.erase(iter++);
|
||||
|
@ -16,7 +16,7 @@ public:
|
||||
~DevTools();
|
||||
|
||||
Q_SIGNALS:
|
||||
void statusChanged(const QString &);
|
||||
void statusChanged(const QString&);
|
||||
|
||||
private Q_SLOTS:
|
||||
void stateChanged(QAbstractSocket::SocketState state);
|
||||
@ -28,7 +28,7 @@ public:
|
||||
bool checkMethod(long id);
|
||||
int getSession();
|
||||
bool SendRequest(QString method, QJsonObject params, QJsonObject& root);
|
||||
long methodToReceive(QString method, QJsonObject params);
|
||||
long methodToReceive(QString method, QJsonObject params = {});
|
||||
QString getStatus();
|
||||
|
||||
private:
|
||||
@ -37,14 +37,12 @@ private:
|
||||
bool GetwebSocketDebuggerUrl(QString& url, int port = 9222);
|
||||
long idIncrement();
|
||||
long idmIncrement();
|
||||
bool compareJson(QJsonObject storedparams, QJsonObject params);
|
||||
bool compareJson(QJsonValue storedparams, QJsonValue params);
|
||||
int session;
|
||||
QWebSocket webSocket;
|
||||
std::mutex mutex;
|
||||
MapResponse mapqueue;
|
||||
MapMethod mapmethod;
|
||||
bool pagenavigated;
|
||||
bool translateready;
|
||||
long idcounter;
|
||||
long idmethod;
|
||||
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_COMMAND_FAIL = L"Error: command failed";
|
||||
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";
|
||||
QStringList languages
|
||||
@ -88,8 +89,8 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
||||
}
|
||||
pageenabled = 1;
|
||||
}
|
||||
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument", {});
|
||||
long target = devtools->methodToReceive("DOM.attributeModified", { {"value" , "lmt__mobile_share_container"} });
|
||||
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument");
|
||||
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
|
||||
|
||||
// Navigate to site
|
||||
QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext;
|
||||
@ -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) };
|
||||
}
|
||||
QString OuterHTML("<div></div>");
|
||||
|
||||
// Get document
|
||||
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));
|
||||
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;
|
||||
if (!(devtools->SendRequest("DOM.querySelector", { {"nodeId", docfound}, {"selector", "div.lmt__system_notification"} }, root))
|
||||
|| (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();
|
||||
|
||||
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.remove(QRegExp("<[^>]*>"));
|
||||
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 = OuterHTML.trimmed();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user