From 2ab780a4917e486bc42a017b91809e86c61c4640 Mon Sep 17 00:00:00 2001 From: zeheyler Date: Sun, 18 Oct 2020 16:16:06 +0300 Subject: [PATCH] add user-agent switch method Add method that changes user-agent when in the headless mode --- extensions/devtools.cpp | 55 +++++++++++++++------------ extensions/devtools.h | 5 ++- extensions/devtoolsdeepltranslate.cpp | 20 +++++++++- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/extensions/devtools.cpp b/extensions/devtools.cpp index 0330e29..bf627e9 100644 --- a/extensions/devtools.cpp +++ b/extensions/devtools.cpp @@ -13,20 +13,31 @@ void DevTools::startDevTools(QString path, bool headless, int port) { if (startChrome(path, headless, port)) { + QJsonDocument doc; QString webSocketDebuggerUrl; - if (GetwebSocketDebuggerUrl(webSocketDebuggerUrl, port)) + if (GetJsonfromHTTP(doc, "/json/list", port)) { - connect(&webSocket, &QWebSocket::stateChanged, this, &DevTools::stateChanged); - connect(&webSocket, &QWebSocket::textMessageReceived, this, &DevTools::onTextMessageReceived); - webSocket.open(webSocketDebuggerUrl); - session += 1; + for (const auto obj : doc.array()) + if (obj.toObject().value("type") == "page") + { + webSocketDebuggerUrl.append(obj.toObject().value("webSocketDebuggerUrl").toString()); + break; + } + if (!webSocketDebuggerUrl.isEmpty()) + { + if (GetJsonfromHTTP(doc, "/json/version", port)) + { + useragent = doc.object().value("User-Agent").toString(); + } + connect(&webSocket, &QWebSocket::stateChanged, this, &DevTools::stateChanged); + connect(&webSocket, &QWebSocket::textMessageReceived, this, &DevTools::onTextMessageReceived); + webSocket.open(webSocketDebuggerUrl); + session += 1; + return; + } } - else - { - status = "Failed to find chrome debug port!"; - emit statusChanged(status); - } - + status = "Failed to find chrome debug port!"; + emit statusChanged(status); } else { @@ -45,6 +56,11 @@ QString DevTools::getStatus() return status; } +QString DevTools::getUserAgent() +{ + return useragent; +} + DevTools::~DevTools() { closeDevTools(); @@ -71,14 +87,13 @@ bool DevTools::startChrome(QString path, bool headless, int port) return true; } -bool DevTools::GetwebSocketDebuggerUrl(QString& url, int port) +bool DevTools::GetJsonfromHTTP(QJsonDocument& doc, QString object, int port) { - url.clear(); if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"127.0.0.1", L"POST", - FormatString(L"/json/list").c_str(), + object.toStdWString().c_str(), "", NULL, NULL, @@ -89,16 +104,8 @@ bool DevTools::GetwebSocketDebuggerUrl(QString& url, int port) }) { QString qtString = QString::fromStdWString(httpRequest.response); - QJsonDocument doc = QJsonDocument::fromJson(qtString.toUtf8()); - QJsonArray rootObject = doc.array(); - - for (const auto obj : rootObject) - if (obj.toObject().value("type") == "page") - { - url.append(obj.toObject().value("webSocketDebuggerUrl").toString()); - break; - } - if (!url.isEmpty()) + doc = QJsonDocument::fromJson(qtString.toUtf8()); + if (!doc.isEmpty()) return true; else return false; diff --git a/extensions/devtools.h b/extensions/devtools.h index 546eb9e..82641d1 100644 --- a/extensions/devtools.h +++ b/extensions/devtools.h @@ -1,4 +1,3 @@ -#include #include #include #include @@ -30,11 +29,12 @@ public: bool SendRequest(QString method, QJsonObject params, QJsonObject& root); long methodToReceive(QString method, QJsonObject params = {}); QString getStatus(); + QString getUserAgent(); private: bool isConnected(); bool startChrome(QString path, bool headless = false, int port = 9222); - bool GetwebSocketDebuggerUrl(QString& url, int port = 9222); + bool GetJsonfromHTTP(QJsonDocument& doc, QString object, int port = 9222); long idIncrement(); long idmIncrement(); bool compareJson(QJsonValue storedparams, QJsonValue params); @@ -47,4 +47,5 @@ private: long idmethod; PROCESS_INFORMATION processInfo; QString status; + QString useragent; }; \ No newline at end of file diff --git a/extensions/devtoolsdeepltranslate.cpp b/extensions/devtoolsdeepltranslate.cpp index 7ad0956..e10526c 100644 --- a/extensions/devtoolsdeepltranslate.cpp +++ b/extensions/devtoolsdeepltranslate.cpp @@ -1,6 +1,5 @@ #include "qtcommon.h" #include "extension.h" -#include "network.h" #include "devtools.h" extern const wchar_t* TRANSLATION_ERROR; @@ -33,7 +32,7 @@ QStringList languages "Spanish: es", }; -int docfound = -1, targetNodeId = -1, session = -1, pageenabled = -1; +int docfound = -1, targetNodeId = -1, session = -1, pageenabled = -1, useragentflag = -1; std::pair Translate(const std::wstring& text, DevTools* devtools) { @@ -65,6 +64,7 @@ std::pair Translate(const std::wstring& text, DevTools* devt docfound = -1; targetNodeId = -1; pageenabled = -1; + useragentflag = -1; } // Add spaces near ellipsis for better translation and check for quotes @@ -89,6 +89,22 @@ std::pair Translate(const std::wstring& text, DevTools* devt } pageenabled = 1; } + + // Change user-agent if in headless mode + if (useragentflag == -1) + { + QString useragent = devtools->getUserAgent(); + useragent.replace(QRegularExpression("HeadlessChrome"), "Chrome"); + if (!useragent.isEmpty()) + { + if (!devtools->SendRequest("Network.setUserAgentOverride", { {"userAgent", useragent} }, root)) + { + return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; + } + } + useragentflag = 1; + } + long navigate = devtools->methodToReceive("Page.navigatedWithinDocument"); long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });