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

[adblock] Added support for $image rules.

It tests whether the url ends with .jpg/.jpeg/.gif/.png
This commit is contained in:
nowrep 2013-02-07 11:04:09 +01:00
parent 2bb002b62d
commit 3b301e8446
4 changed files with 31 additions and 3 deletions

View File

@ -1,6 +1,6 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -112,6 +112,8 @@ AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscriptio
, m_subdocumentException(false)
, m_xmlhttprequest(false)
, m_xmlhttprequestException(false)
, m_image(false)
, m_imageException(false)
, m_document(false)
, m_elemhide(false)
, m_caseSensitivity(Qt::CaseInsensitive)
@ -241,6 +243,11 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do
if (m_xmlhttprequest && !matchXmlHttpRequest(request)) {
return false;
}
// Check image restriction
if (m_image && !matchImage(encodedUrl)) {
return false;
}
}
return matched;
@ -348,6 +355,16 @@ bool AdBlockRule::matchXmlHttpRequest(const QNetworkRequest &request) const
return m_xmlhttprequestException ? !match : match;
}
bool AdBlockRule::matchImage(const QString &encodedUrl) const
{
bool match = encodedUrl.endsWith(QLatin1String(".png")) ||
encodedUrl.endsWith(QLatin1String(".jpg")) ||
encodedUrl.endsWith(QLatin1String(".gif")) ||
encodedUrl.endsWith(QLatin1String(".jpeg"));
return m_imageException ? !match : match;
}
void AdBlockRule::parseFilter()
{
QString parsedLine = m_filter;
@ -416,6 +433,11 @@ void AdBlockRule::parseFilter()
m_xmlhttprequestException = option.startsWith(QLatin1Char('~'));
++handledOptions;
}
else if (option.endsWith(QLatin1String("image"))) {
m_image = true;
m_imageException = option.startsWith(QLatin1Char('~'));
++handledOptions;
}
else if (option == QLatin1String("document") && m_exception) {
m_document = true;
++handledOptions;

View File

@ -1,6 +1,6 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -92,6 +92,7 @@ public:
bool matchObject(const QNetworkRequest &request) const;
bool matchSubdocument(const QNetworkRequest &request) const;
bool matchXmlHttpRequest(const QNetworkRequest &request) const;
bool matchImage(const QString &encodedUrl) const;
private:
void parseFilter();
@ -134,6 +135,9 @@ private:
bool m_xmlhttprequest;
bool m_xmlhttprequestException;
bool m_image;
bool m_imageException;
// Exception only options
bool m_document;
bool m_elemhide;

View File

@ -621,6 +621,8 @@ void WebPage::cleanBlockedObjects()
QWebElement bodyElement = docElement.findFirst("body");
bodyElement.appendInside("<style type=\"text/css\">\n/* AdBlock for QupZilla */\n" + elementHiding);
// When hiding some elements, scroll position of page will change
// If user loaded anchor link in background tab (and didn't show it yet), fix the scroll position
if (!view()->isVisible() && !url().fragment().isEmpty()) {
mainFrame()->scrollToAnchor(url().fragment());
}

View File

@ -260,7 +260,7 @@ QPixmap WebTab::renderTabPreview()
const QSize oldSize = currentWebView ? currentWebView->page()->viewportSize() : page->viewportSize();
const QPoint originalScrollPosition = page->mainFrame()->scrollPosition();
// Hack to ensure rendering the same preview before and after the page was shown for the first tie
// Hack to ensure rendering the same preview before and after the page was shown for the first time
// This can occur eg. with opening background tabs
page->setViewportSize(oldSize);