From d8e0556d3e0615147fbbac98619daca68f8c285a Mon Sep 17 00:00:00 2001 From: nowrep Date: Wed, 4 Jul 2012 17:53:49 +0200 Subject: [PATCH] AdBlock: Changes to element blocking rules are now instant. - instead of need to reload settings / restart browser --- src/lib/adblock/adblockmanager.cpp | 10 +++++++++- src/lib/adblock/adblockrule.cpp | 4 ++++ src/lib/adblock/adblocksubscription.cpp | 16 ++++++++++++++++ src/lib/adblock/adblocksubscription.h | 3 +++ src/lib/app/mainapplication.cpp | 21 ++++++++++++++------- src/lib/app/mainapplication.h | 1 + tests/adblock.html | 16 ++++++++++++---- 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/lib/adblock/adblockmanager.cpp b/src/lib/adblock/adblockmanager.cpp index 7813d1e6e..a153f23fb 100644 --- a/src/lib/adblock/adblockmanager.cpp +++ b/src/lib/adblock/adblockmanager.cpp @@ -211,16 +211,20 @@ void AdBlockManager::load() AdBlockSubscription* subscription = new AdBlockSubscription(title, this); subscription->setUrl(url); subscription->setFilePath(absolutePath); + connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); + m_subscriptions.append(subscription); } // Prepend EasyList AdBlockSubscription* easyList = new AdBlockEasyList(this); m_subscriptions.prepend(easyList); + connect(easyList, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); // Append CustomList - AdBlockSubscription* customList = new AdBlockCustomList(this); + AdBlockCustomList* customList = new AdBlockCustomList(this); m_subscriptions.append(customList); + connect(customList, SIGNAL(subscriptionEdited()), mApp, SLOT(reloadUserStyleSheet())); // Load all subscriptions foreach(AdBlockSubscription * subscription, m_subscriptions) { @@ -286,6 +290,10 @@ bool AdBlockManager::canBeBlocked(const QUrl &url) const QString AdBlockManager::elementHidingRules() const { + if (!m_enabled) { + return QString(); + } + QString rules; foreach(AdBlockSubscription * subscription, m_subscriptions) { diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index f094dea3a..445acbcc5 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -260,6 +260,10 @@ bool AdBlockRule::urlMatch(const QUrl &url) const bool AdBlockRule::matchDomain(const QString &domain) const { + if (!m_enabled) { + return false; + } + if (!m_domainRestricted) { return true; } diff --git a/src/lib/adblock/adblocksubscription.cpp b/src/lib/adblock/adblocksubscription.cpp index 20348bc72..554597592 100644 --- a/src/lib/adblock/adblocksubscription.cpp +++ b/src/lib/adblock/adblocksubscription.cpp @@ -270,6 +270,11 @@ const AdBlockRule* AdBlockSubscription::enableRule(int offset) rule->setEnabled(true); AdBlockManager::instance()->removeDisabledRule(rule->filter()); + if (rule->isCssRule()) { + populateCache(); + mApp->reloadUserStyleSheet(); + } + return rule; } @@ -283,6 +288,11 @@ const AdBlockRule* AdBlockSubscription::disableRule(int offset) rule->setEnabled(false); AdBlockManager::instance()->addDisabledRule(rule->filter()); + if (rule->isCssRule()) { + populateCache(); + mApp->reloadUserStyleSheet(); + } + return rule; } @@ -429,6 +439,8 @@ int AdBlockCustomList::addRule(const AdBlockRule &rule) m_rules.append(rule); populateCache(); + emit subscriptionEdited(); + return m_rules.count() - 1; } @@ -443,6 +455,8 @@ bool AdBlockCustomList::removeRule(int offset) m_rules.removeAt(offset); populateCache(); + emit subscriptionEdited(); + AdBlockManager::instance()->removeDisabledRule(filter); return true; @@ -457,5 +471,7 @@ const AdBlockRule* AdBlockCustomList::replaceRule(const AdBlockRule &rule, int o m_rules[offset] = rule; populateCache(); + emit subscriptionEdited(); + return &m_rules[offset]; } diff --git a/src/lib/adblock/adblocksubscription.h b/src/lib/adblock/adblocksubscription.h index ae6c68cb0..e80a7406b 100644 --- a/src/lib/adblock/adblocksubscription.h +++ b/src/lib/adblock/adblocksubscription.h @@ -157,6 +157,9 @@ public: int addRule(const AdBlockRule &rule); bool removeRule(int offset); const AdBlockRule* replaceRule(const AdBlockRule &rule, int offset); + +signals: + void subscriptionEdited(); }; #endif // ADBLOCKSUBSCRIPTION_H diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 11d3c6e51..de9bb1c64 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -756,21 +756,28 @@ void MainApplication::startPrivateBrowsing() } } +void MainApplication::reloadUserStyleSheet() +{ + Settings settings; + settings.beginGroup("Web-Browser-Settings"); + m_websettings->setUserStyleSheetUrl(userStyleSheet(settings.value("userStyleSheet", "").toString())); + settings.endGroup(); +} + QUrl MainApplication::userStyleSheet(const QString &filePath) const { - QString userStyle; + QString userStyle = AdBlockManager::instance()->elementHidingRules() + "{ display:none !important;}"; QFile file(filePath); if (!filePath.isEmpty() && file.open(QFile::ReadOnly)) { - userStyle = file.readAll(); - userStyle.remove('\n'); + QString fileData = file.readAll(); + fileData.remove('\n'); + userStyle.append(fileData); file.close(); } - userStyle.append(AdBlockManager::instance()->elementHidingRules() + "{ display:none !important;}"); - - QString encodedStyle = userStyle.toAscii().toBase64(); - QString dataString = QString("data:text/css;charset=utf-8;base64,%1").arg(encodedStyle); + const QString &encodedStyle = userStyle.toAscii().toBase64(); + const QString &dataString = QString("data:text/css;charset=utf-8;base64,%1").arg(encodedStyle); return QUrl(dataString); } diff --git a/src/lib/app/mainapplication.h b/src/lib/app/mainapplication.h index 6dc9cbd21..b0f646a17 100644 --- a/src/lib/app/mainapplication.h +++ b/src/lib/app/mainapplication.h @@ -109,6 +109,7 @@ public slots: void addNewTab(const QUrl &url = QUrl()); void startPrivateBrowsing(); + void reloadUserStyleSheet(); signals: void message(Qz::AppMessageType mes, bool state); diff --git a/tests/adblock.html b/tests/adblock.html index eeaa6ed40..06efee8c3 100644 --- a/tests/adblock.html +++ b/tests/adblock.html @@ -3,9 +3,17 @@ AdBlock Tests &A -

Popup Block Tests

- Open Popup window (will be blocked) -
-
+

AdBlock Tests

+

Popup Blocking

+

+Open Popup window (will be blocked) +

+ +

Element hiding

+Blocking element with ##div.test-qz-ad rule. +
+If you see this text, the rule does not works! +
+