1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

AdBlock: Support for $object $subdocument and $xmlhttprequest options

- also there's now support for blocking all netscape plugins,
  not just flash plugin
This commit is contained in:
nowrep 2012-07-01 12:07:00 +02:00
parent 2ffb8ee523
commit 6228082225
8 changed files with 113 additions and 21 deletions

View File

@ -53,6 +53,8 @@
#include <QString>
#include <QStringList>
#include <QNetworkRequest>
#include <QWebFrame>
#include <QWebPage>
// Version for Qt < 4.8 has one issue, it will wrongly
// count .co.uk (and others) as second-level domain
@ -101,6 +103,12 @@ AdBlockRule::AdBlockRule(const QString &filter)
, m_useRegExp(false)
, m_thirdParty(false)
, m_thirdPartyException(false)
, m_object(false)
, m_objectException(false)
, m_subdocument(false)
, m_subdocumentException(false)
, m_xmlhttprequest(false)
, m_xmlhttprequestException(false)
, m_caseSensitivity(Qt::CaseInsensitive)
{
setFilter(filter);
@ -183,6 +191,21 @@ bool AdBlockRule::networkMatch(const QNetworkRequest &request, const QString &do
if (m_thirdParty && !matchThirdParty(request)) {
return false;
}
// Check object restrictions
if (m_object && !matchObject(request)) {
return false;
}
// Check subdocument restriction
if (m_subdocument && !matchSubdocument(request)) {
return false;
}
// Check xmlhttprequest restriction
if (m_xmlhttprequest && !matchXmlHttpRequest(request)) {
return false;
}
}
return matched;
@ -237,7 +260,40 @@ bool AdBlockRule::matchThirdParty(const QNetworkRequest &request) const
const QString &refererHost = toSecondLevelDomain(QUrl(referer));
const QString &host = toSecondLevelDomain(request.url());
return m_thirdPartyException ? refererHost == host : refererHost != host;
bool match = refererHost != host;
return m_thirdPartyException ? !match : match;
}
bool AdBlockRule::matchObject(const QNetworkRequest &request) const
{
bool match = request.attribute(QNetworkRequest::Attribute(QNetworkRequest::User + 150)).toString() == QString("object");
return m_objectException ? !match : match;
}
bool AdBlockRule::matchSubdocument(const QNetworkRequest &request) const
{
QWebFrame* originatingFrame = static_cast<QWebFrame*>(request.originatingObject());
if (!originatingFrame) {
return false;
}
QWebPage* page = originatingFrame->page();
if (!page) {
return false;
}
bool match = originatingFrame == page->mainFrame();
return m_subdocumentException ? !match : match;
}
bool AdBlockRule::matchXmlHttpRequest(const QNetworkRequest &request) const
{
bool match = request.rawHeader("X-Requested-With") == QByteArray("XMLHttpRequest");
return m_xmlhttprequestException ? !match : match;
}
void AdBlockRule::parseFilter()
@ -289,18 +345,33 @@ void AdBlockRule::parseFilter()
parseDomains(option.mid(7), '|');
++handledOptions;
}
else if (option.startsWith("match-case")) {
else if (option.endsWith("match-case")) {
m_caseSensitivity = Qt::CaseSensitive;
++handledOptions;
}
else if (option.contains("third-party")) {
else if (option.endsWith("third-party")) {
m_thirdParty = true;
m_thirdPartyException = option.startsWith('~');
++handledOptions;
}
else if (option.endsWith("object")) {
m_object = true;
m_objectException = option.startsWith('~');
++handledOptions;
}
else if (option.endsWith("subdocument")) {
m_subdocument = true;
m_subdocumentException = option.startsWith('~');
++handledOptions;
}
else if (option.endsWith("xmlhttprequest")) {
m_xmlhttprequest = true;
m_xmlhttprequestException = option.startsWith('~');
++handledOptions;
}
}
// If we don't handle all known options, it's safer to just disable this rule
// If we don't handle all options, it's safer to just disable this rule
if (handledOptions != options.count()) {
m_internalDisabled = true;
return;

View File

@ -78,6 +78,9 @@ public:
bool matchDomain(const QString &domain) const;
bool matchThirdParty(const QNetworkRequest &request) const;
bool matchObject(const QNetworkRequest &request) const;
bool matchSubdocument(const QNetworkRequest &request) const;
bool matchXmlHttpRequest(const QNetworkRequest &request) const;
private:
void parseFilter();
@ -104,6 +107,16 @@ private:
bool m_thirdParty;
bool m_thirdPartyException;
bool m_object;
bool m_objectException;
bool m_subdocument;
bool m_subdocumentException;
bool m_xmlhttprequest;
bool m_xmlhttprequestException;
Qt::CaseSensitivity m_caseSensitivity;
};

View File

@ -202,7 +202,6 @@ void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bo
// Clearing web page info from request
QNetworkRequest req = request;
req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), 0);
req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), 0);
handleUnsupportedContent(m_networkManager->get(req), page, fromPageDownload, suggestedFileName);
}

