aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mainwindow.cpp2
-rw-r--r--src/mainwindow.h1
-rw-r--r--src/smolbote.pro8
-rw-r--r--src/webengine/adblockinterceptor.cpp56
-rw-r--r--src/webengine/adblockinterceptor.h25
-rw-r--r--src/webengine/adblockrule.cpp12
-rw-r--r--src/webengine/adblockrule.h24
7 files changed, 126 insertions, 2 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 6985292..3f23448 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -137,6 +137,8 @@ void MainWindow::loadProfile(const QString &name)
profile = new WebEngineProfile(profileName, this);
}
+ AdBlockInterceptor *interceptor = new AdBlockInterceptor(this);
+ profile->setRequestInterceptor(interceptor);
connect(profile, SIGNAL(downloadRequested(QWebEngineDownloadItem*)), downloadManager, SLOT(addDownload(QWebEngineDownloadItem*)));
}
diff --git a/src/mainwindow.h b/src/mainwindow.h
index 63ff93a..0b01783 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -29,6 +29,7 @@
#include <QUrl>
#include "widgets/webviewtabbar.h"
#include "forms/downloaddialog.h"
+#include "webengine/adblockinterceptor.h"
namespace Ui {
class MainWindow;
diff --git a/src/smolbote.pro b/src/smolbote.pro
index bf7a05d..d7505c8 100644
--- a/src/smolbote.pro
+++ b/src/smolbote.pro
@@ -19,7 +19,9 @@ SOURCES += main.cpp \
forms/profiledialog.cpp \
webengine/webengineprofile.cpp \
forms/downloaddialog.cpp \
- webengine/downloaditemform.cpp
+ webengine/downloaditemform.cpp \
+ webengine/adblockinterceptor.cpp \
+ webengine/adblockrule.cpp
HEADERS += mainwindow.h \
browser.h \
@@ -28,7 +30,9 @@ HEADERS += mainwindow.h \
forms/profiledialog.h \
webengine/webengineprofile.h \
forms/downloaddialog.h \
- webengine/downloaditemform.h
+ webengine/downloaditemform.h \
+ webengine/adblockinterceptor.h \
+ webengine/adblockrule.h
FORMS += mainwindow.ui \
forms/profiledialog.ui \
diff --git a/src/webengine/adblockinterceptor.cpp b/src/webengine/adblockinterceptor.cpp
new file mode 100644
index 0000000..02bf2f4
--- /dev/null
+++ b/src/webengine/adblockinterceptor.cpp
@@ -0,0 +1,56 @@
+#include "adblockinterceptor.h"
+
+#include <QFile>
+#include <QTextStream>
+
+AdBlockInterceptor::AdBlockInterceptor(QObject *parent) :
+ QWebEngineUrlRequestInterceptor(parent)
+{
+ loadSubscription("blocklist.txt");
+}
+
+void AdBlockInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
+{
+ bool blocked = false;
+ for(AdBlockRule *rule : m_urlBlacklist) {
+ if(rule->match(info.requestUrl())) {
+ info.block(true);
+ blocked = true;
+ }
+ }
+
+ qDebug("%i %i %i %s %s", blocked, info.navigationType(), info.resourceType(), qUtf8Printable(info.requestMethod()), qUtf8Printable(info.requestUrl().toString()));
+}
+
+int AdBlockInterceptor::loadSubscription(const QString &subpath)
+{
+ QFile subfile(subpath);
+ if(!subfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ qDebug("AdBlockInterceptor: cannot load subscription: %s", qUtf8Printable(subpath));
+ return -1;
+ }
+
+ QTextStream subscription(&subfile);
+
+ QString header = subscription.readLine();
+ if(header != "[Adblock Plus 2.0]") {
+ qDebug("AdBlockInterceptor: invalid format of subscription: %s", qUtf8Printable(subpath));
+ return -1;
+ }
+
+ int rules = 0;
+
+ while(!subscription.atEnd()) {
+ QString line = subscription.readLine();
+ if(!line.isEmpty() && !line.startsWith('!')) {
+ // The line is not a comment
+ AdBlockRule *rule = new AdBlockRule(line, this);
+ m_urlBlacklist.append(rule);
+ rules++;
+ }
+ }
+
+ qDebug("Loaded %i rules from subscription %s", rules, qUtf8Printable(subpath));
+
+ return rules;
+}
diff --git a/src/webengine/adblockinterceptor.h b/src/webengine/adblockinterceptor.h
new file mode 100644
index 0000000..061b626
--- /dev/null
+++ b/src/webengine/adblockinterceptor.h
@@ -0,0 +1,25 @@
+#ifndef ADBLOCKINTERCEPTOR_H
+#define ADBLOCKINTERCEPTOR_H
+
+#include <QWebEngineUrlRequestInterceptor>
+#include "adblockrule.h"
+
+class AdBlockInterceptor : public QWebEngineUrlRequestInterceptor
+{
+ Q_OBJECT
+public:
+ explicit AdBlockInterceptor(QObject *parent = 0);
+
+ void interceptRequest(QWebEngineUrlRequestInfo &info);
+
+ int loadSubscription(const QString &subpath);
+
+signals:
+
+public slots:
+
+private:
+ QList<AdBlockRule*> m_urlBlacklist;
+};
+
+#endif // ADBLOCKINTERCEPTOR_H
diff --git a/src/webengine/adblockrule.cpp b/src/webengine/adblockrule.cpp
new file mode 100644
index 0000000..fe3e64e
--- /dev/null
+++ b/src/webengine/adblockrule.cpp
@@ -0,0 +1,12 @@
+#include "adblockrule.h"
+
+AdBlockRule::AdBlockRule(QString rule, QObject *parent) :
+ QObject(parent)
+{
+ ruleExpression.setPattern(rule);
+}
+
+bool AdBlockRule::match(const QUrl &url)
+{
+ return ruleExpression.match(url.toString()).hasMatch();
+}
diff --git a/src/webengine/adblockrule.h b/src/webengine/adblockrule.h
new file mode 100644
index 0000000..8c68de7
--- /dev/null
+++ b/src/webengine/adblockrule.h
@@ -0,0 +1,24 @@
+#ifndef ADBLOCKRULE_H
+#define ADBLOCKRULE_H
+
+#include <QObject>
+#include <QUrl>
+#include <QRegularExpression>
+
+class AdBlockRule : public QObject
+{
+ Q_OBJECT
+public:
+ explicit AdBlockRule(QString rule, QObject *parent = 0);
+
+ bool match(const QUrl &url);
+
+signals:
+
+public slots:
+
+private:
+ QRegularExpression ruleExpression;
+};
+
+#endif // ADBLOCKRULE_H