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
|
#include <QMainWindow>
#include <QWebEngineUrlRequestInterceptor>
#include <QWebEngineView>
#include <QtCore>
#include <qwebenginepage.h>
#include <qwebengineprofile.h>
#include "schemes/NullRpcSchemeHandler.hpp"
#include "widgets/WebView.hpp"
WebView::WebView(uint32_t webview_id, QWebEngineProfile *profile, QWidget *parent_node)
: QWebEngineView(profile, parent_node), id(webview_id) {}
void WebView::open_devtools() {
if (devtools_window != nullptr)
return;
devtools_window = new DevtoolsWindow(page()->profile());
devtools_window->show();
connect(devtools_window, &DevtoolsWindow::closed, this, [this]() {
devtools_window->deleteLater();
devtools_window = nullptr;
});
connect(this, &WebView::destroyed, this, [this]() {
devtools_window->deleteLater();
devtools_window = nullptr;
});
page()->setDevToolsPage(devtools_window->page());
}
void WebView::scroll_increment(int deltax, int deltay) {
// clang-format off
run_javascript(
QString(R"JS(
(() => {
const $el = document.scrollingElement;
$el.scrollTo($el.scrollLeft + %1, $el.scrollTop + %2);
})()
)JS").arg(deltax).arg(deltay)
);
// clang-format on
}
void WebView::scroll_to_top() {
run_javascript(R"JS(
document.scrollingElement.scrollTo(0, 0)
)JS");
}
void WebView::scroll_to_bottom() {
run_javascript(R"JS(
document.scrollingElement.scrollTo(0, document.scrollingElement.scrollHeight)
)JS");
}
void WebView::enable_rpc_api() {
rpc_enabled = true;
auto &nullrpc = NullRPCSchemeHandler::instance();
connect(&nullrpc, &NullRPCSchemeHandler::message_received, this, &WebView::on_rpc_message);
}
void WebView::expose_rpc_function(const QString &name, const RpcFunc &action) {
exposed_functions.insert({name, action});
}
void WebView::on_rpc_message(const NullRPCMessage &message) {
if (!rpc_enabled || !exposed_functions.contains(message.name))
return;
RpcArgs args;
for (auto pair : message.params.queryItems(QUrl::FullyDecoded))
args.insert(pair);
auto func = exposed_functions.at(message.name);
func(args);
}
|