diff --git a/extensions/devtools.cpp b/extensions/devtools.cpp index aa5c517..0330e29 100644 --- a/extensions/devtools.cpp +++ b/extensions/devtools.cpp @@ -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); -} +} \ No newline at end of file diff --git a/extensions/devtools.h b/extensions/devtools.h index fa4cb5f..546eb9e 100644 --- a/extensions/devtools.h +++ b/extensions/devtools.h @@ -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; diff --git a/extensions/devtoolsdeepltranslate.cpp b/extensions/devtoolsdeepltranslate.cpp index 06fb6af..7ad0956 100644 --- a/extensions/devtoolsdeepltranslate.cpp +++ b/extensions/devtoolsdeepltranslate.cpp @@ -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 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 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 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("
"); // Get document if (docfound == -1) @@ -139,9 +139,13 @@ std::pair 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 == "
") { - // 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 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 Translate(const std::wstring& text, DevTools* devt { return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; } -} +} \ No newline at end of file