From ea6ddc7a7cf5dde7f12db7f73f6c9e401a5ce6f0 Mon Sep 17 00:00:00 2001 From: zeheyler Date: Wed, 9 Dec 2020 14:00:57 +0300 Subject: [PATCH] small improvements Added check for the empty answer from the translator Small refactors --- extensions/devtoolsdeepltranslate.cpp | 34 +++++++++++++++++---------- extensions/devtoolswrapper.cpp | 5 ++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/extensions/devtoolsdeepltranslate.cpp b/extensions/devtoolsdeepltranslate.cpp index f227706..f60b52a 100644 --- a/extensions/devtoolsdeepltranslate.cpp +++ b/extensions/devtoolsdeepltranslate.cpp @@ -15,6 +15,7 @@ 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"; +const wchar_t* ERROR_EMPTY_ANSWER = L"Error: empty translation"; QString URL = "https://www.deepl.com/en/translator"; QStringList languagesTo @@ -56,6 +57,7 @@ std::pair Translate(const std::wstring& text, DevTools* devT { QString qtext = S(text); qtext.remove(QString(12288)); // japanese space (no need for translator) + qtext.replace(QString(12289), ","); // replace the japanese comma with the latin comma for correct translation // Remove quotes bool checkQuote = false; @@ -148,7 +150,7 @@ std::pair Translate(const std::wstring& text, DevTools* devT std::this_thread::sleep_for(std::chrono::milliseconds(100)); timer += 0.1; } - if (timer >= timerStop) + if (timer >= 2 * timerStop) errorCode = 5; // Set methods to receive @@ -243,23 +245,29 @@ std::pair Translate(const std::wstring& text, DevTools* devT // Catch the translation QString OuterHTML; - if (errorCode == 0 && !devTools->SendRequest("DOM.getOuterHTML", { {"nodeId", targetNodeId + 1} }, root)) + if (errorCode == 0) { - targetNodeId = -1; - errorCode = 1; + if (!devTools->SendRequest("DOM.getOuterHTML", { {"nodeId", targetNodeId + 1} }, root)) + { + targetNodeId = -1; + errorCode = 1; + } + else + { + OuterHTML = root.value("result").toObject().value("outerHTML").toString(); + } } - else - { - OuterHTML = root.value("result").toObject().value("outerHTML").toString(); - } - if (OuterHTML == "
") + if (errorCode == 0 && OuterHTML == "
") { // Try to catch the notification int noteNodeId = -1; - if (errorCode == 0 && (!devTools->SendRequest("DOM.querySelector", { {"nodeId", docFound}, {"selector", "div.lmt__system_notification"} }, root) - || root.value("result").toObject().value("nodeId").toInt() == 0) && timer >= timerStop) + if (!devTools->SendRequest("DOM.querySelector", { {"nodeId", docFound}, {"selector", "div.lmt__system_notification"} }, root) + || root.value("result").toObject().value("nodeId").toInt() == 0) { - errorCode = 2; + if (timer >= timerStop) + errorCode = 2; + else + errorCode = 6; } else { @@ -340,6 +348,8 @@ std::pair Translate(const std::wstring& text, DevTools* devT return { false, FormatString(L"%s (%s): %s", ERROR_LANGUAGE, S(targetLang), S(OuterHTML)) }; else if (errorCode == 5) return { false, FormatString(L"%s: %d", ERROR_GOT_TIMEOUT, 2*timerStop) }; + else if (errorCode == 6) + return { false, FormatString(L"%s", ERROR_EMPTY_ANSWER) }; else return { false, FormatString(L"%s", TRANSLATION_ERROR) }; } \ No newline at end of file diff --git a/extensions/devtoolswrapper.cpp b/extensions/devtoolswrapper.cpp index 546d5a2..19fe29c 100644 --- a/extensions/devtoolswrapper.cpp +++ b/extensions/devtoolswrapper.cpp @@ -62,6 +62,11 @@ void EraseControlCharacters(std::wstring& text) { text.erase(it--); } + if (*it == '\\' && *(it + 1) == 'n') + { + text.erase((it + 1)--); + text.erase(it--); + } } }