2019-02-11 10:46:39 +08:00
|
|
|
|
#include "extension.h"
|
|
|
|
|
#include "defs.h"
|
2018-12-19 05:32:28 +08:00
|
|
|
|
#include "text.h"
|
2019-02-11 10:46:39 +08:00
|
|
|
|
#include "network.h"
|
2018-10-18 05:36:56 +08:00
|
|
|
|
#include <QInputDialog>
|
2018-10-28 14:28:08 +08:00
|
|
|
|
#include <QTimer>
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"English: en",
|
|
|
|
|
"Arabic: ar",
|
|
|
|
|
"Bosnian: bs-Latn",
|
|
|
|
|
"Bulgarian: bg",
|
|
|
|
|
"Catalan: ca",
|
|
|
|
|
"Chinese(Simplified): zh-CHS",
|
|
|
|
|
"Chinese(Traditional): zh-CHT",
|
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Estonian: et",
|
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
|
|
|
|
"Hebrew: he",
|
|
|
|
|
"Hindi: hi",
|
|
|
|
|
"Hungarian: hu",
|
|
|
|
|
"Indonesian: id",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
|
|
|
|
"Klingon: tlh",
|
|
|
|
|
"Korean: ko",
|
2018-10-18 05:36:56 +08:00
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
|
|
|
|
"Malay: ms",
|
|
|
|
|
"Maltese: mt",
|
|
|
|
|
"Norwegian: no",
|
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
2018-10-28 14:28:08 +08:00
|
|
|
|
"Portuguese: pt",
|
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
|
|
|
|
"Serbian: sr-Cyrl",
|
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Spanish: es",
|
|
|
|
|
"Swedish: sv",
|
|
|
|
|
"Thai: th",
|
|
|
|
|
"Turkish: tr",
|
|
|
|
|
"Ukranian: uk",
|
|
|
|
|
"Urdu: ur",
|
|
|
|
|
"Vietnamese: vi",
|
|
|
|
|
"Welsh: cy"
|
2018-10-18 05:36:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
std::wstring translateTo = L"en";
|
2018-10-28 14:28:08 +08:00
|
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
|
{
|
|
|
|
|
switch (ul_reason_for_call)
|
|
|
|
|
{
|
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
|
{
|
|
|
|
|
QTimer::singleShot(0, []
|
|
|
|
|
{
|
2018-11-04 17:31:49 +08:00
|
|
|
|
translateTo = QInputDialog::getItem(
|
|
|
|
|
nullptr,
|
2018-12-19 05:32:28 +08:00
|
|
|
|
SELECT_LANGUAGE,
|
|
|
|
|
BING_PROMPT,
|
2018-11-04 17:31:49 +08:00
|
|
|
|
languages,
|
|
|
|
|
0, false, nullptr,
|
|
|
|
|
Qt::WindowCloseButtonHint
|
|
|
|
|
).split(" ")[1].toStdWString();
|
2018-10-28 14:28:08 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
// This function detects language and returns it if translateFrom is empty
|
|
|
|
|
std::wstring Translate(const std::wstring& text, std::wstring translateFrom, std::wstring translateTo)
|
2018-10-18 05:36:56 +08:00
|
|
|
|
{
|
2019-01-24 02:41:50 +08:00
|
|
|
|
static std::atomic<HINTERNET> internet = NULL;
|
2018-10-18 22:13:33 +08:00
|
|
|
|
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
std::wstring escapedText;
|
|
|
|
|
for (unsigned char ch : WideStringToString(text))
|
2018-11-02 09:32:13 +08:00
|
|
|
|
{
|
2019-02-11 10:46:39 +08:00
|
|
|
|
wchar_t escapedChar[4] = {};
|
|
|
|
|
swprintf_s<4>(escapedChar, L"%%%02X", (int)ch);
|
|
|
|
|
escapedText = escapedChar;
|
2018-11-02 09:32:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
std::wstring location = translateFrom.empty()
|
|
|
|
|
? L"/tdetect?text=" + text
|
|
|
|
|
: L"/ttranslate?from=" + translateFrom + L"&to=" + translateTo + L"&text=" + text;
|
2018-10-18 05:36:56 +08:00
|
|
|
|
std::wstring translation;
|
|
|
|
|
if (internet)
|
2019-02-11 10:46:39 +08:00
|
|
|
|
if (InternetHandle connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
|
if (InternetHandle request = WinHttpOpenRequest(connection, L"POST", location.c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
2018-10-18 05:36:56 +08:00
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
2019-02-11 10:46:39 +08:00
|
|
|
|
if (auto response = ReceiveHttpRequest(request))
|
|
|
|
|
if (translateFrom.empty()) translation = response.value();
|
|
|
|
|
else if (std::wsmatch results; std::regex_search(response.value(), results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
|
|
|
|
|
|
|
|
|
|
Escape(translation);
|
2018-10-19 10:52:27 +08:00
|
|
|
|
return translation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
|
{
|
2018-11-04 11:26:27 +08:00
|
|
|
|
if (sentenceInfo["hook address"] == -1) return false;
|
2018-10-18 05:36:56 +08:00
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
static RateLimiter rateLimiter(30, 60 * 1000);
|
2019-01-22 03:22:47 +08:00
|
|
|
|
|
2019-02-11 10:46:39 +08:00
|
|
|
|
std::wstring translation;
|
|
|
|
|
if (!(rateLimiter.Request() || sentenceInfo["current select"])) translation = TOO_MANY_TRANS_REQUESTS;
|
|
|
|
|
else translation = Translate(sentence, Translate(sentence, L"", translateTo), translateTo);
|
2018-12-19 05:32:28 +08:00
|
|
|
|
if (translation.empty()) translation = TRANSLATION_ERROR;
|
2019-01-11 10:51:20 +08:00
|
|
|
|
sentence += L"\n" + translation;
|
2018-10-18 05:36:56 +08:00
|
|
|
|
return true;
|
2018-12-19 05:32:28 +08:00
|
|
|
|
}
|
2019-02-11 10:46:39 +08:00
|
|
|
|
|
|
|
|
|
TEST(
|
|
|
|
|
{
|
|
|
|
|
std::wstring test = L"こんにちは";
|
|
|
|
|
ProcessSentence(test, SentenceInfo{ SentenceInfo::DUMMY });
|
|
|
|
|
assert(test.find(L"Hello") != std::wstring::npos);
|
|
|
|
|
}
|
|
|
|
|
);
|