Added custom adblocked page.

This commit is contained in:
Martin Rotter 2017-07-27 08:18:25 +02:00
parent 281e903752
commit c1b23b6f6d
13 changed files with 266 additions and 116 deletions

View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<div class="container">
<div class="row">
<div class="alert alert-danger" role="alert">
<span class="sr-only">Error:</span>
%1
</div>
%2
</div>
</div>

View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<div class="container">
<div class="row">
<div class="alert alert-danger" role="alert">
<span class="sr-only">Error:</span>
%1
</div>
%2
</div>
</div>

View File

@ -498,16 +498,18 @@ equals(USE_WEBENGINE, true) {
src/gui/webbrowser.h \
src/gui/discoverfeedsbutton.h \
src/network-web/googlesuggest.h \
src/network-web/webpage.h
src/network-web/webpage.h \
src/network-web/rssguardschemehandler.h
SOURCES += src/gui/locationlineedit.cpp \
src/gui/webviewer.cpp \
src/gui/webbrowser.cpp \
src/gui/discoverfeedsbutton.cpp \
src/network-web/googlesuggest.cpp \
src/network-web/webpage.cpp
src/network-web/webpage.cpp \
src/network-web/rssguardschemehandler.cpp
# Add Ad-Block sources.
# Add AdBlock sources.
HEADERS += src/network-web/adblock/adblockaddsubscriptiondialog.h \
src/network-web/adblock/adblockdialog.h \
src/network-web/adblock/adblockicon.h \

View File

@ -26,6 +26,7 @@
#define ARGUMENTS_LIST_SEPARATOR "\n"
#define ADBLOCK_ADBLOCKED_PAGE "adblockedpage"
#define ADBLOCK_HOWTO_FILTERS "http://adblockplus.org/en/filters"
#define ADBLOCK_UPDATE_DAYS_INTERVAL 5
#define ADBLOCK_ICON_ACTIVE "adblock"

View File

@ -73,7 +73,6 @@ FormMain::FormMain(QWidget* parent, Qt::WindowFlags f)
prepareMenus();
// Prepare tabs.
//m_ui->m_tabWidget->initializeTabs();
tabWidget()->feedMessageViewer()->feedsToolBar()->loadSavedActions();
tabWidget()->feedMessageViewer()->messagesToolBar()->loadSavedActions();

View File

@ -42,6 +42,7 @@
#include "network-web/networkurlinterceptor.h"
#include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockmanager.h"
#include "network-web/rssguardschemehandler.h"
#include <QWebEngineProfile>
#include <QWebEngineDownloadItem>
@ -71,6 +72,10 @@ Application::Application(const QString& id, int& argc, char** argv)
// TODO: Call load settings when saving app settings from dialog.
// Will need add that if I add more settings in the future.
m_urlInterceptor->loadSettings();
QWebEngineProfile::defaultProfile()->installUrlSchemeHandler(
QByteArray(APP_LOW_NAME),
new RssGuardSchemeHandler(QWebEngineProfile::defaultProfile()));
#endif
}

View File

