fix bugs and add new languages to deepl

This commit is contained in:
Akash Mozumdar 2021-06-05 08:25:46 -06:00
parent 795ecce45e
commit 76fea31fb3
4 changed files with 157 additions and 142 deletions

View File

@ -10,17 +10,30 @@ const char* TRANSLATION_PROVIDER = "DeepL Translate";
const char* GET_API_KEY_FROM = "https://www.deepl.com/pro.html"; const char* GET_API_KEY_FROM = "https://www.deepl.com/pro.html";
QStringList languages QStringList languages
{ {
"Bulgarian: BG",
"Chinese: ZH", "Chinese: ZH",
"Czech: CS",
"Danish: DA",
"Dutch: NL", "Dutch: NL",
"English: EN", "English: EN",
"Estonian: ET",
"Finnish: FI",
"French: FR", "French: FR",
"German: DE", "German: DE",
"Greek: EL",
"Hungarian: HU",
"Italian: IT", "Italian: IT",
"Japanese: JA", "Japanese: JA",
"Latvian: LV",
"Lithuanian: LT",
"Polish: PL", "Polish: PL",
"Portuguese: PT", "Portuguese: PT",
"Romanian: RO",
"Russian: RU", "Russian: RU",
"Slovak: SK",
"Slovenian: SL",
"Spanish: ES", "Spanish: ES",
"Swedish: SV"
}; };
std::wstring autoDetectLanguage = L"auto"; std::wstring autoDetectLanguage = L"auto";
@ -28,7 +41,9 @@ bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 1000; int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
enum KeyType { CAT, REST }; enum KeyType { CAT, REST };
int keyType = CAT; int keyType = REST;
enum PlanLevel { FREE, PAID };
int planLevel = PAID;
std::pair<bool, std::wstring> Translate(const std::wstring& text) std::pair<bool, std::wstring> Translate(const std::wstring& text)
{ {
@ -37,18 +52,25 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
std::string translateFromComponent = translateFrom.Copy() == autoDetectLanguage ? "" : "&source_lang=" + WideStringToString(translateFrom.Copy()); std::string translateFromComponent = translateFrom.Copy() == autoDetectLanguage ? "" : "&source_lang=" + WideStringToString(translateFrom.Copy());
if (HttpRequest httpRequest{ if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor", L"Mozilla/5.0 Textractor",
L"api.deepl.com", planLevel == PAID ? L"api.deepl.com" : L"api-free.deepl.com",
L"POST", L"POST",
keyType == CAT ? L"/v1/translate" : L"/v2/translate", keyType == CAT ? L"/v1/translate" : L"/v2/translate",
FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), authKey.Copy(), translateTo.Copy()) + translateFromComponent, FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), authKey.Copy(), translateTo.Copy()) + translateFromComponent,
L"Content-Type: application/x-www-form-urlencoded" L"Content-Type: application/x-www-form-urlencoded"
}; httpRequest && (!httpRequest.response.empty() || (httpRequest = HttpRequest{ }; httpRequest && (!httpRequest.response.empty() || (httpRequest = HttpRequest{
L"Mozilla/5.0 Textractor", L"Mozilla/5.0 Textractor",
L"api.deepl.com", planLevel == PAID ? L"api.deepl.com" : L"api-free.deepl.com",
L"POST", L"POST",
(keyType = !keyType) == CAT ? L"/v1/translate" : L"/v2/translate", (keyType = !keyType) == CAT ? L"/v1/translate" : L"/v2/translate",
FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), authKey.Copy(), translateTo.Copy()) + translateFromComponent, FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), authKey.Copy(), translateTo.Copy()) + translateFromComponent,
L"Content-Type: application/x-www-form-urlencoded" L"Content-Type: application/x-www-form-urlencoded"
})) && (httpRequest.response.find(L"Wrong endpoint. Use") == std::string::npos || (httpRequest = HttpRequest{
L"Mozilla/5.0 Textractor",
(planLevel = !planLevel) == PAID ? L"api.deepl.com" : L"api-free.deepl.com",
L"POST",
keyType == CAT ? L"/v1/translate" : L"/v2/translate",
FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), authKey.Copy(), translateTo.Copy()) + translateFromComponent,
L"Content-Type: application/x-www-form-urlencoded"
}))) })))
// Response formatted as JSON: translation starts with text":" and ends with "}] // Response formatted as JSON: translation starts with text":" and ends with "}]
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"translations"][0][L"text"].String())) return { true, translation.value() }; if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"translations"][0][L"text"].String())) return { true, translation.value() };

