mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 08:54: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,11 +4,9 @@ DevTools::DevTools(QObject* parent) :
|
||||
QObject(parent),
|
||||
idcounter(0),
|
||||
idmethod(0),
|
||||
pagenavigated(false),
|
||||
translateready(false),
|
||||
status("Stopped"),
|
||||
session(0)
|
||||
{
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) != FALSE) && (exitCode == STILL_ACTIVE))
|
||||
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())
|
||||
+ "\\devtoolscache --remote-debugging-port="
|
||||
+ "\\devtoolscache --remote-debugging-port="
|
||||
+ QString::number(port);
|
||||
if (headless)
|
||||
args += " --headless";
|
||||
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))
|
||||
return false;
|
||||
else
|
||||
@ -127,8 +125,7 @@ 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);
|
||||
json.insert("params", params);
|
||||
QJsonDocument doc(json);
|
||||
QString message(doc.toJson(QJsonDocument::Compact));
|
||||
mutex.lock();
|
||||
@ -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()))
|
||||
return false;
|
||||
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;
|
||||
}
|
||||
|
||||
@ -223,9 +230,8 @@ 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()))))
|
||||
if ((iter->second.value("method") == root.value("method"))
|
||||
&& (compareJson(iter->second.value("params"), root.value("params"))))
|
||||
{
|
||||
mutex.lock();
|
||||
mapmethod.erase(iter++);
|
||||
@ -248,7 +254,7 @@ void DevTools::onTextMessageReceived(QString message)
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DevTools::closeDevTools()
|
||||
@ -289,4 +295,4 @@ void DevTools::closeDevTools()
|
||||
}
|
||||
status = "Stopped";
|
||||
emit statusChanged(status);
|
||||
}
|
||||
}
|
@ -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
|
||||
@ -49,7 +50,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
||||
return { true, text };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (devtools->getStatus() == "Stopped")
|
||||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
@ -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));
|
||||
timer += 0.1;
|
||||
}
|
||||
}
|
||||
if (timer >= timer_stop)
|
||||
{
|
||||
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) };
|
||||
OuterHTML.remove(QRegExp("<[^>]*>"));
|
||||
OuterHTML = OuterHTML.trimmed();
|
||||
|
||||
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();
|
||||
|
||||
@ -199,4 +195,4 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|
||||
{
|
||||
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user