2018-11-03 23:26:27 -04:00
|
|
|
#include "../extension.h"
|
2018-10-17 17:36:56 -04:00
|
|
|
#include <winhttp.h>
|
|
|
|
#include <QInputDialog>
|
2018-10-28 02:28:08 -04:00
|
|
|
#include <QTimer>
|
2018-10-17 17:36:56 -04: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 02:28:08 -04: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-17 17:36:56 -04:00
|
|
|
"Latvian: lv",
|
|
|
|
"Lithuanian: lt",
|
|
|
|
"Malay: ms",
|
|
|
|
"Maltese: mt",
|
|
|
|
"Norwegian: no",
|
|
|
|
"Persian: fa",
|
|
|
|
"Polish: pl",
|
2018-10-28 02:28:08 -04: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-17 17:36:56 -04:00
|
|
|
};
|
|
|
|
|
2018-10-28 02:28:08 -04:00
|
|
|
std::wstring translateTo;
|
|
|
|
|
|
|
|
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 04:31:49 -05:00
|
|
|
translateTo = QInputDialog::getItem(
|
|
|
|
nullptr,
|
|
|
|
"Select Language",
|
|
|
|
"What language should Bing translate to?",
|
|
|
|
languages,
|
|
|
|
0, false, nullptr,
|
|
|
|
Qt::WindowCloseButtonHint
|
|
|
|
).split(" ")[1].toStdWString();
|
2018-10-28 02:28:08 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
{
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-10-18 22:52:27 -04:00
|
|
|
// This function detects language and puts it in translateFrom if it's empty
|
|
|
|
std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstring translateTo)
|
2018-10-17 17:36:56 -04:00
|
|
|
{
|
2018-10-18 10:13:33 -04:00
|
|
|
static HINTERNET internet = NULL;
|
|
|
|
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
|
|
|
|
2018-11-03 23:26:27 -04:00
|
|
|
char utf8[10000] = {};
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, utf8, 10000, NULL, NULL);
|
2018-11-01 21:32:13 -04:00
|
|
|
text.clear();
|
2018-11-03 23:26:27 -04:00
|
|
|
for (int i = 0; utf8[i];)
|
2018-11-01 21:32:13 -04:00
|
|
|
{
|
|
|
|
wchar_t utf8char[3] = {};
|
2018-11-04 01:15:34 -04:00
|
|
|
swprintf_s<3>(utf8char, L"%02X", (int)(unsigned char)utf8[i++]);
|
2018-11-01 21:32:13 -04:00
|
|
|
text += L"%" + std::wstring(utf8char);
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:36:56 -04:00
|
|
|
std::wstring translation;
|
|
|
|
if (internet)
|
|
|
|
{
|
2018-10-18 22:52:27 -04:00
|
|
|
std::wstring location = translateFrom.empty()
|
|
|
|
? L"/tdetect?text=" + text
|
|
|
|
: L"/ttranslate?from=" + translateFrom + L"&to=" + translateTo + L"&text=" + text;
|
2018-10-17 17:36:56 -04:00
|
|
|
if (HINTERNET connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
{
|
2018-10-18 22:52:27 -04:00
|
|
|
if (HINTERNET request = WinHttpOpenRequest(connection, L"POST", location.c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
2018-10-17 17:36:56 -04:00
|
|
|
{
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
|
|
|
{
|
|
|
|
DWORD bytesRead;
|
|
|
|
char buffer[10000] = {};
|
|
|
|
WinHttpReceiveResponse(request, NULL);
|
|
|
|
WinHttpReadData(request, buffer, 10000, &bytesRead);
|
2018-10-18 22:52:27 -04:00
|
|
|
wchar_t wbuffer[10000] = {};
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
|
|
|
|
if (translateFrom.empty()) translateFrom = wbuffer;
|
|
|
|
// Response formatted as JSON: translation starts with :" and ends with "}
|
|
|
|
if (std::wcmatch results; std::regex_search(wbuffer, results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
|
2018-10-17 17:36:56 -04:00
|
|
|
}
|
|
|
|
WinHttpCloseHandle(request);
|
|
|
|
}
|
|
|
|
WinHttpCloseHandle(connection);
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 22:52:27 -04:00
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
{
|
2018-11-03 23:26:27 -04:00
|
|
|
if (sentenceInfo["hook address"] == -1) return false;
|
2018-10-17 17:36:56 -04:00
|
|
|
|
2018-11-03 23:26:27 -04:00
|
|
|
{
|
|
|
|
static std::mutex m;
|
|
|
|
static std::vector<DWORD> requestTimes;
|
|
|
|
std::lock_guard l(m);
|
|
|
|
requestTimes.push_back(GetTickCount());
|
|
|
|
requestTimes.erase(std::remove_if(requestTimes.begin(), requestTimes.end(), [&](DWORD requestTime) { return GetTickCount() - requestTime > 60 * 1000; }), requestTimes.end());
|
|
|
|
if (!sentenceInfo["current select"] && requestTimes.size() > 30)
|
|
|
|
{
|
2018-12-14 22:26:49 -05:00
|
|
|
sentence += L"\r\nToo many translation requests: refuse to make more";
|
2018-11-03 23:26:27 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring translation, translateFrom;
|
2018-10-18 22:52:27 -04:00
|
|
|
Translate(sentence, translateFrom, translateTo);
|
|
|
|
translation = Translate(sentence, translateFrom, translateTo);
|
|
|
|
for (auto& c : translation) if (c == L'\\') c = 0x200b;
|
|
|
|
if (translation.empty()) translation = L"Error while translating.";
|
2018-10-17 17:36:56 -04:00
|
|
|
sentence += L"\r\n" + translation;
|
|
|
|
return true;
|
|
|
|
}
|