mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 08:54:12 +08:00
add user-agent switch method
Add method that changes user-agent when in the headless mode
This commit is contained in:
parent
73d1f21bc1
commit
2ab780a491
@ -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;
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include <fstream>
|
||||
#include <QtCore>
|
||||
#include <QtWebSockets/QWebSocket>
|
||||
#include <ppltasks.h>
|
||||
@ -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;
|
||||
};
|
@ -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<bool, std::wstring> Translate(const std::wstring& text, DevTools* devtools)
|
||||
{
|
||||
@ -65,6 +64,7 @@ std::pair<bool, std::wstring> 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<bool, std::wstring> 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" } });
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user