diff --git a/deploy.ps1 b/deploy.ps1 index a0def0e..d1b1f91 100644 --- a/deploy.ps1 +++ b/deploy.ps1 @@ -45,6 +45,7 @@ foreach ($language in @{ "Copy to Clipboard", "DeepL Translate", "DevTools DeepL Translate", + "DevTools Papago Translate", "DevTools Systran Translate", "Extra Newlines", "Extra Window", diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index fc346dc..54a9c75 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(Bing\ Translate MODULE bingtranslate.cpp translatewrapper.cpp networ add_library(Copy\ to\ Clipboard MODULE copyclipboard.cpp extensionimpl.cpp) add_library(DeepL\ Translate MODULE deepltranslate.cpp translatewrapper.cpp network.cpp extensionimpl.cpp) add_library(DevTools\ DeepL\ Translate MODULE devtoolsdeepltranslate.cpp devtools.cpp translatewrapper.cpp network.cpp extensionimpl.cpp) +add_library(DevTools\ Papago\ Translate MODULE devtoolspapagotranslate.cpp devtools.cpp translatewrapper.cpp network.cpp extensionimpl.cpp) add_library(DevTools\ Systran\ Translate MODULE devtoolssystrantranslate.cpp devtools.cpp translatewrapper.cpp network.cpp extensionimpl.cpp) add_library(Extra\ Newlines MODULE extranewlines.cpp extensionimpl.cpp) add_library(Extra\ Window MODULE extrawindow.cpp extensionimpl.cpp) @@ -25,6 +26,7 @@ target_precompile_headers(Bing\ Translate REUSE_FROM pch) target_precompile_headers(Copy\ to\ Clipboard REUSE_FROM pch) target_precompile_headers(DeepL\ Translate REUSE_FROM pch) target_precompile_headers(DevTools\ DeepL\ Translate REUSE_FROM pch) +target_precompile_headers(DevTools\ Papago\ Translate REUSE_FROM pch) target_precompile_headers(DevTools\ Systran\ Translate REUSE_FROM pch) target_precompile_headers(Extra\ Newlines REUSE_FROM pch) target_precompile_headers(Extra\ Window REUSE_FROM pch) @@ -42,6 +44,7 @@ target_precompile_headers(Thread\ Linker REUSE_FROM pch) target_link_libraries(Bing\ Translate winhttp Qt5::Widgets) target_link_libraries(DeepL\ Translate winhttp Qt5::Widgets) target_link_libraries(DevTools\ DeepL\ Translate shell32 winhttp Qt5::Widgets Qt5::WebSockets) +target_link_libraries(DevTools\ Papago\ Translate shell32 winhttp Qt5::Widgets Qt5::WebSockets) target_link_libraries(DevTools\ Systran\ Translate shell32 winhttp Qt5::Widgets Qt5::WebSockets) target_link_libraries(Extra\ Window Qt5::Widgets) target_link_libraries(Google\ Translate winhttp Qt5::Widgets) diff --git a/extensions/devtoolspapagotranslate.cpp b/extensions/devtoolspapagotranslate.cpp new file mode 100644 index 0000000..fcb43e2 --- /dev/null +++ b/extensions/devtoolspapagotranslate.cpp @@ -0,0 +1,64 @@ +#include "qtcommon.h" +#include "devtools.h" + +extern const wchar_t* ERROR_START_CHROME; +extern const wchar_t* TRANSLATION_ERROR; + +extern Synchronized translateTo, translateFrom; + +const char* TRANSLATION_PROVIDER = "DevTools Papago Translate"; +const char* GET_API_KEY_FROM = nullptr; +bool translateSelectedOnly = true, rateLimitAll = false, rateLimitSelected = false, useCache = true, useFilter = true; +int tokenCount = 30, tokenRestoreDelay = 60000, maxSentenceSize = 2500; + +QStringList languages +{ + "Chinese (simplified): zh-CN", + "Chinese (traditional): zt-TW", + "English: en", + "French: fr", + "German: de", + "Hindi: hi", + "Indonesian: id", + "Italian: it", + "Japanese: ja", + "Korean: ko", + "Portuguese: pt", + "Russian: ru", + "Spanish: es", + "Thai: th", + "Vietnamese: vi", +}; +std::wstring autoDetectLanguage = L"auto"; + +BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + { + DevTools::Start(); + } + break; + case DLL_PROCESS_DETACH: + { + DevTools::Close(); + } + break; + } + return TRUE; +} + +std::pair Translate(const std::wstring& text) +{ + if (!DevTools::Connected()) return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, ERROR_START_CHROME) }; + // DevTools can't handle concurrent translations yet + static std::mutex translationMutex; + std::scoped_lock lock(translationMutex); + DevTools::SendRequest("Page.navigate", FormatString(LR"({"url":"https://papago.naver.com/?sk=%s&tk=%s&st=%s"})", translateFrom.Copy(), translateTo.Copy(), Escape(text))); + for (int retry = 0; ++retry < 100; Sleep(100)) + if (auto translation = Copy(DevTools::SendRequest("Runtime.evaluate", + LR"({"expression":"document.querySelector('#txtTarget').textContent.trim() ","returnByValue":true})" + )[L"result"][L"value"].String())) if (!translation->empty()) return { true, translation.value() }; + return { false, TRANSLATION_ERROR }; +}