blob: 02bf2f41084c798e0c5b1ea0b0d5ca007565ce7b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
}
|