Merge remote-tracking branch 'Blu3train/fixDevtoolsdeepltranslateWrongTargetLanguageSelection'

This commit is contained in:
Chenx221 2024-09-05 12:42:16 +08:00
commit 287a0365b9

View File

@ -7,6 +7,7 @@ extern const wchar_t* TRANSLATION_ERROR;
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
const char* GET_API_KEY_FROM = nullptr;
std::wstring currTranslateTo;
extern const QStringList languagesTo
{
@ -23,10 +24,13 @@ extern const QStringList languagesTo
"German",
"Greek",
"Hungarian",
"Indonesian",
"Italian",
"Japanese",
"Korean",
"Latvian",
"Lithuanian",
"Norwegian",
"Polish",
"Portuguese",
"Portuguese (Brazilian)",
@ -35,7 +39,9 @@ extern const QStringList languagesTo
"Slovak",
"Slovenian",
"Spanish",
"Swedish"
"Swedish",
"Turkish",
"Ukrainian"
},
languagesFrom =
{
@ -51,10 +57,13 @@ languagesFrom =
"German",
"Greek",
"Hungarian",
"Indonesian",
"Italian",
"Japanese",
"Korean",
"Latvian",
"Lithuanian",
"Norwegian",
"Polish",
"Portuguese",
"Romanian",
@ -62,39 +71,46 @@ languagesFrom =
"Slovak",
"Slovenian",
"Spanish",
"Swedish"
"Swedish",
"Turkish",
"Ukrainian"
};
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"Detect language" } }
{ { L"Bulgarian" }, { L"bg-BG" } },
{ { L"Chinese" }, { L"zh" } },
{ { L"Chinese (Simplified)" }, { L"zh-ZH" } },
{ { L"Czech" }, { L"cs-CS" } },
{ { L"Danish" }, { L"da-DA" } },
{ { L"Dutch" }, { L"nl-NL" } },
{ { L"English" }, { L"en" } },
{ { L"English (American)" }, { L"en-US" } },
{ { L"English (British)" }, { L"en-GB" } },
{ { L"Estonian" }, { L"et-ET" } },
{ { L"Finnish" }, { L"fi-FI" } },
{ { L"French" }, { L"fr-FR" } },
{ { L"German" }, { L"de-DE" } },
{ { L"Greek" }, { L"el-EL" } },
{ { L"Hungarian" }, { L"hu-HU" } },
{ { L"Indonesian" }, { L"id-ID" } },
{ { L"Italian" }, { L"it-IT" } },
{ { L"Japanese" }, { L"ja-JA" } },
{ { L"Korean" }, { L"ko-KO" } },
{ { L"Latvian" }, { L"lv-LV" } },
{ { L"Lithuanian" }, { L"lt-LT" } },
{ { L"Norwegian" }, { L"nb-NB" } },
{ { L"Polish" }, { L"pl-PL" } },
{ { L"Portuguese" }, { L"pt-PT" } },
{ { L"Portuguese (Brazilian)" }, { L"pt-BR" } },
{ { L"Romanian" }, { L"ro-RO" } },
{ { L"Russian" }, { L"ru-RU" } },
{ { L"Slovak" }, { L"sk-SK" } },
{ { L"Slovenian" }, { L"sl-SL" } },
{ { L"Spanish" }, { L"es-ES" } },
{ { L"Swedish" }, { L"sv-SV" } },
{ { L"Turkish" }, { L"tr-TR" } },
{ { L"Ukrainian" }, { L"uk-UK" } },
{ { L"?" }, { L"auto" } }
};
bool translateSelectedOnly = true, useRateLimiter = true, rateLimitSelected = false, useCache = true, useFilter = true;
@ -119,6 +135,21 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
return TRUE;
}
std::wstring htmlDecode (std::wstring text) {
const std::wstring enc[] = { L"&amp;", L"&lt;", L"&gt;" };
const std::wstring dec[] = { L"&", L"<", L">" };
size_t pos;
for(int j = 0; j < 3; j++) {
do {
pos = text.find(enc[j]);
if (pos != std::wstring::npos)
text.replace (pos,enc[j].length(),dec[j]);
} while (pos != std::wstring::npos);
}
return text;
}
std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationParam tlp)
{
if (!DevTools::Connected()) return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, ERROR_START_CHROME) };
@ -127,21 +158,18 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, TranslationPar
std::scoped_lock lock(translationMutex);
std::wstring escaped; // DeepL breaks with slash in input
for (auto ch : text) ch == '/' ? escaped += L"\\/" : escaped += ch;
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#en/en/%s"})", Escape(escaped)));
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;
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"
document.querySelector('.lmt__language_select--source').querySelector('button').click();
document.evaluate(`//*[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(`//*[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)));
if (currTranslateTo == tlp.translateTo)
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#%s/%s/%s"})", (tlp.translateFrom == L"?") ? codes.at(tlp.translateFrom) : codes.at(tlp.translateFrom).substr(0, 2), codes.at(tlp.translateTo).substr(0, 2), Escape(escaped)));
else
{
currTranslateTo = tlp.translateTo;
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#%s/%s/%s"})", (tlp.translateFrom == L"?") ? codes.at(tlp.translateFrom) : codes.at(tlp.translateFrom).substr(0, 2), codes.at(tlp.translateTo), Escape(escaped)));
}
for (int retry = 0; ++retry < 100; Sleep(100))
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() };
LR"({"expression":"document.querySelector('[data-testid=translator-target-input]').textContent.trim() ","returnByValue":true})"
)[L"result"][L"value"].String())) if (!translation->empty()) return { true, htmlDecode(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()) };