forked from Public-Mirror/Textractor
fix bugs and add new languages to deepl
This commit is contained in:
parent
795ecce45e
commit
76fea31fb3
@ -10,17 +10,30 @@ const char* TRANSLATION_PROVIDER = "DeepL Translate";
|
||||
const char* GET_API_KEY_FROM = "https://www.deepl.com/pro.html";
|
||||
QStringList languages
|
||||
{
|
||||
"Bulgarian: BG",
|
||||
"Chinese: ZH",
|
||||
"Czech: CS",
|
||||
"Danish: DA",
|
||||
"Dutch: NL",
|
||||
"English: EN",
|
||||
"Estonian: ET",
|
||||
"Finnish: FI",
|
||||
"French: FR",
|
||||
"German: DE",
|
||||
"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";
|
||||
|
||||
@ -28,7 +41,9 @@ bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true
|
||||
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 1000;
|
||||
|
||||
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)
|
||||
{
|
||||
@ -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());
|
||||
if (HttpRequest httpRequest{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"api.deepl.com",
|
||||
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"
|
||||
}; httpRequest && (!httpRequest.response.empty() || (httpRequest = HttpRequest{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"api.deepl.com",
|
||||
planLevel == PAID ? L"api.deepl.com" : L"api-free.deepl.com",
|
||||
L"POST",
|
||||
(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,
|
||||
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 "}]
|
||||
if (auto translation = Copy(JSON::Parse(httpRequest.response)[L"translations"][0][L"text"].String())) return { true, translation.value() };
|
||||
|
@ -1,20 +1,40 @@
|
||||
#include "devtools.h"
|
||||
#include <QWebSocket>
|
||||
#include <QMetaEnum>
|
||||
#include <QFileDialog>
|
||||
#include <QMouseEvent>
|
||||
#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
|
||||
{
|
||||
std::function<void(QString)> OnStatusChanged = Swallow;
|
||||
auto statusLabel = new QLabel("Stopped");
|
||||
PROCESS_INFORMATION processInfo = {};
|
||||
std::atomic<int> idCounter = 0;
|
||||
std::mutex devToolsMutex;
|
||||
QWebSocket webSocket;
|
||||
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 _ = ([]
|
||||
{
|
||||
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)
|
||||
{
|
||||
auto result = JSON::Parse(S(message));
|
||||
@ -30,47 +50,89 @@ namespace
|
||||
|
||||
namespace DevTools
|
||||
{
|
||||
void Start(const std::wstring& path, std::function<void(QString)> statusChanged, bool headless)
|
||||
{
|
||||
OnStatusChanged = statusChanged;
|
||||
DWORD exitCode = 0;
|
||||
auto args = FormatString(
|
||||
L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir=%s\\devtoolscache --remote-debugging-port=9222",
|
||||
path,
|
||||
std::filesystem::current_path().wstring()
|
||||
);
|
||||
if (headless) args += L" --headless";
|
||||
STARTUPINFOW DUMMY = { sizeof(DUMMY) };
|
||||
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE) ||
|
||||
CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo)
|
||||
)
|
||||
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 })
|
||||
{
|
||||
if (HttpRequest httpRequest{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"127.0.0.1",
|
||||
L"POST",
|
||||
L"/json/list",
|
||||
"",
|
||||
NULL,
|
||||
9222,
|
||||
NULL,
|
||||
WINHTTP_FLAG_ESCAPE_DISABLE
|
||||
})
|
||||
{
|
||||
if (auto list = Copy(JSON::Parse(httpRequest.response).Array())) if (auto it = std::find_if(
|
||||
list->begin(),
|
||||
list->end(),
|
||||
[](const JSON::Value<wchar_t>& object) { return object[L"type"].String() && *object[L"type"].String() == L"page" && object[L"webSocketDebuggerUrl"].String(); }
|
||||
); it != list->end())
|
||||
{
|
||||
std::scoped_lock lock(devToolsMutex);
|
||||
webSocket.open(S(*(*it)[L"webSocketDebuggerUrl"].String()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
OnStatusChanged("Failed Connection");
|
||||
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);
|
||||
}
|
||||
else OnStatusChanged("Failed Startup");
|
||||
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]
|
||||
{
|
||||
DWORD exitCode = 0;
|
||||
auto args = FormatString(
|
||||
L"%s --proxy-server=direct:// --disable-extensions --disable-gpu --user-data-dir=%s\\devtoolscache --remote-debugging-port=9222",
|
||||
S(chromePathEdit->text()),
|
||||
std::filesystem::current_path().wstring()
|
||||
);
|
||||
if (headlessCheck->isChecked()) args += L" --headless";
|
||||
STARTUPINFOW DUMMY = { sizeof(DUMMY) };
|
||||
if ((GetExitCodeProcess(processInfo.hProcess, &exitCode) && exitCode == STILL_ACTIVE) ||
|
||||
CreateProcessW(NULL, args.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &DUMMY, &processInfo)
|
||||
)
|
||||
{
|
||||
if (HttpRequest httpRequest{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"127.0.0.1",
|
||||
L"POST",
|
||||
L"/json/list",
|
||||
"",
|
||||
NULL,
|
||||
9222,
|
||||
NULL,
|
||||
WINHTTP_FLAG_ESCAPE_DISABLE
|
||||
})
|
||||
{
|
||||
if (auto list = Copy(JSON::Parse(httpRequest.response).Array())) if (auto it = std::find_if(
|
||||
list->begin(),
|
||||
list->end(),
|
||||
[](const JSON::Value<wchar_t>& object) { return object[L"type"].String() && *object[L"type"].String() == L"page" && object[L"webSocketDebuggerUrl"].String(); }
|
||||
); it != list->end())
|
||||
{
|
||||
std::scoped_lock lock(devToolsMutex);
|
||||
webSocket.open(S(*(*it)[L"webSocketDebuggerUrl"].String()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
StatusChanged("Failed Connection");
|
||||
}
|
||||
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()
|
||||
@ -88,7 +150,7 @@ namespace DevTools
|
||||
CloseHandle(processInfo.hThread);
|
||||
}
|
||||
try { std::filesystem::remove_all(L"devtoolscache"); } catch (std::filesystem::filesystem_error) {}
|
||||
OnStatusChanged("Stopped");
|
||||
StatusChanged("Stopped");
|
||||
}
|
||||
|
||||
bool Connected()
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
namespace DevTools
|
||||
{
|
||||
void Start(const std::wstring& path, std::function<void(QString)> statusChanged, bool headless);
|
||||
void Start();
|
||||
void Close();
|
||||
bool Connected();
|
||||
JSON::Value<wchar_t> SendRequest(const char* method, const std::wstring& params = L"{}");
|
||||
|
@ -1,21 +1,10 @@
|
||||
#include "qtcommon.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* TRANSLATION_ERROR;
|
||||
|
||||
extern Synchronized<std::wstring> translateTo, translateFrom;
|
||||
extern QFormLayout* display;
|
||||
extern Settings settings;
|
||||
|
||||
const char* TRANSLATION_PROVIDER = "DevTools DeepL Translate";
|
||||
const char* GET_API_KEY_FROM = nullptr;
|
||||
@ -24,17 +13,30 @@ int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500;
|
||||
|
||||
QStringList languages
|
||||
{
|
||||
"Chinese: zh",
|
||||
"Dutch: nl",
|
||||
"English: en",
|
||||
"French: fr",
|
||||
"German: de",
|
||||
"Italian: it",
|
||||
"Japanese: ja",
|
||||
"Polish: pl",
|
||||
"Portuguese: pt",
|
||||
"Russian: ru",
|
||||
"Spanish: es",
|
||||
"Bulgarian: BG",
|
||||
"Chinese: ZH",
|
||||
"Czech: CS",
|
||||
"Danish: DA",
|
||||
"Dutch: NL",
|
||||
"English: EN",
|
||||
"Estonian: ET",
|
||||
"Finnish: FI",
|
||||
"French: FR",
|
||||
"German: DE",
|
||||
"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";
|
||||
|
||||
@ -44,78 +46,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
|
||||
{
|
||||
case DLL_PROCESS_ATTACH:
|
||||
{
|
||||
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, "/", "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);
|
||||
DevTools::Start();
|
||||
}
|
||||
break;
|
||||
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
|
||||
static std::mutex 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)
|
||||
DevTools::SendRequest("Runtime.evaluate", FormatString(LR"({"expression":"
|
||||
|
Loading…
x
Reference in New Issue
Block a user