From d886b377ac168d32668b5f3d145279a4f64de730 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 23 Jan 2017 15:42:14 +0100 Subject: URL blocking --- src/webengine/adblockinterceptor.cpp | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/webengine/adblockinterceptor.cpp (limited to 'src/webengine/adblockinterceptor.cpp') 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 +#include + +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; +} -- cgit v1.2.1