-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApplicationMain.cpp
More file actions
116 lines (97 loc) · 3.91 KB
/
Copy pathApplicationMain.cpp
File metadata and controls
116 lines (97 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "ApplicationMain.h"
#include <regex>
#include <string>
ApplicationMain::ApplicationMain(QWidget* parent)
: QMainWindow(parent)
{
// ui.setupUi(this); //根据ui设计文件部署界面 我不用这个
QNetworkProxyFactory::setUseSystemConfiguration(false);
// 设置http代理
// QNetworkProxy net;
// net.setType(QNetworkProxy::HttpProxy);
// net.setHostName("127.0.0.1");
// net.setPort(8099);
// QNetworkProxy::setApplicationProxy(net);
QString strHome = "https://games.dmm.com/zh-CHS/detail/kancolle"; // 主页
// QString strHome = "http://www.baidu.com"; // 主页
// 新建url编辑器 其实没有用
pURLEdit = new QLineEdit(this);
pURLEdit->setText(strHome);
// 把编辑器放进工具栏
QToolBar* pToolBar = addToolBar(tr("&URLEdit"));
pToolBar->addWidget(pURLEdit);
pInspectorAction = new QAction(tr("&Inspector"), this);
connect(pInspectorAction, &QAction::triggered, this, &ApplicationMain::openInspector);
QMenu* pInspector = menuBar()->addMenu(tr("&debug"));
pInspector->addAction(pInspectorAction);
pMainWebView = new QWebEngineView(this); // 实例化MainWebView
pMainWebView->load(QUrl(strHome));
pMainWebView->setMinimumSize(1200, 720);
pMainWebPage = pMainWebView->page();
connect(pMainWebView, &QWebEngineView::urlChanged, this, &ApplicationMain::onUrlChanged);
connect(pMainWebPage, &QWebEnginePage::loadFinished, this, &ApplicationMain::PageLoadFinish);
QWidget* widget = new QWidget;
widget->setMinimumSize(1200, 720);
this->setCentralWidget(widget);
QHBoxLayout* layout = new QHBoxLayout(widget);
layout->addWidget(pMainWebView);
this->setLayout(layout);
this->statusBar();
}
ApplicationMain::~ApplicationMain()
{
}
void ApplicationMain::onUrlChanged()
{
pURLEdit->setText(pMainWebView->url().toString());
}
void ApplicationMain::PageLoadFinish() // 页面加载完毕
{
Sleep(500);
pURLEdit->setText(pMainWebView->url().toString());
// document.getElementById("iframe1").src
std::string nowURL = pMainWebView->url().toString().toStdString();
std::regex pattern1("redirect", std::regex::icase);
std::regex pattern2("netgame", std::regex::icase);
std::regex pattern3("gadgets", std::regex::icase);
std::regex pattern4("kcs2", std::regex::icase);
std::smatch result;
if (regex_search(nowURL, result, pattern1))
{
pMainWebPage->runJavaScript(QString("self.location.href=document.getElementById('game_frame').src;"));
}
if (regex_search(nowURL, result, pattern2))
{
pMainWebPage->runJavaScript(QString("self.location.href=document.getElementById('game_frame').src;"));
}
if (regex_search(nowURL, result, pattern3))
{
Sleep(2500);
pMainWebPage->runJavaScript(QString("self.location.href=document.getElementById('htmlWrap').src;"));
}
if (regex_search(nowURL, result, pattern4))
{
Sleep(500);
pMainWebView->setMaximumSize(1200, 720);
pMainWebPage->runJavaScript(QString("document.getElementsByTagName('body')[0].style = 'margin: 0px; overflow: hidden;'"));
}
qDebug() << pMainWebView->url().toString();
}
void ApplicationMain::openInspector()
{
QDialog* pInspectorDialog = new QDialog(this);
pInspectorDialog->setWindowTitle(tr("Inspector"));
QWebEngineView* pInspectorView = new QWebEngineView(this);
pInspectorDialog->setMinimumSize(1200, 720);
QHBoxLayout* pInspectorDialoglayout = new QHBoxLayout(this);
pInspectorDialoglayout->addWidget(pInspectorView);
pInspectorDialog->setLayout(pInspectorDialoglayout);
pMainWebPage->setDevToolsPage(pInspectorView->page());
pInspectorDialog->show();
}
void ApplicationMain::Sleep(int msec) // 延时不阻塞 毫秒
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}