Debian Codegraph @

src/librssguard/network-web/webengine/webenginepage.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "network-web/webengine/webenginepage.h"
4
5 #include "definitions/definitions.h"
6 #include "gui/webviewers/webengine/webengineviewer.h"
7 #include "miscellaneous/application.h"
8 #include "miscellaneous/settings.h"
9 #include "network-web/adblock/adblockmanager.h"
10 #include "network-web/adblock/adblockrequestinfo.h"
11 #include "network-web/webfactory.h"
12
13 #include <QString>
14 #include <QStringList>
15 #include <QTimer>
16 #include <QUrl>
17 #include <QUrlQuery>
18 #include <QWebEngineScript>
19
20 WebEnginePage::WebEnginePage(QObject* parent) : QWebEnginePage(qApp->web()->engineProfile(), parent) {
21 setBackgroundColor(Qt::GlobalColor::transparent);
22
23 connect(this, &QWebEnginePage::loadFinished, this, &WebEnginePage::hideUnwantedElements);
24 }
25
26 WebEngineViewer* WebEnginePage::view() const {
27 #if QT_VERSION_MAJOR == 6
28 return qobject_cast<WebEngineViewer*>(QWebEngineView::forPage(this));
29 #else
30 return qobject_cast<WebEngineViewer*>(QWebEnginePage::view());
31 #endif
32 }
33
34 QString WebEnginePage::pageHtml(const QString& url) {
35 QEventLoop loop;
36 QString html;
37
38 // Load initial DOM/page.
39 connect(this, &WebEnginePage::loadFinished, &loop, &QEventLoop::quit);
40 connect(this, &WebEnginePage::domIsIdle, &loop, &QEventLoop::quit);
41
42 load(url);
43 loop.exec();
44
45 // Page is loaded. Send artificial scroll-to-bottom and wait for changes to end.
46 // runJavaScript("window.resizeTo(3800, 2100);");
47 runJavaScript(IOFactory::readFile(BUILTIN_JS_FOLDER + QL1C('/') + OBSERVER_JS_FILE));
48 loop.exec();
49
50 toHtml([&](const QString& htm) {
51 html = htm;
52 loop.exit();
53 });
54 loop.exec();
55
56 return html;
57 }
58
59 void WebEnginePage::hideUnwantedElements() {
60 if (!qApp->web()->adBlock()->isEnabled()) {
61 return;
62 }
63
64 auto css = qApp->web()->adBlock()->elementHidingRulesForDomain(url());
65
66 if (!css.isEmpty()) {
67 auto js = qApp->web()->adBlock()->generateJsForElementHiding(css);
68
69 runJavaScript(js);
70 qDebugNN << LOGSEC_ADBLOCK << "Running domain-specific JS for element hiding rules.";
71 }
72 }
73
74 void WebEnginePage::javaScriptAlert(const QUrl& security_origin, const QString& msg) {
75 qApp
76 ->showGuiMessage(Notification::Event::GeneralEvent,
77 GuiMessage(tr("Website alert"),
78 tr("URL %1 reports this important message: %2").arg(security_origin.toString(), msg)));
79 }
80
81 bool WebEnginePage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool is_main_frame) {
82 if (type == NavigationType::NavigationTypeLinkClicked) {
83 bool open_externally_now =
84 qApp->settings()->value(GROUP(Browser), SETTING(Browser::OpenLinksInExternalBrowserRightAway)).toBool();
85
86 if (open_externally_now) {
87 qApp->web()->openUrlInExternalBrowser(url.toString());
88 return false;
89 }
90 }
91
92 if (is_main_frame) {
93 auto blocked = qApp->web()->adBlock()->block(AdblockRequestInfo(url));
94
95 if (blocked.m_blocked) {
96 // This website is entirely blocked.
97 setHtml(qApp->skins()->adBlockedPage(url.toString(), blocked.m_blockedByFilter),
98 QUrl::fromUserInput(QSL(INTERNAL_URL_ADBLOCKED)));
99 return false;
100 }
101 }
102
103 return QWebEnginePage::acceptNavigationRequest(url, type, is_main_frame);
104 }
105
106 void WebEnginePage::javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level,
107 const QString& message,
108 int line_number,
109 const QString& source_id) {
110 Q_UNUSED(level)
111
112 qWarningNN << LOGSEC_JS << message << QSL(" (source: %1:%2)").arg(source_id, QString::number(line_number));
113
114 if (message.contains(QSL("iiddllee"))) {
115 emit domIsIdle();
116 }
117 }