diff options
-rw-r--r-- | src/webengine/blockerrule.cpp | 105 | ||||
-rw-r--r-- | src/webengine/blockerrule.h | 33 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 7 |
3 files changed, 108 insertions, 37 deletions
diff --git a/src/webengine/blockerrule.cpp b/src/webengine/blockerrule.cpp index 8ecc1af..4146e85 100644 --- a/src/webengine/blockerrule.cpp +++ b/src/webengine/blockerrule.cpp @@ -48,11 +48,41 @@ BlockerRule::BlockerRule(QString rule, QObject *parent) : QString opts = pattern.mid(pattern.indexOf("$")+1); for(QString opt : opts.split(',')) { if(opt.endsWith("script")) { - m_blacklistOptions.setFlag(RuleOption::script, true); - m_whitelistOptions.setFlag(RuleOption::script, opt.startsWith("~")); + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::script, true); + } else { + m_blacklistOptions.setFlag(RuleOption::script, true); + } } else if(opt.endsWith("image")) { - m_blacklistOptions.setFlag(RuleOption::image, true); - m_whitelistOptions.setFlag(RuleOption::image, opt.startsWith("~")); + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::image, true); + } else { + m_blacklistOptions.setFlag(RuleOption::image, true); + } + } else if(opt.endsWith("stylesheet")) { + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::stylesheet, true); + } else { + m_blacklistOptions.setFlag(RuleOption::stylesheet, true); + } + } else if(opt.endsWith("object")) { + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::object, true); + } else { + m_blacklistOptions.setFlag(RuleOption::object, true); + } + } else if(opt.endsWith("object-subrequest")) { + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::objectsubrequest, true); + } else { + m_blacklistOptions.setFlag(RuleOption::objectsubrequest, true); + } + } else if(opt.endsWith("subdocument")) { + if(opt.startsWith("~")) { + m_whitelistOptions.setFlag(RuleOption::subdocument, true); + } else { + m_blacklistOptions.setFlag(RuleOption::subdocument, true); + } } } @@ -89,7 +119,13 @@ bool BlockerRule::match(const QWebEngineUrlRequestInfo &info) case DomainMatch: if(info.requestUrl().host() == m_domain) { - shouldBlock = true; + if(matchOptions(info, m_whitelistOptions)) { + shouldBlock = false; + } else { + if(matchOptions(info, m_blacklistOptions)) { + shouldBlock = true; + } + } } break; @@ -99,19 +135,6 @@ bool BlockerRule::match(const QWebEngineUrlRequestInfo &info) break; } - if(shouldBlock) { - if(m_whitelistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) { - shouldBlock = false; - } - } - if(shouldBlock) { - if(m_blacklistOptions.testFlag(RuleOption::image) && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) { - shouldBlock = true; - } else { - shouldBlock = false; - } - } - return shouldBlock; } @@ -163,3 +186,49 @@ QString BlockerRule::fromWildcardMatch(const QString &pattern) return parsed; } + +bool BlockerRule::matchOptions(const QWebEngineUrlRequestInfo &info, const RuleOptions &options) +{ + // no options are defined + if(options == 0) { + return false; + } + + bool ret = false; + switch (info.resourceType()) { + case QWebEngineUrlRequestInfo::ResourceTypeScript: + if(options.testFlag(RuleOption::script)) { + ret = true; + } + break; + case QWebEngineUrlRequestInfo::ResourceTypeImage: + if(options.testFlag(RuleOption::image)) { + ret = true; + } + break; + case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: + if(options.testFlag(RuleOption::stylesheet)) { + ret = true; + } + break; + case QWebEngineUrlRequestInfo::ResourceTypeObject: + if(options.testFlag(RuleOption::object)) { + ret = true; + } + break; + case QWebEngineUrlRequestInfo::ResourceTypePluginResource: + if(options.testFlag(RuleOption::objectsubrequest)) { + ret = true; + } + break; + case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: + if(options.testFlag(RuleOption::subdocument)) { + ret = true; + } + break; + default: + break; + } + + return ret; +} diff --git a/src/webengine/blockerrule.h b/src/webengine/blockerrule.h index df09d10..810de6d 100644 --- a/src/webengine/blockerrule.h +++ b/src/webengine/blockerrule.h @@ -38,22 +38,22 @@ public: }; enum RuleOption { - script = 0, - image = 1, - stylesheet = 2, - object = 4, - xmlhttprequest = 8, - objectsubrequest = 16, - subdocument = 32, - ping = 64, - websocket = 128, - document = 256, - - elemhide = 512, - generichide = 1024, - genericblock = 2048, - - other = 4096 + script = 1, + image = 2, + stylesheet = 4, + object = 8, + xmlhttprequest = 16, + objectsubrequest = 32, + subdocument = 64, + ping = 128, + websocket = 256, + document = 512, + + elemhide = 1024, + generichide = 2048, + genericblock = 4096, + + other = 8192 }; Q_DECLARE_FLAGS(RuleOptions, RuleOption) @@ -72,6 +72,7 @@ public slots: private: // TODO: subclass QRegularExpression and move this there QString fromWildcardMatch(const QString &pattern); + bool matchOptions(const QWebEngineUrlRequestInfo &info, const RuleOptions &options); QString m_rule; diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index fbcef64..9352af6 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -29,7 +29,7 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { for(BlockerRule *rule : m_sub->urlWhitelist()) { if(rule->match(info)) { - qDebug("OK %s", qUtf8Printable(info.requestUrl().toString())); + qDebug("OK %i %s", info.resourceType(), qUtf8Printable(info.requestUrl().toString())); return; } } @@ -37,12 +37,13 @@ void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) for(BlockerRule *rule : m_sub->urlBlacklist()) { if(rule->match(info)) { info.block(true); - qDebug(" %s", qUtf8Printable(info.requestUrl().toString())); + qDebug(" %i %s", info.resourceType(), qUtf8Printable(info.requestUrl().toString())); return; } } - qDebug("OK %s", qUtf8Printable(info.requestUrl().toString())); + // rule is neither in whitelist nor blacklist + qDebug("OK %i %s", info.resourceType(), qUtf8Printable(info.requestUrl().toString())); } |