View File

@ -1,20 +1,40 @@
#include "devtools.h" #include "devtools.h"
#include <QWebSocket> #include <QWebSocket>
#include <QMetaEnum> #include <QMetaEnum>
#include <QFileDialog>
#include <QMouseEvent>
#include <ppltasks.h> #include <ppltasks.h>
#include <ShlObj.h>
extern const char* CHROME_LOCATION;
extern const char* START_DEVTOOLS;
extern const char* STOP_DEVTOOLS;
extern const char* HEADLESS_MODE;
extern const char* DEVTOOLS_STATUS;
extern const char* AUTO_START;
extern const char* TRANSLATION_PROVIDER;
extern QFormLayout* display;
extern Settings settings;
namespace namespace
{ {
std::function<void(QString)> OnStatusChanged = Swallow; auto statusLabel = new QLabel("Stopped");
PROCESS_INFORMATION processInfo = {}; PROCESS_INFORMATION processInfo = {};
std::atomic<int> idCounter = 0; std::atomic<int> idCounter = 0;
std::mutex devToolsMutex; std::mutex devToolsMutex;
QWebSocket webSocket; QWebSocket webSocket;
std::unordered_map<int, concurrency::task_completion_event<JSON::Value<wchar_t>>> mapQueue; std::unordered_map<int, concurrency::task_completion_event<JSON::Value<wchar_t>>> mapQueue;
void StatusChanged(QString status)
{
QMetaObject::invokeMethod(statusLabel, std::bind(&QLabel::setText, statusLabel, status));
}
auto _ = ([] auto _ = ([]
{ {
QObject::connect(&webSocket, &QWebSocket::stateChanged, QObject::connect(&webSocket, &QWebSocket::stateChanged,
[](QAbstractSocket::SocketState state) { OnStatusChanged(QMetaEnum::fromType<QAbstractSocket::SocketState>().valueToKey(state)); }); [](QAbstractSocket::SocketState state) { StatusChanged(QMetaEnum::fromType<QAbstractSocket::SocketState>().valueToKey(state)); });
QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](QString message) QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](QString message)
{ {
auto result = JSON::Parse(S(message)); auto result = JSON::Parse(S(message));
@ -30,16 +50,44 @@ namespace
namespace DevTools namespace DevTools
{ {
void Start(const std::wstring& path, std::function<void(QString)> statusChanged, bool headless) void Start()
{
QString chromePath = settings.value(CHROME_LOCATION).toString();
wchar_t programFiles[MAX_PATH + 100] = {};
if (chromePath.isEmpty()) for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
{
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles);
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe");
if (std::filesystem::exists(programFiles)) chromePath = S(programFiles);
}
auto chromePathEdit = new QLineEdit(chromePath);
static struct : QObject
{
bool eventFilter(QObject* object, QEvent* event)
{
if (auto mouseEvent = dynamic_cast<QMouseEvent*>(event))
if (mouseEvent->button() == Qt::LeftButton)
if (QString chromePath = QFileDialog::getOpenFileName(nullptr, TRANSLATION_PROVIDER, "/", "Google Chrome (*.exe)"); !chromePath.isEmpty())
((QLineEdit*)object)->setText(chromePath);
return false;
}
} chromeSelector;
chromePathEdit->installEventFilter(&chromeSelector);
QObject::connect(chromePathEdit, &QLineEdit::textChanged, [chromePathEdit](QString path) { settings.setValue(CHROME_LOCATION, path); });
display->addRow(CHROME_LOCATION, chromePathEdit);
auto headlessCheck = new QCheckBox();
auto startButton = new QPushButton(START_DEVTOOLS), stopButton = new QPushButton(STOP_DEVTOOLS);
headlessCheck->setChecked(settings.value(HEADLESS_MODE, true).toBool());
QObject::connect(headlessCheck, &QCheckBox::clicked, [](bool headless) { settings.setValue(HEADLESS_MODE, headless); });
QObject::connect(startButton, &QPushButton::clicked, [chromePathEdit, headlessCheck]
{ {
OnStatusChanged = statusChanged;
DWORD exitCode = 0; DWORD exitCode = 0;
auto args = FormatString( auto args = FormatString(
L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir=%s\\devtoolscache --remote-debugging-port=9222", L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir=%s\\devtoolscache --remote-debugging-port=9222",
path, S(chromePathEdit->text()),
std::filesystem::current_path().wstring() std::filesystem::current_path().wstring()
); );
if (headless) args += L" --headless"; if (headlessCheck->isChecked()) args += L" --headless";
STARTUPINFOW DUMMY = { sizeof(DUMMY) }; STARTUPINFOW DUMMY = { sizeof(DUMMY) };
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE) || if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE) ||
CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo) CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo)
@ -68,9 +116,23 @@ namespace DevTools
return; return;
} }
} }
OnStatusChanged("Failed Connection"); StatusChanged("Failed Connection");
} }
else OnStatusChanged("Failed Startup"); else StatusChanged("Failed Startup");
});
QObject::connect(stopButton, &QPushButton::clicked, &Close);
auto buttons = new QHBoxLayout();
buttons->addWidget(startButton);
buttons->addWidget(stopButton);
display->addRow(HEADLESS_MODE, headlessCheck);
auto autoStartCheck = new QCheckBox();
autoStartCheck->setChecked(settings.value(AUTO_START, false).toBool());
QObject::connect(autoStartCheck, &QCheckBox::clicked, [](bool autoStart) { settings.setValue(AUTO_START, autoStart); });
display->addRow(AUTO_START, autoStartCheck);
display->addRow(buttons);
statusLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
display->addRow(DEVTOOLS_STATUS, statusLabel);
if (autoStartCheck->isChecked()) QMetaObject::invokeMethod(startButton, &QPushButton::click, Qt::QueuedConnection);
} }
void Close() void Close()
@ -88,7 +150,7 @@ namespace DevTools
CloseHandle(processInfo.hThread); CloseHandle(processInfo.hThread);
} }
try { std::filesystem::remove_all(L"devtoolscache"); } catch (std::filesystem::filesystem_error) {} try { std::filesystem::remove_all(L"devtoolscache"); } catch (std::filesystem::filesystem_error) {}
OnStatusChanged("Stopped"); StatusChanged("Stopped");
} }
bool Connected() bool Connected()

