2021-05-15 11:07:22 +02:00
|
|
|
|
#include "qtcommon.h"
|
2021-06-28 22:24:59 -06:00
|
|
|
|
#include "translatewrapper.h"
|
2021-05-15 11:07:22 +02:00
|
|
|
|
#include "devtools.h"
|
|
|
|
|
|
|
|
|
|
extern const wchar_t* ERROR_START_CHROME;
|
2021-06-05 09:43:53 -06:00
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
2021-05-15 11:07:22 +02:00
|
|
|
|
|
|
|
|
|
const char* TRANSLATION_PROVIDER = "DevTools Papago Translate";
|
|
|
|
|
const char* GET_API_KEY_FROM = nullptr;
|
|
|
|
|
|
2021-06-28 22:24:59 -06:00
|
|
|
|
extern const QStringList languagesTo
|
|
|
|
|
{
|
|
|
|
|
"Chinese (Simplified)",
|
|
|
|
|
"Chinese (Traditional)",
|
|
|
|
|
"English",
|
|
|
|
|
"French",
|
|
|
|
|
"German",
|
|
|
|
|
"Hindi",
|
|
|
|
|
"Indonesian",
|
|
|
|
|
"Italian",
|
|
|
|
|
"Japanese",
|
|
|
|
|
"Korean",
|
|
|
|
|
"Portuguese",
|
|
|
|
|
"Russian",
|
|
|
|
|
"Spanish",
|
|
|
|
|
"Thai",
|
|
|
|
|
"Vietnamese",
|
|
|
|
|
}, languagesFrom = languagesTo;
|
|
|
|
|
extern const std::unordered_map<std::wstring, std::wstring> codes
|
2021-05-15 11:07:22 +02:00
|
|
|
|
{
|
2021-06-28 22:24:59 -06:00
|
|
|
|
{ { L"Chinese (Simplified)" }, { L"zh-CN" } },
|
|
|
|
|
{ { L"Chinese (Traditional)" }, { L"zt-TW" } },
|
|
|
|
|
{ { L"English" }, { L"en" } },
|
|
|
|
|
{ { L"French" }, { L"fr" } },
|
|
|
|
|
{ { L"German" }, { L"de" } },
|
|
|
|
|
{ { L"Hindi" }, { L"hi" } },
|
|
|
|
|
{ { L"Indonesian" }, { L"id" } },
|
|
|
|
|
{ { L"Italian" }, { L"it" } },
|
|
|
|
|
{ { L"Japanese" }, { L"ja" } },
|
|
|
|
|
{ { L"Korean" }, { L"ko" } },
|
|
|
|
|
{ { L"Portuguese" }, { L"pt" } },
|
|
|
|
|
{ { L"Russian" }, { L"ru" } },
|
|
|
|
|
{ { L"Spanish" }, { L"es" } },
|
|
|
|
|
{ { L"Thai" }, { L"th" } },
|
|
|
|
|
{ { L"Vietnamese" }, { L"vi" } },
|
|
|
|
|
{ { L"?" }, { L"auto" } }
|
2021-05-15 11:07:22 +02:00
|
|
|
|
};
|
2021-06-28 22:24:59 -06:00
|
|
|
|
|
|
|
|
|
bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true;
|
|
|
|
|
int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
|
2021-05-15 11:07:22 +02:00
|
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
|
{
|
|
|
|
|
switch (ul_reason_for_call)
|
|
|
|
|
{
|
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
|
{
|
2021-06-06 22:43:40 -06:00
|
|
|
|
DevTools::Initialize();
|
2021-05-15 11:07:22 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
DevTools::Close();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 22:24:59 -06:00
|
|
|
|
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
|
2021-05-15 11:07:22 +02: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-28 22:24:59 -06:00
|
|
|
|
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://papago.naver.com/?sk=%s&tk=%s&st=%s"})", codes.at(tlp.translateFrom), codes.at(tlp.translateTo), Escape(text)));
|
2021-05-15 11:07:22 +02:00
|
|
|
|
for (int retry = 0; ++retry < 100; Sleep(100))
|
|
|
|
|
if (auto translation = Copy(DevTools::SendRequest("Runtime.evaluate",
|
|
|
|
|
LR"({"expression":"document.querySelector('#txtTarget').textContent.trim() ","returnByValue":true})"
|
|
|
|
|
)[L"result"][L"value"].String())) if (!translation->empty()) return { true, translation.value() };
|
|
|
|
|
return { false, TRANSLATION_ERROR };
|
|
|
|
|
}
|