From b0b89339f3c7f7ed3aadda196b8a880425d23d53 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Sun, 23 May 2021 15:00:04 +0200 Subject: [PATCH] Implement generichide and enable elemhide --- src/lib/adblock/adblockmanager.cpp | 4 ++-- src/lib/adblock/adblockmatcher.cpp | 17 +++++++++++++++++ src/lib/adblock/adblockmatcher.h | 2 ++ src/lib/adblock/adblockrule.cpp | 20 +++++++++++++++++--- src/lib/adblock/adblockrule.h | 3 +++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/lib/adblock/adblockmanager.cpp b/src/lib/adblock/adblockmanager.cpp index 7d6c003f8..a7cb0dcc7 100644 --- a/src/lib/adblock/adblockmanager.cpp +++ b/src/lib/adblock/adblockmanager.cpp @@ -412,7 +412,7 @@ bool AdBlockManager::canBeBlocked(const QUrl &url) const QString AdBlockManager::elementHidingRules(const QUrl &url) const { - if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url)) + if (!isEnabled() || !canRunOnScheme(url.scheme()) || m_matcher->genericElemHideDisabledForUrl(url)) return QString(); return m_matcher->elementHidingRules(); @@ -420,7 +420,7 @@ QString AdBlockManager::elementHidingRules(const QUrl &url) const QString AdBlockManager::elementHidingRulesForDomain(const QUrl &url) const { - if (!isEnabled() || !canRunOnScheme(url.scheme()) || !canBeBlocked(url)) + if (!isEnabled() || !canRunOnScheme(url.scheme()) || m_matcher->elemHideDisabledForUrl(url)) return QString(); return m_matcher->elementHidingRulesForDomain(url.host()); diff --git a/src/lib/adblock/adblockmatcher.cpp b/src/lib/adblock/adblockmatcher.cpp index c3245a4f7..cdb38d3dc 100644 --- a/src/lib/adblock/adblockmatcher.cpp +++ b/src/lib/adblock/adblockmatcher.cpp @@ -83,6 +83,20 @@ bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const return false; } +bool AdBlockMatcher::genericElemHideDisabledForUrl(const QUrl &url) const +{ + if (elemHideDisabledForUrl(url)) + return true; + + int count = m_generichideRules.count(); + + for (int i = 0; i < count; ++i) + if (m_generichideRules.at(i)->urlMatch(url)) + return true; + + return false; +} + QString AdBlockMatcher::elementHidingRules() const { return m_elementHidingRules; @@ -153,6 +167,9 @@ void AdBlockMatcher::update() else if (rule->isElemhide()) { m_elemhideRules.append(rule); } + else if (rule->isGenerichide()) { + m_generichideRules.append(rule); + } else if (rule->isException()) { if (!m_networkExceptionTree.add(rule)) m_networkExceptionRules.append(rule); diff --git a/src/lib/adblock/adblockmatcher.h b/src/lib/adblock/adblockmatcher.h index 0c7825300..65b4f3566 100644 --- a/src/lib/adblock/adblockmatcher.h +++ b/src/lib/adblock/adblockmatcher.h @@ -40,6 +40,7 @@ public: bool adBlockDisabledForUrl(const QUrl &url) const; bool elemHideDisabledForUrl(const QUrl &url) const; + bool genericElemHideDisabledForUrl(const QUrl &url) const; QString elementHidingRules() const; QString elementHidingRulesForDomain(const QString &domain) const; @@ -57,6 +58,7 @@ private: QVector m_domainRestrictedCssRules; QVector m_documentRules; QVector m_elemhideRules; + QVector m_generichideRules; QString m_elementHidingRules; AdBlockSearchTree m_networkBlockTree; diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index ba334036a..f9836dd64 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -164,6 +164,11 @@ bool AdBlockRule::isElemhide() const return hasOption(ElementHideOption); } +bool AdBlockRule::isGenerichide() const +{ + return hasOption(GenericHideOption); +} + bool AdBlockRule::isDomainRestricted() const { return hasOption(DomainRestrictedOption); @@ -201,7 +206,7 @@ bool AdBlockRule::isInternalDisabled() const bool AdBlockRule::urlMatch(const QUrl &url) const { - if (!hasOption(DocumentOption) && !hasOption(ElementHideOption)) { + if (!hasOption(DocumentOption) && !hasOption(ElementHideOption) && !hasOption(GenericHideOption) && !hasOption(GenericBlockOption)) { return false; } @@ -485,6 +490,10 @@ void AdBlockRule::parseFilter() setException(OtherOption, option.startsWith(QL1C('~'))); ++handledOptions; } + else if (option == QL1S("collapse")) { + // Hiding placeholders of blocked elements is enabled by default + ++handledOptions; + } else if (option == QL1S("popup")) { // doesn't do anything yet setOption(PopupOption); @@ -498,10 +507,15 @@ void AdBlockRule::parseFilter() setOption(ElementHideOption); ++handledOptions; } - else if (option == QL1S("collapse")) { - // Hiding placeholders of blocked elements is enabled by default + else if (option == QL1S("generichide") && m_isException) { + setOption(GenericHideOption); ++handledOptions; } + else if (option == QL1S("genericblock") && m_isException) { + // doesn't do anything yet + setOption(GenericBlockOption); +// ++handledOptions; + } } // If we don't handle all options, it's safer to just disable this rule diff --git a/src/lib/adblock/adblockrule.h b/src/lib/adblock/adblockrule.h index 261fc9584..c6a817f2a 100644 --- a/src/lib/adblock/adblockrule.h +++ b/src/lib/adblock/adblockrule.h @@ -81,6 +81,7 @@ public: bool isDocument() const; bool isElemhide() const; + bool isGenerichide() const; bool isDomainRestricted() const; bool isException() const; @@ -152,6 +153,8 @@ private: // Exception only options DocumentOption = 1 << 20, ElementHideOption = 1 << 21, + GenericHideOption = 1 << 22, + GenericBlockOption = 1 << 23, }; Q_DECLARE_FLAGS(RuleOptions, RuleOption)