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))
|
if (startChrome(path, headless, port))
|
||||||
{
|
{
|
||||||
|
QJsonDocument doc;
|
||||||
QString webSocketDebuggerUrl;
|
QString webSocketDebuggerUrl;
|
||||||
if (GetwebSocketDebuggerUrl(webSocketDebuggerUrl, port))
|
if (GetJsonfromHTTP(doc, "/json/list", port))
|
||||||
{
|
{
|
||||||
connect(&webSocket, &QWebSocket::stateChanged, this, &DevTools::stateChanged);
|
for (const auto obj : doc.array())
|
||||||
connect(&webSocket, &QWebSocket::textMessageReceived, this, &DevTools::onTextMessageReceived);
|
if (obj.toObject().value("type") == "page")
|
||||||
webSocket.open(webSocketDebuggerUrl);
|
{
|
||||||
session += 1;
|
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
|
else
|
||||||
{
|
{
|
||||||
@ -45,6 +56,11 @@ QString DevTools::getStatus()
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DevTools::getUserAgent()
|
||||||
|
{
|
||||||
|
return useragent;
|
||||||
|
}
|
||||||
|
|
||||||
DevTools::~DevTools()
|
DevTools::~DevTools()
|
||||||
{
|
{
|
||||||
closeDevTools();
|
closeDevTools();
|
||||||
@ -71,14 +87,13 @@ bool DevTools::startChrome(QString path, bool headless, int port)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DevTools::GetwebSocketDebuggerUrl(QString& url, int port)
|
bool DevTools::GetJsonfromHTTP(QJsonDocument& doc, QString object, int port)
|
||||||
{
|
{
|
||||||
url.clear();
|
|
||||||
if (HttpRequest httpRequest{
|
if (HttpRequest httpRequest{
|
||||||
L"Mozilla/5.0 Textractor",
|
L"Mozilla/5.0 Textractor",
|
||||||
L"127.0.0.1",
|
L"127.0.0.1",
|
||||||
L"POST",
|
L"POST",
|
||||||
FormatString(L"/json/list").c_str(),
|
object.toStdWString().c_str(),
|
||||||
"",
|
"",
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
@ -89,16 +104,8 @@ bool DevTools::GetwebSocketDebuggerUrl(QString& url, int port)
|
|||||||
})
|
})
|
||||||
{
|
{
|
||||||
QString qtString = QString::fromStdWString(httpRequest.response);
|
QString qtString = QString::fromStdWString(httpRequest.response);
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(qtString.toUtf8());
|
doc = QJsonDocument::fromJson(qtString.toUtf8());
|
||||||
QJsonArray rootObject = doc.array();
|
if (!doc.isEmpty())
|
||||||
|
|
||||||
for (const auto obj : rootObject)
|
|
||||||
if (obj.toObject().value("type") == "page")
|
|
||||||
{
|
|
||||||
url.append(obj.toObject().value("webSocketDebuggerUrl").toString());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!url.isEmpty())
|
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <QtCore>
|
#include <QtCore>
|
||||||
#include <QtWebSockets/QWebSocket>
|
#include <QtWebSockets/QWebSocket>
|
||||||
#include <ppltasks.h>
|
#include <ppltasks.h>
|
||||||
@ -30,11 +29,12 @@ public:
|
|||||||
bool SendRequest(QString method, QJsonObject params, QJsonObject& root);
|
bool SendRequest(QString method, QJsonObject params, QJsonObject& root);
|
||||||
long methodToReceive(QString method, QJsonObject params = {});
|
long methodToReceive(QString method, QJsonObject params = {});
|
||||||
QString getStatus();
|
QString getStatus();
|
||||||
|
QString getUserAgent();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
bool startChrome(QString path, bool headless = false, int port = 9222);
|
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 idIncrement();
|
||||||
long idmIncrement();
|
long idmIncrement();
|
||||||
bool compareJson(QJsonValue storedparams, QJsonValue params);
|
bool compareJson(QJsonValue storedparams, QJsonValue params);
|
||||||
@ -47,4 +47,5 @@ private:
|
|||||||
long idmethod;
|
long idmethod;
|
||||||
PROCESS_INFORMATION processInfo;
|
PROCESS_INFORMATION processInfo;
|
||||||
QString status;
|
QString status;
|
||||||
|
QString useragent;
|
||||||
};
|
};
|
@ -1,6 +1,5 @@
|
|||||||
#include "qtcommon.h"
|
#include "qtcommon.h"
|
||||||
#include "extension.h"
|
#include "extension.h"
|
||||||
#include "network.h"
|
|
||||||
#include "devtools.h"
|
#include "devtools.h"
|
||||||
|
|
||||||
extern const wchar_t* TRANSLATION_ERROR;
|
extern const wchar_t* TRANSLATION_ERROR;
|
||||||
@ -33,7 +32,7 @@ QStringList languages
|
|||||||
"Spanish: es",
|
"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)
|
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;
|
docfound = -1;
|
||||||
targetNodeId = -1;
|
targetNodeId = -1;
|
||||||
pageenabled = -1;
|
pageenabled = -1;
|
||||||
|
useragentflag = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add spaces near ellipsis for better translation and check for quotes
|
// 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;
|
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 navigate = devtools->methodToReceive("Page.navigatedWithinDocument");
|
||||||
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
|
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user