1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Implement generichide and enable elemhide

This commit is contained in:
Allan Sandfeld Jensen 2021-05-23 15:00:04 +02:00 committed by David Rosca
parent 8080d3b70b
commit b0b89339f3
5 changed files with 41 additions and 5 deletions

View File

@ -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());

View File

@ -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);

View File

@ -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<const AdBlockRule*> m_domainRestrictedCssRules;
QVector<const AdBlockRule*> m_documentRules;
QVector<const AdBlockRule*> m_elemhideRules;
QVector<const AdBlockRule*> m_generichideRules;
QString m_elementHidingRules;
AdBlockSearchTree m_networkBlockTree;

View File

@ -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

View File

@ -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)