aboutsummaryrefslogtreecommitdiff
path: root/src/webengine
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-01-23 15:42:14 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-01-23 15:42:14 +0100
commitd886b377ac168d32668b5f3d145279a4f64de730 (patch)
treeb5547077ce555e8c609495fc6b3851b827abbb3d /src/webengine
parentRemoved window/title config (diff)
downloadsmolbote-d886b377ac168d32668b5f3d145279a4f64de730.tar.xz
URL blocking
Diffstat (limited to 'src/webengine')
-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
4 files changed, 117 insertions, 0 deletions
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