@ -79,6 +79,15 @@ QString SkinFactory::selectedSkinName() const {
return qApp->settings()->value(GROUP(GUI), SETTING(GUI::Skin)).toString();
}
QString SkinFactory::adBlockedPage(const QString& subscription, const QString& rule) {
const QString& adblocked = currentSkin().m_adblocked.arg(tr("This page was blocked by AdBlock"),
tr("Blocked by set: \"%1\"<br/>Blocked by filter: \"%2\"")
.arg(subscription,
rule));
return currentSkin().m_layoutMarkupWrapper.arg(tr("This page was blocked by AdBlock"), adblocked);
}
Skin SkinFactory::skinInfo(const QString& skin_name, bool* ok) const {
Skin skin;
QStringList base_skin_folders;
@ -133,6 +142,7 @@ Skin SkinFactory::skinInfo(const QString& skin_name, bool* ok) const {
skin.m_enclosureMarkup = skin.m_enclosureMarkup.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_rawData = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("theme.css")));
skin.m_rawData = skin.m_rawData.replace(QSL("##"), APP_SKIN_PATH + QL1S("/") + skin_name);
skin.m_adblocked = QString::fromUtf8(IOFactory::readTextFile(skin_folder + QL1S("html_adblocked.html")));
if (ok != nullptr) {
*ok = !skin.m_author.isEmpty() && !skin.m_version.isEmpty() &&

View File

@ -31,6 +31,7 @@ struct Skin {
QString m_email;
QString m_version;
QString m_rawData;
QString m_adblocked;
QString m_layoutMarkupWrapper;
QString m_enclosureImageMarkup;
QString m_layoutMarkup;
@ -60,6 +61,8 @@ class SkinFactory : public QObject {
// after application restart.
QString selectedSkinName() const;
QString adBlockedPage(const QString& subscription, const QString& rule);
// Gets skin about a particular skin.
Skin skinInfo(const QString& skin_name, bool* ok = nullptr) const;

View File

@ -103,15 +103,19 @@ bool AdBlockManager::block(QWebEngineUrlRequestInfo& request) {
const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString);
if (blockedRule) {
res = true;
if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
// NOTE: We are blocking main URL frame, we can display "AdBlock error page" or
// redirect to somewhere.
request.block(true);
QUrlQuery query;
QUrl url(QSL("rssguard:adblockedpage"));
query.addQueryItem(QSL("rule"), blockedRule->filter());
query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title());
url.setQuery(query);
request.redirect(url);
}
else {
res = true;
request.block(true);
}
}

View File

@ -26,7 +26,5 @@ AdBlockUrlInterceptor::AdBlockUrlInterceptor(AdBlockManager* manager)
}
void AdBlockUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) {
if (m_manager->block(info)) {
info.block(true);
}
info.block(m_manager->block(info));
}

View File

@ -0,0 +1,65 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "network-web/rssguardschemehandler.h"
#include "definitions/definitions.h"
#include "miscellaneous/application.h"
#include "miscellaneous/skinfactory.h"
#include <QBuffer>
#include <QUrlQuery>
#include <QWebEngineUrlRequestJob>
RssGuardSchemeHandler::RssGuardSchemeHandler(QObject* parent) : QWebEngineUrlSchemeHandler(parent) {
}
RssGuardSchemeHandler::~RssGuardSchemeHandler() {
}
void RssGuardSchemeHandler::requestStarted(QWebEngineUrlRequestJob* job) {
// Decide which data we want.
QByteArray data = targetData(job->requestUrl());
if (data.isEmpty()) {
job->fail(QWebEngineUrlRequestJob::UrlNotFound);
}
else {
QBuffer* buf = new QBuffer(job);
buf->setData(data);
job->reply(QByteArray("text/html"), buf);
}
}
QByteArray RssGuardSchemeHandler::targetData(const QUrl& url) {
const QString& url_string = url.toString();
if (url_string.contains(QSL(ADBLOCK_ADBLOCKED_PAGE))) {
QUrlQuery query(url);
const QString& subscription = query.queryItemValue(QSL("subscription"));
const QString& rule = query.queryItemValue(QSL("rule"));
return qApp->skins()->adBlockedPage(subscription, rule).toUtf8();
}
else {
return QByteArray();
}
}

View File

@ -0,0 +1,42 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef RSSGUARDSCHEMEHANDLER_H
#define RSSGUARDSCHEMEHANDLER_H
#include <QIODevice>
#include <QWebEngineUrlSchemeHandler>
class QWebEngineUrlRequestJob;
class QBuffer;
class RssGuardSchemeHandler : public QWebEngineUrlSchemeHandler {
Q_OBJECT
public:
explicit RssGuardSchemeHandler(QObject* parent = nullptr);
virtual ~RssGuardSchemeHandler();
void requestStarted(QWebEngineUrlRequestJob* job);
private:
QByteArray targetData(const QUrl& url);
};
#endif // RSSGUARDSCHEMEHANDLER_H

View File

@ -70,7 +70,6 @@ bool WebPage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool
setHtml(view()->messageContents(), QUrl(INTERNAL_URL_MESSAGE));
return true;
}
else {
return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
}