2020-10-14 03:37:03 +03:00
|
|
|
|
#include "qtcommon.h"
|
|
|
|
|
#include "devtools.h"
|
|
|
|
|
|
2021-01-15 06:07:23 -07:00
|
|
|
|
extern const wchar_t* ERROR_START_CHROME;
|
2021-06-05 08:25:46 -06:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
2021-01-15 06:07:23 -07:00
|
|
|
|
|
2021-03-09 21:32:56 -07:00
|
|
|
|
extern Synchronized<std::wstring> translateTo, translateFrom;
|
|
|
|
|
|
|
|
|
|
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
|
|
|
|
|
const char* GET_API_KEY_FROM = nullptr;
|
|
|
|
|
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
|
|
|
|
|
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
|
|
|
|
|
|
2021-01-15 06:07:23 -07:00
|
|
|
|
QStringList languages
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2021-06-05 08:25:46 -06:00
|
|
|
|
"Bulgarian: BG",
|
|
|
|
|
"Chinese: ZH",
|
|
|
|
|
"Czech: CS",
|
|
|
|
|
"Danish: DA",
|
|
|
|
|
"Dutch: NL",
|
|
|
|
|
"English: EN",
|
|
|
|
|
"Estonian: ET",
|
|
|
|
|
"Finnish: FI",
|
|
|
|
|
"French: FR",
|
|
|
|
|
"German: DE",
|
|
|
|
|
"Greek: EL",
|
|
|
|
|
"Hungarian: HU",
|
|
|
|
|
"Italian: IT",
|
|
|
|
|
"Japanese: JA",
|
|
|
|
|
"Latvian: LV",
|
|
|
|
|
"Lithuanian: LT",
|
|
|
|
|
"Polish: PL",
|
|
|
|
|
"Portuguese: PT",
|
|
|
|
|
"Romanian: RO",
|
|
|
|
|
"Russian: RU",
|
|
|
|
|
"Slovak: SK",
|
|
|
|
|
"Slovenian: SL",
|
|
|
|
|
"Spanish: ES",
|
|
|
|
|
"Swedish: SV"
|
2020-10-14 03:37:03 +03:00
|
|
|
|
};
|
2021-01-21 07:07:09 -07:00
|
|
|
|
std::wstring autoDetectLanguage = L"auto";
|
2020-10-14 03:37:03 +03:00
|
|
|
|
|
2021-01-15 06:07:23 -07:00
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
2020-10-14 03:37:03 +03:00
|
|
|
|
{
|
2021-01-15 06:07:23 -07:00
|
|
|
|
switch (ul_reason_for_call)
|
2020-10-22 23:15:45 +03:00
|
|
|
|
{
|
2021-01-15 06:07:23 -07:00
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2020-10-20 02:08:59 +03:00
|
|
|
|
{
|
2021-06-05 08:25:46 -06:00
|
|
|
|
DevTools::Start();
|
2021-01-15 06:07:23 -07:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
DevTools::Close();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
|
|
|
|
{
|
|
|
|
|
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-05 08:25:46 -06:00
|
|
|
|
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#%s/%s/%s"})", translateTo.Copy(), translateTo.Copy(), Escape(text)));
|
2021-03-09 21:32:56 -07:00
|
|
|
|
|
|
|
|
|
if (translateFrom.Copy() != autoDetectLanguage)
|
|
|
|
|
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"
|
2021-03-22 01:18:56 -06:00
|
|
|
|
document.querySelector('.lmt__language_select--source').querySelector('button').click();
|
|
|
|
|
document.evaluate(`//button[contains(text(),'%s')]`,document.querySelector('.lmt__language_select__menu'),null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click();
|
2021-03-09 21:32:56 -07:00
|
|
|
|
"})", S(std::find_if(languages.begin(), languages.end(), [end = S(translateFrom.Copy())](const QString& language) { return language.endsWith(end); })->split(":")[0])));
|
|
|
|
|
|
2021-01-15 09:32:23 -07:00
|
|
|
|
for (int retry = 0; ++retry < 100; Sleep(100))
|
2021-03-09 21:32:56 -07: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 06:07:23 -07:00
|
|
|
|
return { false, TRANSLATION_ERROR };
|
2021-01-21 07:07:09 -07:00
|
|
|
|
}
|