2020-10-14 08:37:03 +08:00
|
|
|
|
#include "qtcommon.h"
|
2021-06-29 12:24:59 +08:00
|
|
|
|
#include "translatewrapper.h"
|
2020-10-14 08:37:03 +08:00
|
|
|
|
#include "devtools.h"
|
|
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
|
extern const wchar_t* ERROR_START_CHROME;
|
2021-06-05 22:25:46 +08:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
2021-01-15 21:07:23 +08:00
|
|
|
|
|
2021-03-10 12:32:56 +08:00
|
|
|
|
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
|
|
|
|
|
const char* GET_API_KEY_FROM = nullptr;
|
|
|
|
|
|
2021-06-29 12:24:59 +08:00
|
|
|
|
extern const QStringList languagesTo
|
2020-10-14 08:37:03 +08:00
|
|
|
|
{
|
2021-06-29 12:24:59 +08:00
|
|
|
|
"Bulgarian",
|
|
|
|
|
"Chinese (Simplified)",
|
|
|
|
|
"Czech",
|
|
|
|
|
"Danish",
|
|
|
|
|
"Dutch",
|
|
|
|
|
"English (American)",
|
|
|
|
|
"English (British)",
|
|
|
|
|
"Estonian",
|
|
|
|
|
"Finnish",
|
|
|
|
|
"French",
|
|
|
|
|
"German",
|
|
|
|
|
"Greek",
|
|
|
|
|
"Hungarian",
|
|
|
|
|
"Italian",
|
|
|
|
|
"Japanese",
|
|
|
|
|
"Latvian",
|
|
|
|
|
"Lithuanian",
|
|
|
|
|
"Polish",
|
|
|
|
|
"Portuguese",
|
|
|
|
|
"Portuguese (Brazilian)",
|
|
|
|
|
"Romanian",
|
|
|
|
|
"Russian",
|
|
|
|
|
"Slovak",
|
|
|
|
|
"Slovenian",
|
|
|
|
|
"Spanish",
|
|
|
|
|
"Swedish"
|
|
|
|
|
},
|
|
|
|
|
languagesFrom =
|
|
|
|
|
{
|
|
|
|
|
"Bulgarian",
|
|
|
|
|
"Chinese",
|
|
|
|
|
"Czech",
|
|
|
|
|
"Danish",
|
|
|
|
|
"Dutch",
|
|
|
|
|
"English",
|
|
|
|
|
"Estonian",
|
|
|
|
|
"Finnish",
|
|
|
|
|
"French",
|
|
|
|
|
"German",
|
|
|
|
|
"Greek",
|
|
|
|
|
"Hungarian",
|
|
|
|
|
"Italian",
|
|
|
|
|
"Japanese",
|
|
|
|
|
"Latvian",
|
|
|
|
|
"Lithuanian",
|
|
|
|
|
"Polish",
|
|
|
|
|
"Portuguese",
|
|
|
|
|
"Romanian",
|
|
|
|
|
"Russian",
|
|
|
|
|
"Slovak",
|
|
|
|
|
"Slovenian",
|
|
|
|
|
"Spanish",
|
|
|
|
|
"Swedish"
|
2020-10-14 08:37:03 +08:00
|
|
|
|
};
|
2021-06-29 12:24:59 +08:00
|
|
|
|
extern const std::unordered_map<std::wstring, std::wstring> codes
|
|
|
|
|
{
|
|
|
|
|
{ { L"Bulgarian" }, { L"Bulgarian" } },
|
|
|
|
|
{ { L"Chinese" }, { L"Chinese" } },
|
|
|
|
|
{ { L"Chinese (Simplified)" }, { L"Chinese (simplified)" } },
|
|
|
|
|
{ { L"Czech" }, { L"Czech" } },
|
|
|
|
|
{ { L"Danish" }, { L"Danish" } },
|
|
|
|
|
{ { L"Dutch" }, { L"Dutch" } },
|
|
|
|
|
{ { L"English" }, { L"English" } },
|
|
|
|
|
{ { L"English (American)" }, { L"English (American)" } },
|
|
|
|
|
{ { L"English (British)" }, { L"English (British)" } },
|
|
|
|
|
{ { L"Estonian" }, { L"Estonian" } },
|
|
|
|
|
{ { L"Finnish" }, { L"Finnish" } },
|
|
|
|
|
{ { L"French" }, { L"French" } },
|
|
|
|
|
{ { L"German" }, { L"German" } },
|
|
|
|
|
{ { L"Greek" }, { L"Greek" } },
|
|
|
|
|
{ { L"Hungarian" }, { L"Hungarian" } },
|
|
|
|
|
{ { L"Italian" }, { L"Italian" } },
|
|
|
|
|
{ { L"Japanese" }, { L"Japanese" } },
|
|
|
|
|
{ { L"Latvian" }, { L"Latvian" } },
|
|
|
|
|
{ { L"Lithuanian" }, { L"Lithuanian" } },
|
|
|
|
|
{ { L"Polish" }, { L"Polish" } },
|
|
|
|
|
{ { L"Portuguese" }, { L"Portuguese" } },
|
|
|
|
|
{ { L"Portuguese (Brazilian)" }, { L"Portuguese (Brazilian)" } },
|
|
|
|
|
{ { L"Romanian" }, { L"Romanian" } },
|
|
|
|
|
{ { L"Russian" }, { L"Russian" } },
|
|
|
|
|
{ { L"Slovak" }, { L"Slovak" } },
|
|
|
|
|
{ { L"Slovenian" }, { L"Slovenian" } },
|
|
|
|
|
{ { L"Spanish" }, { L"Spanish" } },
|
|
|
|
|
{ { L"Swedish" }, { L"Swedish" } },
|
|
|
|
|
{ { L"?" }, { L"Any language (detect)" } }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
|
2021-07-01 07:52:52 +08:00
|
|
|
|
int tokenCount = 30, rateLimitTimespan = 60000, maxSentenceSize = 2500;
|
2020-10-14 08:37:03 +08:00
|
|
|
|
|
2021-01-15 21:07:23 +08:00
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
2020-10-14 08:37:03 +08:00
|
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
|
switch (ul_reason_for_call)
|
2020-10-23 04:15:45 +08:00
|
|
|
|
{
|
2021-01-15 21:07:23 +08:00
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2020-10-20 07:08:59 +08:00
|
|
|
|
{
|
2021-06-07 12:43:40 +08:00
|
|
|
|
DevTools::Initialize();
|
2021-01-15 21:07:23 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
DevTools::Close();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 12:24:59 +08:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
|
2021-01-15 21:07:23 +08:00
|
|
|
|
{
|
|
|
|
|
if (!DevTools::Connected()) return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, ERROR_START_CHROME) };
|
|
|
|
|
// DevTools can't handle concurrent translations yet
|
|
|
|
|
static std::mutex translationMutex;
|
|
|
|
|
std::scoped_lock lock(translationMutex);
|
2021-06-29 12:24:59 +08:00
|
|
|
|
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#en/en/%s"})", Escape(text)));
|
|
|
|
|
for (int retry = 0; ++retry < 20; Sleep(100))
|
|
|
|
|
if (Copy(DevTools::SendRequest("Runtime.evaluate", LR"({"expression":"document.readyState"})")[L"result"][L"value"].String()) == L"complete") break;
|
2021-03-10 12:32:56 +08:00
|
|
|
|
|
2021-06-29 12:24:59 +08:00
|
|
|
|
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"
|
|
|
|
|
document.querySelector('.lmt__language_select--source').querySelector('button').click();
|
|
|
|
|
document.evaluate(`//button[text()='%s']`,document.querySelector('.lmt__language_select__menu'),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click();
|
|
|
|
|
document.querySelector('.lmt__language_select--target').querySelector('button').click();
|
|
|
|
|
document.evaluate(`//button[text()='%s']`,document.querySelector('.lmt__language_select__menu'),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click();
|
|
|
|
|
"})", codes.at(tlp.translateFrom), codes.at(tlp.translateTo)));
|
2021-03-10 12:32:56 +08:00
|
|
|
|
|
2021-01-16 00:32:23 +08:00
|
|
|
|
for (int retry = 0; ++retry < 100; Sleep(100))
|
2021-03-10 12:32:56 +08:00
|
|
|
|
if (auto translation = Copy(DevTools::SendRequest("Runtime.evaluate",
|
|
|
|
|
LR"({"expression":"document.querySelector('#target-dummydiv').innerHTML.trim() ","returnByValue":true})"
|
|
|
|
|
)[L"result"][L"value"].String())) if (!translation->empty()) return { true, translation.value() };
|
|
|
|
|
if (auto errorMessage = Copy(DevTools::SendRequest("Runtime.evaluate",
|
|
|
|
|
LR"({"expression":"document.querySelector('div.lmt__system_notification').innerHTML","returnByValue":true})"
|
|
|
|
|
)[L"result"][L"value"].String())) return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, errorMessage.value()) };
|
2021-01-15 21:07:23 +08:00
|
|
|
|
return { false, TRANSLATION_ERROR };
|
2021-01-21 22:07:09 +08:00
|
|
|
|
}
|