fix google tanslation

This commit is contained in:
Akash Mozumdar 2018-08-29 22:47:00 -04:00
parent 02c163f930
commit 6a25a41d79

View File

@ -1,6 +1,7 @@
#include "extensions.h" #include "extensions.h"
#include <winhttp.h> #include <winhttp.h>
#include <ctime> #include <ctime>
#include <regex>
std::wstring GetTranslationUri(const wchar_t* text, unsigned int TKK) std::wstring GetTranslationUri(const wchar_t* text, unsigned int TKK)
{ {
@ -47,13 +48,14 @@ extern "C"
static unsigned int TKK = 0; static unsigned int TKK = 0;
wchar_t error[] = L"Error while translating."; wchar_t error[] = L"Error while translating.";
wchar_t translation[10000] = {}; std::wstring translation(L"");
wchar_t* message = error; const wchar_t* message = error;
if (wcslen(sentence) > 2000 || GetProperty("hook address", miscInfo) == -1) return sentence; if (wcslen(sentence) > 2000 || GetProperty("hook address", miscInfo) == -1) return sentence;
if (internet) if (internet)
{ {
if (!TKK)
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{ {
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE)) if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
@ -70,6 +72,7 @@ extern "C"
} }
WinHttpCloseHandle(connection); WinHttpCloseHandle(connection);
} }
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{ {
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE)) if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
@ -83,9 +86,18 @@ extern "C"
// Response formatted as JSON: starts with '[[["' // Response formatted as JSON: starts with '[[["'
if (buffer[0] == '[') if (buffer[0] == '[')
{ {
MultiByteToWideChar(CP_UTF8, 0, buffer + 4, (int)((strstr(buffer, "\",\"")) - (buffer + 4)), translation, 10000); wchar_t wbuffer[10000] = {};
message = translation; MultiByteToWideChar(CP_UTF8, 0, (char*)buffer, -1, wbuffer, 10000);
for (int i = -1; translation[++i];) if (translation[i] == L'\\') translation[i] = 0x200b; std::wstring response(wbuffer);
std::wregex translationFinder(L"\\[\"(.*?)\",[n\"]");
std::wsmatch results;
while (std::regex_search(response, results, translationFinder))
{
translation += std::wstring(results[1]) + L" ";
response = results.suffix().str();
}
for (auto& c : translation) if (c == L'\\') c = 0x200b;
message = translation.c_str();
} }
} }
WinHttpCloseHandle(request); WinHttpCloseHandle(request);