-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser.cpp
More file actions
119 lines (100 loc) · 3.39 KB
/
browser.cpp
File metadata and controls
119 lines (100 loc) · 3.39 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
117
118
119
#include <QDebug>
#include <QFile>
#include <QMargins>
#include <QPageLayout>
#include <QPageSize>
#include <QSize>
#include <QTimer>
#include <QWebEngineSettings>
#include "browser.h"
#include "interceptor.h"
Browser::Browser(const Options& options): QObject (), _options(options)
{
RequestInterceptor *interceptor = new RequestInterceptor(_options);
_profile = new QWebEngineProfile();
_profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
_profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
_profile->setPersistentStoragePath(nullptr);
_profile->setUrlRequestInterceptor(interceptor);
if (_options.userAgent.length() > 0) {
_profile->setHttpUserAgent(_options.userAgent);
}
_page = new QWebEnginePage(_profile);
_page->settings()->setAttribute(QWebEngineSettings::ShowScrollBars, false);
_page->settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, true);
_view = new QWebEngineView;
_view->setPage(_page);
_view->resize(QSize(_options.width, _options.height));
connect(_view, &QWebEngineView::loadFinished, this, &Browser::loadFinished);
//qDebug() << "StoragePath:" << _profile->persistentStoragePath();
//qDebug() << "isOffTheRecord:" << _profile->isOffTheRecord();
}
Browser::~Browser()
{
delete _profile;
delete _page;
delete _view;
}
void Browser::run()
{
qDebug() << "Load website";
_view->load(_options.url);
// TODO: Investigate whether showing GUI can be avoided?
qDebug() << "Show browser";
_view->show();
QTimer::singleShot(_options.timeout * 1000, this, SLOT(timeout()));
}
void Browser::loadFinished()
{
qDebug() << "Load finished";
if (_options.delay == 0) {
screenshot();
} else {
QTimer::singleShot(_options.delay * 1000, this, SLOT(delayedShot()));
}
}
void Browser::delayedShot()
{
qDebug() << "Delayed shot";
screenshot();
}
void Browser::pdfPrintingFinished()
{
qDebug() << "PDF printing finished";
emit screenshotFinished();
}
void Browser::timeout()
{
qDebug() << "Timeout";
screenshot();
}
void Browser::screenshot()
{
qDebug() << "Save screenshot to" << _options.out;
if (_options.out.endsWith(".pdf", Qt::CaseInsensitive)) {
QPageSize pageSize(QPageSize::A4);
if (_options.pdfPaperSize == "a3") {
pageSize = QPageSize(QPageSize::A3);
} else if (_options.pdfPaperSize == "a4") {
pageSize = QPageSize(QPageSize::A4);
} else if (_options.pdfPaperSize == "letter") {
pageSize = QPageSize(QPageSize::Letter);
} else if (_options.pdfPaperSize == "tabloid") {
pageSize = QPageSize(QPageSize::Tabloid);
}
QPageLayout layout(pageSize,
_options.pdfOrientation == "landscape"
? QPageLayout::Landscape : QPageLayout::Portrait,
QMarginsF());
connect(_view->page(), &QWebEnginePage::pdfPrintingFinished, this, &Browser::pdfPrintingFinished);
_view->page()->printToPdf(_options.out, layout);
} else {
QSize size(_options.width, _options.height);
QImage *image = new QImage(size, QImage::Format_RGB32);
QPainter *painter = new QPainter(image);
_view->render(painter);
painter->end();
image->save(_options.out);
emit screenshotFinished();
}
}