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

AdBlockRule: Add support for script, stylesheet and object-subrequest

This commit is contained in:
David Rosca 2015-10-05 20:43:05 +02:00
parent 3050913d21
commit 88c5b271e2
2 changed files with 59 additions and 2 deletions

View File

@ -248,6 +248,21 @@ bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QS
if (hasOption(ImageOption) && !matchImage(request)) { if (hasOption(ImageOption) && !matchImage(request)) {
return false; return false;
} }
// Check script restriction
if (hasOption(ScriptOption) && !matchScript(request)) {
return false;
}
// Check stylesheet restriction
if (hasOption(StyleSheetOption) && !matchStyleSheet(request)) {
return false;
}
// Check object-subrequest restriction
if (hasOption(ObjectSubrequestOption) && !matchObjectSubrequest(request)) {
return false;
}
} }
return matched; return matched;
@ -345,6 +360,27 @@ bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const
return hasException(ImageOption) ? !match : match; return hasException(ImageOption) ? !match : match;
} }
bool AdBlockRule::matchScript(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeScript;
return hasException(ScriptOption) ? !match : match;
}
bool AdBlockRule::matchStyleSheet(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeStylesheet;
return hasException(StyleSheetOption) ? !match : match;
}
bool AdBlockRule::matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const
{
bool match = request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeSubResource;
return hasException(ObjectSubrequestOption) ? !match : match;
}
void AdBlockRule::parseFilter() void AdBlockRule::parseFilter()
{ {
QString parsedLine = m_filter; QString parsedLine = m_filter;
@ -423,6 +459,21 @@ void AdBlockRule::parseFilter()
setException(ImageOption, option.startsWith(QL1C('~'))); setException(ImageOption, option.startsWith(QL1C('~')));
++handledOptions; ++handledOptions;
} }
else if (option.endsWith(QL1S("script"))) {
setOption(ScriptOption);
setException(ScriptOption, option.startsWith(QL1C('~')));
++handledOptions;
}
else if (option.endsWith(QL1S("stylesheet"))) {
setOption(StyleSheetOption);
setException(StyleSheetOption, option.startsWith(QL1C('~')));
++handledOptions;
}
else if (option.endsWith(QL1S("object-subrequest"))) {
setOption(ObjectSubrequestOption);
setException(ObjectSubrequestOption, option.startsWith(QL1C('~')));
++handledOptions;
}
else if (option == QL1S("document") && m_isException) { else if (option == QL1S("document") && m_isException) {
setOption(DocumentOption); setOption(DocumentOption);
++handledOptions; ++handledOptions;

View File

@ -99,6 +99,9 @@ public:
bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const; bool matchSubdocument(const QWebEngineUrlRequestInfo &request) const;
bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const; bool matchXmlHttpRequest(const QWebEngineUrlRequestInfo &request) const;
bool matchImage(const QWebEngineUrlRequestInfo &request) const; bool matchImage(const QWebEngineUrlRequestInfo &request) const;
bool matchScript(const QWebEngineUrlRequestInfo &request) const;
bool matchStyleSheet(const QWebEngineUrlRequestInfo &request) const;
bool matchObjectSubrequest(const QWebEngineUrlRequestInfo &request) const;
protected: protected:
bool stringMatch(const QString &domain, const QString &encodedUrl) const; bool stringMatch(const QString &domain, const QString &encodedUrl) const;
@ -123,10 +126,13 @@ private:
SubdocumentOption = 8, SubdocumentOption = 8,
XMLHttpRequestOption = 16, XMLHttpRequestOption = 16,
ImageOption = 32, ImageOption = 32,
ScriptOption = 64,
StyleSheetOption = 128,
ObjectSubrequestOption = 256,
// Exception only options // Exception only options
DocumentOption = 64, DocumentOption = 1024,
ElementHideOption = 128 ElementHideOption = 2048
}; };
Q_DECLARE_FLAGS(RuleOptions, RuleOption) Q_DECLARE_FLAGS(RuleOptions, RuleOption)