1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

AdBlock: Load blocked page as data url instead of qupzilla:adblock

AdBlock is now completely separate main app code.
This commit is contained in:
David Rosca 2018-01-09 18:22:16 +01:00
parent f16580dc05
commit c3e4d80010
5 changed files with 22 additions and 41 deletions

View File

@ -98,7 +98,7 @@ QList<AdBlockSubscription*> AdBlockManager::subscriptions() const
return m_subscriptions; return m_subscriptions;
} }
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request, QString &ruleFilter) bool AdBlockManager::block(QWebEngineUrlRequestInfo &request, QString &ruleFilter, QString &ruleSubscription)
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
@ -122,17 +122,7 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request, QString &ruleFilte
if (blockedRule) { if (blockedRule) {
ruleFilter = blockedRule->filter(); ruleFilter = blockedRule->filter();
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { ruleSubscription = blockedRule->subscription()->title();
QUrl url(QSL("falkon:adblock"));
QUrlQuery query;
query.addQueryItem(QSL("rule"), blockedRule->filter());
query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
url.setQuery(query);
request.redirect(url);
} else {
request.block(true);
}
#ifdef ADBLOCK_DEBUG #ifdef ADBLOCK_DEBUG
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl(); qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl();
#endif #endif

View File

@ -66,7 +66,7 @@ public:
AdBlockSubscription* subscriptionByName(const QString &name) const; AdBlockSubscription* subscriptionByName(const QString &name) const;
QList<AdBlockSubscription*> subscriptions() const; QList<AdBlockSubscription*> subscriptions() const;
bool block(QWebEngineUrlRequestInfo &request, QString &ruleFilter); bool block(QWebEngineUrlRequestInfo &request, QString &ruleFilter, QString &ruleSubscription);
QVector<AdBlockedRequest> blockedRequestsForUrl(const QUrl &url) const; QVector<AdBlockedRequest> blockedRequestsForUrl(const QUrl &url) const;
void clearBlockedRequestsForUrl(const QUrl &url); void clearBlockedRequestsForUrl(const QUrl &url);

View File

@ -18,6 +18,9 @@
#include "adblockurlinterceptor.h" #include "adblockurlinterceptor.h"
#include "adblockrule.h" #include "adblockrule.h"
#include "qztools.h"
#include <QUrlQuery>
AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager) AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
: UrlInterceptor(manager) : UrlInterceptor(manager)
@ -28,10 +31,24 @@ AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager *manager)
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request) void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request)
{ {
QString ruleFilter; QString ruleFilter;
if (!m_manager->block(request, ruleFilter)) { QString ruleSubscription;
if (!m_manager->block(request, ruleFilter, ruleSubscription)) {
return; return;
} }
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
QString page;
page.append(QzTools::readAllFileContents(QSL(":adblock/data/adblock.html")));
page.replace(QSL("%FAVICON%"), QSL("qrc:adblock/data/adblock_big.png"));
page.replace(QSL("%IMAGE%"), QSL("qrc:adblock/data/adblock_big.png"));
page.replace(QSL("%TITLE%"), tr("Blocked content"));
page.replace(QSL("%RULE%"), tr("Blocked by <i>%1 (%2)</i>").arg(ruleFilter, ruleSubscription));
page = QzTools::applyDirectionToPage(page);
request.redirect(QUrl(QString::fromUtf8(QByteArray("data:text/html;base64,") + page.toUtf8().toBase64())));
} else {
request.block(true);
}
AdBlockedRequest r; AdBlockedRequest r;
r.requestUrl = request.requestUrl(); r.requestUrl = request.requestUrl();
r.firstPartyUrl = request.firstPartyUrl(); r.firstPartyUrl = request.firstPartyUrl();

View File

@ -52,7 +52,7 @@ void FalkonSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
} }
QStringList knownPages; QStringList knownPages;
knownPages << "about" << "reportbug" << "start" << "speeddial" << "config" << "restore" << "adblock"; knownPages << "about" << "reportbug" << "start" << "speeddial" << "config" << "restore";
if (knownPages.contains(job->requestUrl().path())) if (knownPages.contains(job->requestUrl().path()))
job->reply(QByteArrayLiteral("text/html"), new FalkonSchemeReply(job)); job->reply(QByteArrayLiteral("text/html"), new FalkonSchemeReply(job));
@ -116,9 +116,6 @@ void FalkonSchemeReply::loadPage()
else if (m_pageName == QLatin1String("restore")) { else if (m_pageName == QLatin1String("restore")) {
stream << restorePage(); stream << restorePage();
} }
else if (m_pageName == QLatin1String("adblock")) {
stream << adblockPage();
}
stream.flush(); stream.flush();
m_buffer.reset(); m_buffer.reset();
@ -495,25 +492,3 @@ QString FalkonSchemeReply::configPage()
return page; return page;
} }
QString FalkonSchemeReply::adblockPage()
{
static QString aPage;
if (aPage.isEmpty()) {
aPage.append(QzTools::readAllFileContents(":adblock/data/adblock.html"));
aPage.replace(QLatin1String("%FAVICON%"), QLatin1String("qrc:adblock/data/adblock_big.png"));
aPage.replace(QLatin1String("%IMAGE%"), QLatin1String("qrc:adblock/data/adblock_big.png"));
aPage.replace(QLatin1String("%TITLE%"), tr("Blocked content"));
aPage = QzTools::applyDirectionToPage(aPage);
}
QString page = aPage;
QUrlQuery query(m_job->requestUrl());
const QString rule = query.queryItemValue(QSL("rule"));
const QString subscription = query.queryItemValue(QSL("subscription"));
page.replace(QLatin1String("%RULE%"), tr("Blocked by <i>%1 (%2)</i>").arg(rule, subscription));
return page;
}

View File

@ -56,7 +56,6 @@ private:
QString speeddialPage(); QString speeddialPage();
QString restorePage(); QString restorePage();
QString configPage(); QString configPage();
QString adblockPage();
bool m_loaded; bool m_loaded;
QBuffer m_buffer; QBuffer m_buffer;