1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

AdBlock: Show error page when the whole page was blocked

This commit is contained in:
David Rosca 2015-10-14 12:34:49 +02:00
parent 74350785b0
commit af93ecf81e
6 changed files with 47 additions and 6 deletions

View File

@ -105,14 +105,26 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo &request)
if (blockedRule) {
res = true;
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
QUrl url(QSL("qupzilla: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
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.url();
qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl();
#endif
}
#ifdef ADBLOCK_DEBUG
qDebug() << timer.elapsed() << request.url();
qDebug() << timer.elapsed() << request.requestUrl();
#endif
return res;

View File

@ -2,7 +2,7 @@
<qresource prefix="/">
<file>html/errorPage.html</file>
<file>html/adblock_big.png</file>
<file>html/adblockPage.html</file>
<file>html/adblock.html</file>
<file>html/about.html</file>
<file>html/box-border.png</file>
<file>html/copyright</file>

View File

@ -222,7 +222,7 @@ QString LocationBar::convertUrlToText(const QUrl &url)
QString stringUrl = QzTools::urlEncodeQueryString(url);
if (stringUrl == QLatin1String("qupzilla:speeddial") || stringUrl == QLatin1String("about:blank")) {
if (stringUrl == QL1S("qupzilla:speeddial") || stringUrl.startsWith(QL1S("qupzilla:adblock")) || stringUrl == QL1S("about:blank")) {
stringUrl.clear();
}

View File

@ -30,6 +30,7 @@
#include <QTimer>
#include <QSettings>
#include <QUrlQuery>
#include <QWebEngineUrlRequestJob>
static QString authorString(const char* name, const QString &mail)
@ -45,7 +46,7 @@ QupZillaSchemeHandler::QupZillaSchemeHandler(QObject *parent)
void QupZillaSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
{
QStringList knownPages;
knownPages << "about" << "reportbug" << "start" << "speeddial" << "config" << "restore";
knownPages << "about" << "reportbug" << "start" << "speeddial" << "config" << "restore" << "adblock";
if (knownPages.contains(job->requestUrl().path()))
job->reply(QByteArrayLiteral("text/html"), new QupZillaSchemeReply(job));
@ -91,6 +92,9 @@ void QupZillaSchemeReply::loadPage()
else if (m_pageName == QLatin1String("restore")) {
stream << restorePage();
}
else if (m_pageName == QLatin1String("adblock")) {
stream << adblockPage();
}
stream.flush();
m_buffer.reset();
@ -480,3 +484,27 @@ QString QupZillaSchemeReply::configPage()
return page;
}
QString QupZillaSchemeReply::adblockPage()
{
static QString aPage;
if (aPage.isEmpty()) {
aPage.append(QzTools::readAllFileContents(":html/adblock.html"));
aPage.replace(QLatin1String("%FAVICON%"), QLatin1String("qrc:html/adblock_big.png"));
aPage.replace(QLatin1String("%BOX-BORDER%"), QLatin1String("qrc:html/box-border.png"));
aPage.replace(QLatin1String("%IMAGE%"), QLatin1String("qrc:html/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

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