summaryrefslogtreecommitdiff
path: root/src/adblock
diff options
context:
space:
mode:
authorAndrea Diamantini <adjam7@gmail.com>2010-01-14 03:28:15 +0100
committerAndrea Diamantini <adjam7@gmail.com>2010-01-14 03:28:15 +0100
commit1af5fc922877a5ec4db5dfa068b0e88c55fdc1c7 (patch)
tree4d7b7f022cfdd4ec02bb5789ecf8dd50c7bf899c /src/adblock
parentAdBlock Plus rules fast summary :) (diff)
downloadrekonq-1af5fc922877a5ec4db5dfa068b0e88c55fdc1c7.tar.xz
Better AdBlocking things :)
Diffstat (limited to 'src/adblock')
-rw-r--r--src/adblock/adblockmanager.cpp73
-rw-r--r--src/adblock/adblockmanager.h36
-rw-r--r--src/adblock/adblockrule.cpp20
-rw-r--r--src/adblock/adblockrule.h3
-rw-r--r--src/adblock/tests/RULES19
-rw-r--r--src/adblock/tests/divhidingtest.html15
6 files changed, 122 insertions, 44 deletions
diff --git a/src/adblock/adblockmanager.cpp b/src/adblock/adblockmanager.cpp
index 209c2ab0..5290d561 100644
--- a/src/adblock/adblockmanager.cpp
+++ b/src/adblock/adblockmanager.cpp
@@ -30,6 +30,7 @@
// Local Includes
#include "adblocknetworkreply.h"
+#include "webpage.h"
// KDE Includes
#include <KSharedConfig>
@@ -38,6 +39,7 @@
// Qt Includes
#include <QUrl>
+#include <QWebElement>
AdBlockManager::AdBlockManager(QObject *parent)
@@ -64,11 +66,13 @@ void AdBlockManager::loadSettings()
_isAdblockEnabled = cg.readEntry("Enabled", false);
_isHideAdsEnabled = cg.readEntry("Shrink", false);
- filterList.clear();
-
// no need to load filters if adblock is not enabled :)
if(!_isAdblockEnabled)
return;
+
+ _whiteList.clear();
+ _blackList.clear();
+ _hideList.clear();
QMap<QString,QString> entryMap = cg.entryMap();
QMap<QString,QString>::ConstIterator it;
@@ -79,8 +83,27 @@ void AdBlockManager::loadSettings()
if (name.startsWith(QLatin1String("Filter")))
{
- AdBlockRule filter(url);
- filterList << filter;
+ if(url.startsWith("!"))
+ {
+ continue;
+ }
+
+ if(url.startsWith("@@"))
+ {
+ AdBlockRule rule( url.mid(2) );
+ _whiteList << rule;
+ continue;
+ }
+
+ if(url.startsWith("##"))
+ {
+ _hideList << url.mid(2);
+ }
+ else
+ {
+ AdBlockRule rule( url );
+ _blackList << rule;
+ }
}
}
}
@@ -98,14 +121,52 @@ QNetworkReply *AdBlockManager::block(const QNetworkRequest &request)
QString urlString = request.url().toString();
- foreach(const AdBlockRule &filter, filterList)
+ // check white rules before :)
+ foreach(const AdBlockRule &filter, _whiteList)
{
if(filter.match(urlString))
{
- kDebug() << "****ADBLOCK: Matched: ***********" << urlString;
+ kDebug() << "****ADBLOCK: WHITE RULE (@@) Matched: ***********" << urlString;
+ return 0;
+ }
+ }
+
+ // then check the black ones :(
+ foreach(const AdBlockRule &filter, _blackList)
+ {
+ if(filter.match(urlString))
+ {
+ kDebug() << "****ADBLOCK: BLACK RULE Matched: ***********" << urlString;
AdBlockNetworkReply *reply = new AdBlockNetworkReply(request, urlString, this);
return reply;
}
}
+
+ // no match
return 0;
}
+
+
+void AdBlockManager::applyHidingRules(WebPage *page)
+{
+ if(!page || !page->mainFrame())
+ return;
+
+ if (!_isAdblockEnabled)
+ return;
+
+ if (!_isHideAdsEnabled)
+ return;
+
+ foreach(const QString &filter, _hideList)
+ {
+ QWebElement document = page->mainFrame()->documentElement();
+ QWebElementCollection elements = document.findAll(filter);
+
+ foreach (QWebElement element, elements)
+ {
+ element.setStyleProperty(QLatin1String("visibility"), QLatin1String("hidden"));
+ element.removeFromDocument();
+ }
+ }
+}
diff --git a/src/adblock/adblockmanager.h b/src/adblock/adblockmanager.h
index 1f53b76e..45c3fa18 100644
--- a/src/adblock/adblockmanager.h
+++ b/src/adblock/adblockmanager.h
@@ -28,19 +28,6 @@
#ifndef ADBLOCK_MANAGER_H
#define ADBLOCK_MANAGER_H
-// Local Includes
-#include "adblockrule.h"
-
-// Qt Includes
-#include <QObject>
-#include <QNetworkReply>
-
-// Forward Includes
-class QNetworkRequest;
-
-// Definitions
-typedef QList<AdBlockRule> AdBlockRuleList;
-
// NOTE: AdBlockPlus Filters (fast) summary
//
@@ -114,7 +101,23 @@ typedef QList<AdBlockRule> AdBlockRuleList;
//
// The previous rule will hide every div whose class is named "advise". Usual CSS selectors apply here :)
//
-// END NOTE
+// END NOTE ----------------------------------------------------------------------------------------------------------
+
+
+// Local Includes
+#include "adblockrule.h"
+
+// Qt Includes
+#include <QObject>
+#include <QNetworkReply>
+
+// Forward Includes
+class QNetworkRequest;
+class WebPage;
+class QStringList;
+
+// Definitions
+typedef QList<AdBlockRule> AdBlockRuleList;
class AdBlockManager : public QObject
@@ -127,12 +130,15 @@ public:
void loadSettings();
QNetworkReply *block(const QNetworkRequest &request);
+ void applyHidingRules(WebPage *page);
private:
bool _isAdblockEnabled;
bool _isHideAdsEnabled;
- AdBlockRuleList filterList;
+ AdBlockRuleList _blackList;
+ AdBlockRuleList _whiteList;
+ QStringList _hideList;
};
#endif
diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp
index 870ad825..ea3e17de 100644
--- a/src/adblock/adblockrule.cpp
+++ b/src/adblock/adblockrule.cpp
@@ -60,24 +60,10 @@
AdBlockRule::AdBlockRule(const QString &filter)
- : m_cssRule(false)
- , m_exceptionRule(false)
- , m_enabledRule(true)
{
bool isRegExpRule = false;
- if (filter.startsWith(QLatin1String("!")) || filter.trimmed().isEmpty())
- m_enabledRule = false;
-
- if (filter.contains(QLatin1String("##")))
- m_cssRule = true;
-
QString parsedLine = filter;
- if (parsedLine.startsWith(QLatin1String("@@")))
- {
- m_exceptionRule = true;
- parsedLine = parsedLine.mid(2);
- }
if (parsedLine.startsWith(QLatin1Char('/')))
{
@@ -113,12 +99,6 @@ AdBlockRule::AdBlockRule(const QString &filter)
// return true means "matched rule", so stop url!
bool AdBlockRule::match(const QString &encodedUrl) const
{
- if (m_cssRule)
- return false;
-
- if (!m_enabledRule)
- return false;
-
bool matched = m_regExp.indexIn(encodedUrl) != -1;
if (matched && !m_options.isEmpty())
diff --git a/src/adblock/adblockrule.h b/src/adblock/adblockrule.h
index 8680942f..7c4c4161 100644
--- a/src/adblock/adblockrule.h
+++ b/src/adblock/adblockrule.h
@@ -72,9 +72,6 @@ public:
private:
QString convertPatternToRegExp(const QString &wildcardPattern);
- bool m_cssRule;
- bool m_exceptionRule;
- bool m_enabledRule;
QRegExp m_regExp;
QStringList m_options;
};
diff --git a/src/adblock/tests/RULES b/src/adblock/tests/RULES
new file mode 100644
index 00000000..ee8485a6
--- /dev/null
+++ b/src/adblock/tests/RULES
@@ -0,0 +1,19 @@
+! cancel ALL your rules in the adblock,
+! load these and test rekonq against
+! the proposed sites
+! Good Luck!
+!
+! This rule will block EVERY site containing adv
+adv
+!
+! This one will wildcard EVERY site containing the word "advice"
+@@advice
+!
+! Block every google site :)
+|http://google*
+!
+! Block every pdf file!
+*pdf|
+!
+! Block every div of the class "advise"
+##div.advise \ No newline at end of file
diff --git a/src/adblock/tests/divhidingtest.html b/src/adblock/tests/divhidingtest.html
new file mode 100644
index 00000000..a38e9ea1
--- /dev/null
+++ b/src/adblock/tests/divhidingtest.html
@@ -0,0 +1,15 @@
+<html>
+
+<head>
+</head>
+
+<body>
+
+<div class="advise">
+<h1>Oh oh.. an advise!!</h1>
+</div>
+<h2>no advises here :)</h2>
+At least, I hope so...
+</body>
+
+</html> \ No newline at end of file