View File

@ -41,19 +41,17 @@
#include "clickablelabel.h"
#include "mainapplication.h"
#include "pluginproxy.h"
#include "adblockmanager.h"
#include "adblocksubscription.h"
#include "squeezelabelv2.h"
#include "webpage.h"
#include "globalfunctions.h"
#include "qupzilla.h"
#include "tabbedwebview.h"
#include <QHBoxLayout>
#include <QToolButton>
#include <QFormLayout>
#include <QMenu>
#include <QTimer>
#include <QWebView>
#include <QNetworkRequest>
#include <QWebHitTestResult>
@ -72,15 +70,6 @@ ClickToFlash::ClickToFlash(const QUrl &pluginUrl, const QStringList &argumentNam
, m_url(pluginUrl)
, m_page(parentPage)
{
//AdBlock
AdBlockManager* manager = AdBlockManager::instance();
if (manager->isEnabled()) {
if (manager->block(QNetworkRequest(pluginUrl))) {
QTimer::singleShot(200, this, SLOT(hideAdBlocked()));
return;
}
}
m_layout1 = new QHBoxLayout(this);
m_frame = new QFrame(this);
m_frame->setObjectName("click2flash-frame");
@ -131,7 +120,7 @@ void ClickToFlash::customContextMenuRequested(const QPoint &pos)
menu.addAction(tr("Object blocked by ClickToFlash"));
menu.addAction(tr("Show more information about object"), this, SLOT(showInfo()));
menu.addSeparator();
menu.addAction(tr("Delete object"), this, SLOT(hideAdBlocked()));
menu.addAction(tr("Delete object"), this, SLOT(hideObject()));
menu.addAction(tr("Add %1 to whitelist").arg(m_url.host()), this, SLOT(toWhitelist()));
menu.actions().at(0)->setEnabled(false);
menu.exec(mapToGlobal(pos));
@ -143,7 +132,7 @@ void ClickToFlash::toWhitelist()
load();
}
void ClickToFlash::hideAdBlocked()
void ClickToFlash::hideObject()
{
findElement();
if (!m_element.isNull()) {
@ -280,7 +269,7 @@ bool ClickToFlash::checkUrlOnElement(QWebElement el)
checkString = el.attribute("value");
}
checkString = m_page->getView()->url().resolved(QUrl(checkString)).toString(QUrl::RemoveQuery);
checkString = m_page->url().resolved(QUrl(checkString)).toString(QUrl::RemoveQuery);
return m_url.toEncoded().contains(checkString.toUtf8());
}

View File

@ -70,7 +70,7 @@ private slots:
void toWhitelist();
void findElement();
void hideAdBlocked();
void hideObject();
void showInfo();
void ensurePluginVisible();

View File

@ -19,9 +19,11 @@
#include "clicktoflash.h"
#include "mainapplication.h"
#include "pluginproxy.h"
#include "adblockmanager.h"
#include "webpage.h"
#include <QDebug>
#include <QNetworkRequest>
WebPluginFactory::WebPluginFactory(WebPage* page)
: QWebPluginFactory(page)
@ -56,6 +58,14 @@ QObject* WebPluginFactory::create(const QString &mimeType, const QUrl &url, cons
return 0;
}
// AdBlock
AdBlockManager* manager = AdBlockManager::instance();
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::Attribute(QNetworkRequest::User + 150), QString("object"));
if (manager->isEnabled() && manager->block(request)) {
return new QObject();
}
// Click2Flash whitelist
QStringList whitelist = mApp->plugins()->c2f_getWhiteList();
if (whitelist.contains(url.host()) || whitelist.contains("www." + url.host()) || whitelist.contains(url.host().remove("www."))) {

View File

@ -77,6 +77,11 @@ void SideBar::close()
{
m_manager->closeSideBar();
QWidget* p = parentWidget();
if (p) {
p->setFocus();
}
QWidget::close();
}

View File

@ -73,6 +73,11 @@ void AnimatedWidget::hide()
m_timeLine.start();
connect(&m_timeLine, SIGNAL(finished()), this, SLOT(close()));
QWidget* p = parentWidget();
if (p) {
p->setFocus();
}
}
void AnimatedWidget::resizeEvent(QResizeEvent* event)