1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 09:42:10 +02:00

AdBlock: Changes to element blocking rules are now instant.

- instead of need to reload settings / restart browser
This commit is contained in:
nowrep 2012-07-04 17:53:49 +02:00
parent 2dc0785aff
commit d8e0556d3e
7 changed files with 59 additions and 12 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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];
}

View File

@ -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

View File

@ -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);
}

View File

@ -109,6 +109,7 @@ public slots:
void addNewTab(const QUrl &url = QUrl());
void startPrivateBrowsing();
void reloadUserStyleSheet();
signals:
void message(Qz::AppMessageType mes, bool state);

View File

@ -3,9 +3,17 @@
<title>AdBlock Tests &A</title>
</head>
<body>
<h2>Popup Block Tests</h2>
<a href="javascript:window.open('http://www.google.com/aclk?sa=l&adurl=http://www.edreams.com/', '_blank', 'width=600,height=600,statusbar=no,toolbar=no')">Open Popup window (will be blocked)</a>
<br/>
<br/>
<h2>AdBlock Tests</h2>
<h3>Popup Blocking</h3>
<p>
<a href="javascript:window.open('http://www.google.com/aclk?sa=l&adurl=http://www.edreams.com/', '_blank', 'width=600,height=600,statusbar=no,toolbar=no')">Open Popup window (will be blocked)</a>
</p>
<h3>Element hiding</h3>
Blocking element with <b>##div.test-qz-ad</b> rule.
<div class="test-qz-ad" style="font-weight: bold; color: darkred;">
If you see this text, the rule does not works!
</div>
</body>
</html>