diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-23 15:42:14 +0100 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2017-01-23 15:42:14 +0100 |
commit | d886b377ac168d32668b5f3d145279a4f64de730 (patch) | |
tree | b5547077ce555e8c609495fc6b3851b827abbb3d /src/webengine/adblockinterceptor.cpp | |
parent | Removed window/title config (diff) | |
download | smolbote-d886b377ac168d32668b5f3d145279a4f64de730.tar.xz |
URL blocking
Diffstat (limited to 'src/webengine/adblockinterceptor.cpp')
-rw-r--r-- | src/webengine/adblockinterceptor.cpp | 56 |
1 files changed, 56 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; +} |