1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 17:52:10 +02: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)) {
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;
@ -345,6 +360,27 @@ bool AdBlockRule::matchImage(const QWebEngineUrlRequestInfo &request) const
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()
{
QString parsedLine = m_filter;
@ -423,6 +459,21 @@ void AdBlockRule::parseFilter()
setException(ImageOption, option.startsWith(QL1C('~')));
++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) {
setOption(DocumentOption);
++handledOptions;

View File

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