From e53906ccae7610b00ee12c3c0c45710907d7ff81 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 24 Jul 2018 12:09:07 +0200 Subject: UrlRequestInterceptor: add filter rules --- lib/web/urlfilter/adblockrule.cpp | 70 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'lib/web/urlfilter/adblockrule.cpp') diff --git a/lib/web/urlfilter/adblockrule.cpp b/lib/web/urlfilter/adblockrule.cpp index 60262b7..58b1941 100644 --- a/lib/web/urlfilter/adblockrule.cpp +++ b/lib/web/urlfilter/adblockrule.cpp @@ -1,30 +1,13 @@ -#include "adblockrule.h" - -inline std::pair parseOption(const QString &option) -{ - if(option.endsWith(QLatin1Literal("script"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeScript, !option.startsWith(QLatin1Literal("~"))); - - } else if(option.endsWith(QLatin1Literal("image"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeImage, !option.startsWith(QLatin1Literal("~"))); - - } else if(option.endsWith(QLatin1Literal("stylesheet"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, !option.startsWith(QLatin1Literal("~"))); +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ +// Based on Falkon's AdBlockRule class - } else if(option.endsWith(QLatin1Literal("object"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeObject, !option.startsWith(QLatin1Literal("~"))); - - } else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeXhr, !option.startsWith(QLatin1Literal("~"))); - - } else if(option.endsWith(QLatin1Literal("other"))) { - return std::make_pair(QWebEngineUrlRequestInfo::ResourceTypeUnknown, !option.startsWith(QLatin1Literal("~"))); - - } else { - // unhandled pair - Q_ASSERT(false); - } -} +#include "adblockrule.h" // adblock format documentation // https://adblockplus.org/filters @@ -35,9 +18,10 @@ inline std::pair parseOption(const AdBlockRule::AdBlockRule(const QString &filter) { + originalFilter = filter; QString parsedLine = filter.trimmed(); - // there is no rule, or it"s a comment + // there is no rule, or it's a comment if(parsedLine.isEmpty() || parsedLine.startsWith("!")) { return; } @@ -51,9 +35,10 @@ AdBlockRule::AdBlockRule(const QString &filter) // exception rules if(parsedLine.startsWith(QLatin1Literal("@@"))) { - m_isException = true; + m_isBlocking = false; parsedLine.remove(0, 2); - } + } else + m_isBlocking = true; // parse options { @@ -71,16 +56,29 @@ AdBlockRule::AdBlockRule(const QString &filter) else allowedDomains.append(domain); } - } else { - auto optPair = parseOption(option); - m_resourceTypeOptions.insert(optPair.first, optPair.second); + } else if(option.endsWith(QLatin1Literal("script"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeScript, !option.startsWith(QLatin1Literal("~"))); + + } else if(option.endsWith(QLatin1Literal("image"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeImage, !option.startsWith(QLatin1Literal("~"))); + + } else if(option.endsWith(QLatin1Literal("stylesheet"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeStylesheet, !option.startsWith(QLatin1Literal("~"))); + + } else if(option.endsWith(QLatin1Literal("object"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeObject, !option.startsWith(QLatin1Literal("~"))); + + } else if(option.endsWith(QLatin1Literal("xmlhttprequest"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeXhr, !option.startsWith(QLatin1Literal("~"))); + + } else if(option.endsWith(QLatin1Literal("other"))) { + m_resourceTypeOptions.insert(QWebEngineUrlRequestInfo::ResourceTypeUnknown, !option.startsWith(QLatin1Literal("~"))); } } - } } - // regular expression rules + // regular expression rule if(parsedLine.startsWith(QLatin1Literal("/")) && parsedLine.endsWith(QLatin1Literal("/"))) { parsedLine = parsedLine.mid(1, parsedLine.length() - 2); @@ -89,7 +87,7 @@ AdBlockRule::AdBlockRule(const QString &filter) return; } - // basic filter rules + // string equals rule if(parsedLine.startsWith(QLatin1Literal("|")) && parsedLine.endsWith(QLatin1Literal("|"))) { urlMatchType = StringEquals; match = parsedLine.mid(1, parsedLine.length() - 2); @@ -117,6 +115,8 @@ AdBlockRule::AdBlockRule(const QString &filter) // separator "^" - end, ? or / if(parsedLine.contains(QLatin1Literal("*")) || parsedLine.contains(QLatin1Literal("^"))) { urlMatchType = RegularExpressionMatch; + parsedLine.replace(QLatin1Literal("||"), QLatin1Literal("^\\w+://")); + parsedLine.replace(QLatin1Literal("|"), QLatin1Literal("\\|")); parsedLine.replace(QLatin1Literal("*"), QLatin1Literal(".*")); parsedLine.replace(QLatin1Literal("^"), QLatin1Literal("($|\\?|\\/)")); regexp.setPattern(parsedLine); -- cgit v1.2.1