diff options
author | Andrea Diamantini <adjam7@gmail.com> | 2013-02-24 18:25:01 +0100 |
---|---|---|
committer | Andrea Diamantini <adjam7@gmail.com> | 2013-02-24 18:25:54 +0100 |
commit | 90eab081fa7f46961cd1597a7cb98c1dc32bd0b8 (patch) | |
tree | 34ab4d91018b1cc37bb0caa0a8fd72c99703ef65 | |
parent | remove consecutive duplicate word >a< (diff) | |
download | rekonq-90eab081fa7f46961cd1597a7cb98c1dc32bd0b8.tar.xz |
AdBlock Improvement
Hiding rules support (at least the easy ones, the most ones, against
hiding id && classes)
:D
To test it, take a look (in example) at the url:
http://simple-adblock.com/faq/testing-your-adblocker/
-rw-r--r-- | src/adblock/adblockmanager.cpp | 53 | ||||
-rw-r--r-- | src/adblock/adblockmanager.h | 6 | ||||
-rw-r--r-- | src/webtab/networkaccessmanager.cpp | 6 | ||||
-rw-r--r-- | src/webtab/networkaccessmanager.h | 4 | ||||
-rw-r--r-- | src/webtab/webpage.cpp | 7 |
5 files changed, 67 insertions, 9 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp index 62f8930e..51000a3b 100644 --- a/src/adblock/adblockmanager.cpp +++ b/src/adblock/adblockmanager.cpp @@ -34,6 +34,8 @@ // Local Includes #include "adblocksettingwidget.h" +#include "webpage.h" + // KDE Includes #include <KIO/FileCopyJob> #include <KStandardDirs> @@ -115,6 +117,7 @@ void AdBlockManager::loadSettings() _whiteList.clear(); _blackList.clear(); + _hideList.clear(); KConfigGroup settingsGroup(_adblockConfig, "Settings"); @@ -224,7 +227,7 @@ void AdBlockManager::loadRuleString(const QString &stringRule) const QString filter = stringRule.mid(2); if (filter.isEmpty()) return; - + _hideList << filter; return; } @@ -389,3 +392,51 @@ bool AdBlockManager::isAdblockEnabledForHost(const QString &host) { return ! _hostWhiteList.match(host); } + + +void AdBlockManager::applyHidingRules(QWebFrame *frame) +{ + if (!frame) + return; + + if (!_isAdblockEnabled) + return; + + connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(applyHidingRules(bool))); +} + + +void AdBlockManager::applyHidingRules(bool ok) +{ + if (!ok) + return; + + QWebFrame *frame = qobject_cast<QWebFrame *>(sender()); + if (!frame) + return; + + WebPage *page = qobject_cast<WebPage *>(frame->page()); + if (!page) + return; + + QString mainPageHost = page->loadingUrl().host(); + QStringList hosts = ReKonfig::whiteReferer(); + if (hosts.contains(mainPageHost)) + return; + + QWebElement document = frame->documentElement(); + + // HIDE RULES + Q_FOREACH(const QString & filter, _hideList) + { + QWebElementCollection elements = document.findAll(filter); + + Q_FOREACH(QWebElement el, elements) + { + if (el.isNull()) + continue; + kDebug() << "Hide element: " << el.localName(); + el.removeFromDocument(); + } + } +} diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h index 7f044f3e..3d329ad8 100644 --- a/src/adblock/adblockmanager.h +++ b/src/adblock/adblockmanager.h @@ -139,7 +139,7 @@ // Forward Includes class QNetworkRequest; -class WebPage; +class QWebFrame; // Definitions typedef QList<AdBlockRule> AdBlockRuleList; @@ -186,6 +186,9 @@ private Q_SLOTS: void slotFinished(KJob *); + void applyHidingRules(QWebFrame *); + void applyHidingRules(bool); + Q_SIGNALS: void reloadCurrentPage(); @@ -197,6 +200,7 @@ private: AdBlockHostMatcher _hostWhiteList; AdBlockRuleList _blackList; AdBlockRuleList _whiteList; + QStringList _hideList; KSharedConfig::Ptr _adblockConfig; diff --git a/src/webtab/networkaccessmanager.cpp b/src/webtab/networkaccessmanager.cpp index 95bfa47d..8e1394ca 100644 --- a/src/webtab/networkaccessmanager.cpp +++ b/src/webtab/networkaccessmanager.cpp @@ -149,7 +149,7 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR if (frame) { if (!m_blockedRequests.contains(frame)) - connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(slotFinished(bool))); + connect(frame, SIGNAL(loadFinished(bool)), this, SLOT(applyHidingBlockedElements(bool))); m_blockedRequests.insert(frame, req.url()); } @@ -157,7 +157,7 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR } -void NetworkAccessManager::slotFinished(bool ok) +void NetworkAccessManager::applyHidingBlockedElements(bool ok) { if (!ok) return; @@ -181,5 +181,5 @@ void NetworkAccessManager::slotFinished(bool ok) collection += frame->parentFrame()->findAllElements(HIDABLE_ELEMENTS); Q_FOREACH(const QUrl & url, urls) - hideBlockedElements(url, collection); + hideBlockedElements(url, collection); } diff --git a/src/webtab/networkaccessmanager.h b/src/webtab/networkaccessmanager.h index d839bdda..3bf45e18 100644 --- a/src/webtab/networkaccessmanager.h +++ b/src/webtab/networkaccessmanager.h @@ -53,8 +53,8 @@ protected: virtual QNetworkReply *createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData = 0); private Q_SLOTS: - void slotFinished(bool); - + void applyHidingBlockedElements(bool); + private: QMultiHash<QWebFrame*, QUrl> m_blockedRequests; QByteArray _acceptLanguage; diff --git a/src/webtab/webpage.cpp b/src/webtab/webpage.cpp index 958dc5c1..b61f66cf 100644 --- a/src/webtab/webpage.cpp +++ b/src/webtab/webpage.cpp @@ -38,6 +38,7 @@ #include "rekonq.h" // Local Includes +#include "adblockmanager.h" #include "downloadmanager.h" #include "historymanager.h" #include "iconmanager.h" @@ -175,6 +176,8 @@ WebPage::WebPage(QWidget *parent, bool isPrivateBrowsing) connect(this, SIGNAL(loadStarted()), this, SLOT(loadStarted())); connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool))); + connect(this, SIGNAL(frameCreated(QWebFrame *)), AdBlockManager::self(), SLOT(applyHidingRules(QWebFrame *))); + // protocol handler signals connect(&_protHandler, SIGNAL(downloadUrl(KUrl)), this, SLOT(downloadUrl(KUrl))); } @@ -292,7 +295,7 @@ bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &r if (!settings()->testAttribute(QWebSettings::PrivateBrowsingEnabled)) { // Get the SSL information sent, if any... - KIO::AccessManager *manager = qobject_cast<KIO::AccessManager*>(networkAccessManager()); + NetworkAccessManager *manager = qobject_cast<NetworkAccessManager *>(networkAccessManager()); KIO::MetaData metaData = manager->requestMetaData(); if (metaData.contains(QL1S("ssl_in_use"))) { @@ -502,7 +505,7 @@ void WebPage::loadStarted() int value = val.toInt(); if (value != 10) - mainFrame()->setZoomFactor(QVariant(value).toReal() / 10); // Don't allox max +1 values + mainFrame()->setZoomFactor(QVariant(value).toReal() / 10); // Don't allox max +1 values } |