View File

@ -3,7 +3,7 @@
namespace DevTools namespace DevTools
{ {
void Start(const std::wstring& path, std::function<void(QString)> statusChanged, bool headless); void Start();
void Close(); void Close();
bool Connected(); bool Connected();
JSON::Value<wchar_t> SendRequest(const char* method, const std::wstring& params = L"{}"); JSON::Value<wchar_t> SendRequest(const char* method, const std::wstring& params = L"{}");

View File

@ -1,21 +1,10 @@
#include "qtcommon.h" #include "qtcommon.h"
#include "devtools.h" #include "devtools.h"
#include <QFileDialog>
#include <QMouseEvent>
#include <ShlObj.h>
extern const wchar_t* TRANSLATION_ERROR;
extern const char* CHROME_LOCATION;
extern const char* START_DEVTOOLS;
extern const char* STOP_DEVTOOLS;
extern const char* HEADLESS_MODE;
extern const char* DEVTOOLS_STATUS;
extern const char* AUTO_START;
extern const wchar_t* ERROR_START_CHROME; extern const wchar_t* ERROR_START_CHROME;
extern const wchar_t* TRANSLATION_ERROR;
extern Synchronized<std::wstring> translateTo, translateFrom; extern Synchronized<std::wstring> translateTo, translateFrom;
extern QFormLayout* display;
extern Settings settings;
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate"; const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
const char* GET_API_KEY_FROM = nullptr; const char* GET_API_KEY_FROM = nullptr;
@ -24,17 +13,30 @@ int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
QStringList languages QStringList languages
{ {
"Chinese: zh", "Bulgarian: BG",
"Dutch: nl", "Chinese: ZH",
"English: en", "Czech: CS",
"French: fr", "Danish: DA",
"German: de", "Dutch: NL",
"Italian: it", "English: EN",
"Japanese: ja", "Estonian: ET",
"Polish: pl", "Finnish: FI",
"Portuguese: pt", "French: FR",
"Russian: ru", "German: DE",
"Spanish: es", "Greek: EL",
"Hungarian: HU",
"Italian: IT",
"Japanese: JA",
"Latvian: LV",
"Lithuanian: LT",
"Polish: PL",
"Portuguese: PT",
"Romanian: RO",
"Russian: RU",
"Slovak: SK",
"Slovenian: SL",
"Spanish: ES",
"Swedish: SV"
}; };
std::wstring autoDetectLanguage = L"auto"; std::wstring autoDetectLanguage = L"auto";
@ -44,78 +46,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
{ {
QString chromePath = settings.value(CHROME_LOCATION).toString(); DevTools::Start();
wchar_t programFiles[MAX_PATH + 100] = {};
if (chromePath.isEmpty()) for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
{
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles);
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe");
if (std::filesystem::exists(programFiles)) chromePath = S(programFiles);
}
auto chromePathEdit = new QLineEdit(chromePath);
static struct : QObject
{
bool eventFilter(QObject* object, QEvent* event)
{
if (auto mouseEvent = dynamic_cast<QMouseEvent*>(event))
if (mouseEvent->button() == Qt::LeftButton)
if (QString chromePath = QFileDialog::getOpenFileName(nullptr, TRANSLATION_PROVIDER, "/", "Chrome (*.exe)"); !chromePath.isEmpty())
((QLineEdit*)object)->setText(chromePath);
return false;
}
} chromeSelector;
chromePathEdit->installEventFilter(&chromeSelector);
QObject::connect(chromePathEdit, &QLineEdit::textChanged, [chromePathEdit](QString path) { settings.setValue(CHROME_LOCATION, path); });
display->addRow(CHROME_LOCATION, chromePathEdit);
auto statusLabel = new QLabel("Stopped");
auto startButton = new QPushButton(START_DEVTOOLS), stopButton = new QPushButton(STOP_DEVTOOLS);
auto headlessCheck = new QCheckBox();
headlessCheck->setChecked(settings.value(HEADLESS_MODE, true).toBool());
QObject::connect(headlessCheck, &QCheckBox::clicked, [](bool headless) { settings.setValue(HEADLESS_MODE, headless); });
QObject::connect(startButton, &QPushButton::clicked, [statusLabel, chromePathEdit, headlessCheck]
{
DevTools::Start(
S(chromePathEdit->text()),
[statusLabel](QString status)
{
QMetaObject::invokeMethod(statusLabel, std::bind(&QLabel::setText, statusLabel, status));
if (status == "ConnectedState") std::thread([]
{
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"127.0.0.1",
L"POST",
L"/json/version",
"",
NULL,
9222,
NULL,
WINHTTP_FLAG_ESCAPE_DISABLE
})
if (auto userAgent = Copy(JSON::Parse(httpRequest.response)[L"User-Agent"].String()))
if (userAgent->find(L"Headless") != std::string::npos)
DevTools::SendRequest(
"Network.setUserAgentOverride",
FormatString(LR"({"userAgent":"%s"})", userAgent->replace(userAgent->find(L"Headless"), 8, L""))
);
}).detach();
},
headlessCheck->isChecked()
);
});
QObject::connect(stopButton, &QPushButton::clicked, &DevTools::Close);
auto buttons = new QHBoxLayout();
buttons->addWidget(startButton);
buttons->addWidget(stopButton);
display->addRow(HEADLESS_MODE, headlessCheck);
auto autoStartCheck = new QCheckBox();
autoStartCheck->setChecked(settings.value(AUTO_START, false).toBool());
QObject::connect(autoStartCheck, &QCheckBox::clicked, [](bool autoStart) { settings.setValue(AUTO_START, autoStart); });
display->addRow(AUTO_START, autoStartCheck);
display->addRow(buttons);
statusLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
display->addRow(DEVTOOLS_STATUS, statusLabel);
if (autoStartCheck->isChecked()) QMetaObject::invokeMethod(startButton, &QPushButton::click, Qt::QueuedConnection);
} }
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
@ -133,7 +64,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
// DevTools can't handle concurrent translations yet // DevTools can't handle concurrent translations yet
static std::mutex translationMutex; static std::mutex translationMutex;
std::scoped_lock lock(translationMutex); std::scoped_lock lock(translationMutex);
DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#any/%s/%s"})", translateTo.Copy(), Escape(text))); DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://www.deepl.com/en/translator#%s/%s/%s"})", translateTo.Copy(), translateTo.Copy(), Escape(text)));
if (translateFrom.Copy() != autoDetectLanguage) if (translateFrom.Copy() != autoDetectLanguage)
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":" DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"