adblock work

This commit is contained in:
Martin Rotter 2021-05-04 11:40:48 +02:00
parent 3a21265f0c
commit f344ace62e
16 changed files with 160 additions and 448 deletions

View File

@ -30,7 +30,7 @@
<url type="donation">https://martinrotter.github.io/donate/</url> <url type="donation">https://martinrotter.github.io/donate/</url>
<content_rating type="oars-1.1" /> <content_rating type="oars-1.1" />
<releases> <releases>
<release version="3.9.0" date="2021-05-03"/> <release version="3.9.0" date="2021-05-04"/>
</releases> </releases>
<content_rating type="oars-1.0"> <content_rating type="oars-1.0">
<content_attribute id="violence-cartoon">none</content_attribute> <content_attribute id="violence-cartoon">none</content_attribute>

View File

@ -33,9 +33,13 @@ const hostname = '127.0.0.1';
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
try { try {
console.log(new Date());
const chunks = []; const chunks = [];
req.on('data', chunk => chunks.push(chunk)); req.on('data', chunk => chunks.push(chunk));
req.on('end', () => { req.on('end', () => {
console.log(new Date());
try { try {
const jsonData = Buffer.concat(chunks); const jsonData = Buffer.concat(chunks);
const jsonStruct = JSON.parse(jsonData.toString()); const jsonStruct = JSON.parse(jsonData.toString());
@ -72,6 +76,8 @@ const server = http.createServer((req, res) => {
res.statusCode = 200; res.statusCode = 200;
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(resultJson)); res.end(JSON.stringify(resultJson));
console.log(new Date());
} }
catch (inner_error) { catch (inner_error) {
console.error(`adblocker: ${inner_error}.`); console.error(`adblocker: ${inner_error}.`);

View File

@ -441,7 +441,7 @@ equals(USE_WEBENGINE, true) {
network-web/webpage.cpp network-web/webpage.cpp
# Add AdBlock sources. # Add AdBlock sources.
HEADERS += network-web/adblock/adblockaddsubscriptiondialog.h \ HEADERS += \
network-web/adblock/adblockdialog.h \ network-web/adblock/adblockdialog.h \
network-web/adblock/adblockicon.h \ network-web/adblock/adblockicon.h \
network-web/adblock/adblockmanager.h \ network-web/adblock/adblockmanager.h \
@ -450,7 +450,7 @@ equals(USE_WEBENGINE, true) {
network-web/urlinterceptor.h \ network-web/urlinterceptor.h \
network-web/networkurlinterceptor.h network-web/networkurlinterceptor.h
SOURCES += network-web/adblock/adblockaddsubscriptiondialog.cpp \ SOURCES += \
network-web/adblock/adblockdialog.cpp \ network-web/adblock/adblockdialog.cpp \
network-web/adblock/adblockicon.cpp \ network-web/adblock/adblockicon.cpp \
network-web/adblock/adblockmanager.cpp \ network-web/adblock/adblockmanager.cpp \
@ -458,7 +458,7 @@ equals(USE_WEBENGINE, true) {
network-web/adblock/adblockrequestinfo.cpp \ network-web/adblock/adblockrequestinfo.cpp \
network-web/networkurlinterceptor.cpp network-web/networkurlinterceptor.cpp
FORMS += network-web/adblock/adblockaddsubscriptiondialog.ui \ FORMS += \
network-web/adblock/adblockdialog.ui network-web/adblock/adblockdialog.ui
} }
else { else {

View File

@ -25,7 +25,10 @@ DKEY AdBlock::AdBlockEnabled = "enabled";
DVALUE(bool) AdBlock::AdBlockEnabledDef = false; DVALUE(bool) AdBlock::AdBlockEnabledDef = false;
DKEY AdBlock::FilterLists = "filter_lists"; DKEY AdBlock::FilterLists = "filter_lists";
DVALUE(QStringList) AdBlock::FilterListsDef = {}; DVALUE(QStringList) AdBlock::FilterListsDef = {
QSL("https://easylist.to/easylist/easylist.txt"),
QSL("https://easylist.to/easylist/easyprivacy.txt")
};
DKEY AdBlock::CustomFilters = "custom_filters"; DKEY AdBlock::CustomFilters = "custom_filters";
DVALUE(QStringList) AdBlock::CustomFiltersDef = {}; DVALUE(QStringList) AdBlock::CustomFiltersDef = {};

View File

@ -1,83 +0,0 @@
// For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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/adblock/adblockaddsubscriptiondialog.h"
#include "definitions/definitions.h"
#include "gui/guiutilities.h"
#include "miscellaneous/application.h"
#include "miscellaneous/iconfactory.h"
AdBlockAddSubscriptionDialog::AdBlockAddSubscriptionDialog(QWidget* parent)
: QDialog(parent), m_ui(new Ui::AdBlockAddSubscriptionDialog) {
m_ui->setupUi(this);
m_knownSubscriptions
<< Subscription(QSL("EasyList"), QSL(ADBLOCK_EASYLIST_URL))
<< Subscription(QSL("EasyPrivacy"), QSL("https://easylist.to/easylist/easyprivacy.txt"))
<< Subscription(QSL("EasyPrivacy Tracking Protection List"), QSL("https://easylist-downloads.adblockplus.org/easyprivacy.tpl"))
<< Subscription(QSL("Adblock Warning Removal List"), QSL("https://easylist-downloads.adblockplus.org/antiadblockfilters.txt"));
for (const Subscription& subscription : qAsConst(m_knownSubscriptions)) {
m_ui->m_cmbPresets->addItem(subscription.m_title);
}
connect(m_ui->m_cmbPresets, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &AdBlockAddSubscriptionDialog::indexChanged);
connect(m_ui->m_cbUsePredefined, &QCheckBox::toggled, this,
&AdBlockAddSubscriptionDialog::presetsEnabledChanged);
m_ui->m_cbUsePredefined->setChecked(true);
GuiUtilities::applyDialogProperties(*this,
qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE),
tr("Add subscription"));
}
QString AdBlockAddSubscriptionDialog::url() const {
return m_ui->m_txtUrl->text();
}
void AdBlockAddSubscriptionDialog::indexChanged(int index) {
const Subscription subscription = m_knownSubscriptions.at(index);
m_ui->m_txtUrl->setText(subscription.m_url);
}
void AdBlockAddSubscriptionDialog::presetsEnabledChanged(bool enabled) {
m_ui->m_txtUrl->setEnabled(!enabled);
m_ui->m_cmbPresets->setEnabled(enabled);
if (!enabled) {
m_ui->m_txtUrl->clear();
m_ui->m_txtUrl->setFocus();
}
else {
indexChanged(m_ui->m_cmbPresets->currentIndex());
}
}
AdBlockAddSubscriptionDialog::~AdBlockAddSubscriptionDialog() {
delete m_ui;
}
AdBlockAddSubscriptionDialog::Subscription::Subscription() {}
AdBlockAddSubscriptionDialog::Subscription::Subscription(const QString& title, const QString& url) {
m_title = title;
m_url = url;
}

View File

@ -1,58 +0,0 @@
// For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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 ADBLOCKADDSUBSCRIPTIONDIALOG_H
#define ADBLOCKADDSUBSCRIPTIONDIALOG_H
#include <QDialog>
#include <QVector>
#include "ui_adblockaddsubscriptiondialog.h"
namespace Ui {
class AdBlockAddSubscriptionDialog;
}
class AdBlockAddSubscriptionDialog : public QDialog {
Q_OBJECT
public:
explicit AdBlockAddSubscriptionDialog(QWidget* parent = nullptr);
virtual ~AdBlockAddSubscriptionDialog();
QString url() const;
private slots:
void indexChanged(int index);
void presetsEnabledChanged(bool enabled);
private:
struct Subscription {
QString m_title;
QString m_url;
explicit Subscription();
explicit Subscription(const QString& title, const QString& url);
};
Ui::AdBlockAddSubscriptionDialog* m_ui;
QVector<Subscription> m_knownSubscriptions;
};
#endif // ADBLOCKADDSUBSCRIPTIONDIALOG_H

View File

@ -1,117 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AdBlockAddSubscriptionDialog</class>
<widget class="QDialog" name="AdBlockAddSubscriptionDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>440</width>
<height>145</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="m_cbUsePredefined">
<property name="text">
<string>Use predefined subscription</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="m_cmbPresets">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="m_lblUrl">
<property name="text">
<string>URL</string>
</property>
<property name="buddy">
<cstring>m_txtUrl</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="m_txtUrl">
<property name="placeholderText">
<string>Absolute URL to online subscription file</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0" colspan="2">
<widget class="QDialogButtonBox" name="m_buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>m_cbUsePredefined</tabstop>
<tabstop>m_cmbPresets</tabstop>
<tabstop>m_txtUrl</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>m_buttonBox</sender>
<signal>accepted()</signal>
<receiver>AdBlockAddSubscriptionDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>283</x>
<y>111</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>132</y>
</hint>
</hints>
</connection>
<connection>
<sender>m_buttonBox</sender>
<signal>rejected()</signal>
<receiver>AdBlockAddSubscriptionDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>351</x>
<y>111</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>132</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -1,25 +1,7 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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/adblock/adblockdialog.h" #include "network-web/adblock/adblockdialog.h"
#include "network-web/adblock/adblockaddsubscriptiondialog.h"
#include "network-web/adblock/adblockmanager.h" #include "network-web/adblock/adblockmanager.h"
#include "definitions/definitions.h" #include "definitions/definitions.h"
@ -34,60 +16,53 @@
#include <QTimer> #include <QTimer>
AdBlockDialog::AdBlockDialog(QWidget* parent) AdBlockDialog::AdBlockDialog(QWidget* parent)
: QDialog(parent), m_manager(qApp->web()->adBlock()), m_loaded(false), m_ui(new Ui::AdBlockDialog) { : QDialog(parent), m_manager(qApp->web()->adBlock()), m_loaded(false) {
m_ui->setupUi(this); m_ui.setupUi(this);
m_ui->m_cbEnable->setChecked(m_manager->isEnabled()); m_ui.m_cbEnable->setChecked(m_manager->isEnabled());
GuiUtilities::applyDialogProperties(*this, GuiUtilities::applyDialogProperties(*this,
qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE), qApp->icons()->miscIcon(ADBLOCK_ICON_ACTIVE),
tr("AdBlock configuration")); tr("AdBlock configuration"));
QPushButton* btn_options = m_ui->m_buttonBox->addButton(QDialogButtonBox::FirstButton); QPushButton* btn_options = m_ui.m_buttonBox->addButton(tr("Options"),
QDialogButtonBox::ButtonRole::HelpRole);
btn_options->setText(tr("Options"));
auto* menu = new QMenu(btn_options); auto* menu = new QMenu(btn_options);
m_actionAddSubscription = menu->addAction(tr("Add subscription"), this, &AdBlockDialog::addSubscription); menu->addAction(tr("Learn about writing rules..."), this, &AdBlockDialog::learnAboutAdblock);
menu->addSeparator();
menu->addAction(tr("Learn about writing rules..."), this, &AdBlockDialog::learnAboutRules);
btn_options->setMenu(menu); btn_options->setMenu(menu);
connect(m_ui->m_cbEnable, &QCheckBox::toggled, this, &AdBlockDialog::enableAdBlock); connect(m_ui.m_cbEnable, &QCheckBox::toggled, this, &AdBlockDialog::enableAdBlock);
connect(m_ui->m_buttonBox, &QDialogButtonBox::rejected, this, &AdBlockDialog::close); connect(m_ui.m_buttonBox, &QDialogButtonBox::rejected, this, &AdBlockDialog::saveAndClose);
load(); load();
m_ui->m_buttonBox->setFocus(); m_ui.m_buttonBox->setFocus();
} }
void AdBlockDialog::addSubscription() { void AdBlockDialog::saveAndClose() {
AdBlockAddSubscriptionDialog dialog(this); m_manager->setFilterLists(m_ui.m_txtPredefined->toPlainText().split(QSL("\n")));
m_manager->setCustomFilters(m_ui.m_txtCustom->toPlainText().split(QSL("\n")));
m_manager->updateUnifiedFiltersFile();
if (dialog.exec() != QDialog::Accepted) { close();
return;
}
QString url = dialog.url();
// TODO: add filter list.
} }
void AdBlockDialog::enableAdBlock(bool state) { void AdBlockDialog::enableAdBlock(bool enable) {
m_manager->load(false); m_manager->load(false);
if (state) { if (enable) {
load(); load();
} }
} }
void AdBlockDialog::learnAboutRules() { void AdBlockDialog::learnAboutAdblock() {
qApp->web()->openUrlInExternalBrowser(QSL(ADBLOCK_HOWTO)); qApp->web()->openUrlInExternalBrowser(QSL(ADBLOCK_HOWTO));
} }
void AdBlockDialog::load() { void AdBlockDialog::load() {
if (m_loaded || !m_ui->m_cbEnable->isChecked()) { if (m_loaded || !m_ui.m_cbEnable->isChecked()) {
return; return;
} }
// TODO: load m_ui.m_txtCustom->setPlainText(m_manager->customFilters().join(QSL("\n")));
m_ui.m_txtPredefined->setPlainText(m_manager->filterLists().join(QSL("\n")));
} }

View File

@ -1,22 +1,5 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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 ADBLOCKDIALOG_H #ifndef ADBLOCKDIALOG_H
#define ADBLOCKDIALOG_H #define ADBLOCKDIALOG_H
@ -33,19 +16,18 @@ class AdBlockDialog : public QDialog {
explicit AdBlockDialog(QWidget* parent = nullptr); explicit AdBlockDialog(QWidget* parent = nullptr);
private slots: private slots:
void addSubscription(); void saveAndClose();
void enableAdBlock(bool state); void enableAdBlock(bool enable);
void learnAboutRules(); void learnAboutAdblock();
private: private:
void load(); void load();
private: private:
AdBlockManager* m_manager; AdBlockManager* m_manager;
QAction* m_actionAddSubscription;
bool m_loaded; bool m_loaded;
Ui::AdBlockDialog* m_ui; Ui::AdBlockDialog m_ui;
}; };
#endif // ADBLOCKDIALOG_H #endif // ADBLOCKDIALOG_H

View File

@ -27,13 +27,20 @@
<item> <item>
<widget class="QTabWidget" name="m_tcSubscriptions"> <widget class="QTabWidget" name="m_tcSubscriptions">
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>0</number>
</property> </property>
<widget class="QWidget" name="m_tabPredefinedLists"> <widget class="QWidget" name="m_tabPredefinedLists">
<attribute name="title"> <attribute name="title">
<string>Filter lists (list per line)</string> <string>Filter lists (list per line)</string>
</attribute> </attribute>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Add your direct links to filter lists here (one URL per line)</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPlainTextEdit" name="m_txtPredefined"/> <widget class="QPlainTextEdit" name="m_txtPredefined"/>
</item> </item>
@ -43,7 +50,14 @@
<attribute name="title"> <attribute name="title">
<string>Custom filters</string> <string>Custom filters</string>
</attribute> </attribute>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Add your custom filters here (one filter per line)</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPlainTextEdit" name="m_txtCustom"/> <widget class="QPlainTextEdit" name="m_txtCustom"/>
</item> </item>

View File

@ -1,22 +1,5 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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/adblock/adblockicon.h" #include "network-web/adblock/adblockicon.h"
#include "gui/dialogs/formmain.h" #include "gui/dialogs/formmain.h"

View File

@ -1,22 +1,5 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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 ADBLOCKICON_H #ifndef ADBLOCKICON_H
#define ADBLOCKICON_H #define ADBLOCKICON_H

View File

@ -1,39 +1,21 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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/adblock/adblockmanager.h" #include "network-web/adblock/adblockmanager.h"
#include "exceptions/applicationexception.h"
#include "miscellaneous/application.h" #include "miscellaneous/application.h"
#include "miscellaneous/settings.h" #include "miscellaneous/settings.h"
#include "network-web/adblock/adblockdialog.h" #include "network-web/adblock/adblockdialog.h"
#include "network-web/adblock/adblockicon.h" #include "network-web/adblock/adblockicon.h"
#include "network-web/adblock/adblockrequestinfo.h" #include "network-web/adblock/adblockrequestinfo.h"
#include "network-web/adblock/adblockurlinterceptor.h" #include "network-web/adblock/adblockurlinterceptor.h"
#include "network-web/networkfactory.h"
#include "network-web/networkurlinterceptor.h" #include "network-web/networkurlinterceptor.h"
#include "network-web/webfactory.h" #include "network-web/webfactory.h"
#include <QDateTime> #include <QDateTime>
#include <QDir> #include <QDir>
#include <QMessageBox> #include <QMessageBox>
#include <QMutexLocker>
#include <QSaveFile>
#include <QTextStream>
#include <QTimer> #include <QTimer>
#include <QUrlQuery> #include <QUrlQuery>
#include <QWebEngineProfile> #include <QWebEngineProfile>
@ -46,9 +28,7 @@ AdBlockManager::AdBlockManager(QObject* parent)
m_unifiedFiltersFile = qApp->userDataFolder() + QDir::separator() + QSL("adblock-unified-filters.txt"); m_unifiedFiltersFile = qApp->userDataFolder() + QDir::separator() + QSL("adblock-unified-filters.txt");
} }
bool AdBlockManager::block(const AdblockRequestInfo& request) { bool AdBlockManager::block(const AdblockRequestInfo& request) const {
QMutexLocker locker(&m_mutex);
if (!isEnabled()) { if (!isEnabled()) {
return false; return false;
} }
@ -66,7 +46,6 @@ bool AdBlockManager::block(const AdblockRequestInfo& request) {
} }
void AdBlockManager::load(bool initial_load) { void AdBlockManager::load(bool initial_load) {
QMutexLocker locker(&m_mutex);
auto new_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool(); auto new_enabled = qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::AdBlockEnabled)).toBool();
if (!initial_load) { if (!initial_load) {
@ -90,14 +69,7 @@ void AdBlockManager::load(bool initial_load) {
} }
if (m_enabled) { if (m_enabled) {
if (!QFile::exists(m_unifiedFiltersFile)) { updateUnifiedFiltersFile();
updateUnifiedFiltersFile();
}
}
else {
if (QFile::exists(m_unifiedFiltersFile)) {
QFile::remove(m_unifiedFiltersFile);
}
} }
} }
@ -114,7 +86,23 @@ QString AdBlockManager::elementHidingRulesForDomain(const QUrl& url) const {
return {}; return {};
} }
QString AdBlockManager::generateJsForElementHiding(const QString& css) const { QStringList AdBlockManager::filterLists() const {
return qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::FilterLists)).toStringList();
}
void AdBlockManager::setFilterLists(const QStringList& filter_lists) {
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::FilterLists, filter_lists);
}
QStringList AdBlockManager::customFilters() const {
return qApp->settings()->value(GROUP(AdBlock), SETTING(AdBlock::CustomFilters)).toStringList();
}
void AdBlockManager::setCustomFilters(const QStringList& custom_filters) {
qApp->settings()->setValue(GROUP(AdBlock), AdBlock::CustomFilters, custom_filters);
}
QString AdBlockManager::generateJsForElementHiding(const QString& css) {
QString source = QL1S("(function() {" QString source = QL1S("(function() {"
"var head = document.getElementsByTagName('head')[0];" "var head = document.getElementsByTagName('head')[0];"
"if (!head) return;" "if (!head) return;"
@ -135,7 +123,57 @@ void AdBlockManager::showDialog() {
AdBlockDialog(qApp->mainFormWidget()).exec(); AdBlockDialog(qApp->mainFormWidget()).exec();
} }
void AdBlockManager::updateUnifiedFiltersFile() { void AdBlockManager::restartServer() {
// TODO: download contents of all filter lists + append custom filters // TODO:
// and combine into single file. }
void AdBlockManager::updateUnifiedFiltersFile() {
if (QFile::exists(m_unifiedFiltersFile)) {
QFile::remove(m_unifiedFiltersFile);
}
// TODO: generate file
QByteArray unified_contents;
auto filter_lists = filterLists();
// Download filters one by one and append.
for (const QString& filter_list_url : qAsConst(filter_lists)) {
QByteArray out;
auto res = NetworkFactory::performNetworkOperation(filter_list_url,
2000,
{},
out, QNetworkAccessManager::Operation::GetOperation);
if (res.first == QNetworkReply::NetworkError::NoError) {
unified_contents += out;
}
else {
qWarningNN << LOGSEC_ADBLOCK
<< "Failed to download list of filters"
<< QUOTE_W_SPACE(filter_list_url)
<< "with error"
<< QUOTE_W_SPACE_DOT(res.first);
}
}
unified_contents += customFilters().join(QSL("\n")).toUtf8();
// Save.
m_unifiedFiltersFile = IOFactory::getSystemFolder(QStandardPaths::StandardLocation::TempLocation) +
QDir::separator() +
QSL("adblock.filters");
try {
IOFactory::writeFile(m_unifiedFiltersFile, unified_contents);
if (m_enabled) {
// TODO: re-start nodejs adblock server.
restartServer();
}
}
catch (const ApplicationException& ex) {
qCriticalNN << LOGSEC_ADBLOCK
<< "Failed to write unified filters to file, error:"
<< QUOTE_W_SPACE_DOT(ex.message());
}
} }

View File

@ -1,29 +1,9 @@
// For license of this file, see <project-root-folder>/LICENSE.md. // For license of this file, see <project-root-folder>/LICENSE.md.
//
// 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 ADBLOCKMANAGER_H #ifndef ADBLOCKMANAGER_H
#define ADBLOCKMANAGER_H #define ADBLOCKMANAGER_H
#include <QMutex>
#include <QObject> #include <QObject>
#include <QPointer>
#include <QStringList>
class QUrl; class QUrl;
class AdblockRequestInfo; class AdblockRequestInfo;
@ -42,32 +22,38 @@ class AdBlockManager : public QObject {
// initial loading of Adblock. // initial loading of Adblock.
void load(bool initial_load); void load(bool initial_load);
// General method for adblocking. Returns true if request should be blocked.
bool block(const AdblockRequestInfo& request);
bool isEnabled() const; bool isEnabled() const;
bool canRunOnScheme(const QString& scheme) const; bool canRunOnScheme(const QString& scheme) const;
QString elementHidingRulesForDomain(const QUrl& url) const;
QString generateJsForElementHiding(const QString& css) const;
AdBlockIcon* adBlockIcon() const; AdBlockIcon* adBlockIcon() const;
// General methods for adblocking.
bool block(const AdblockRequestInfo& request) const;
QString elementHidingRulesForDomain(const QUrl& url) const;
QStringList filterLists() const;
void setFilterLists(const QStringList& filter_lists);
QStringList customFilters() const;
void setCustomFilters(const QStringList& custom_filters);
void updateUnifiedFiltersFile();
static QString generateJsForElementHiding(const QString& css);
public slots: public slots:
void showDialog(); void showDialog();
signals: signals:
void enabledChanged(bool enabled); void enabledChanged(bool enabled);
private slots: private:
void updateUnifiedFiltersFile(); void restartServer();
private: private:
bool m_loaded; bool m_loaded;
bool m_enabled; bool m_enabled;
AdBlockIcon* m_adblockIcon; AdBlockIcon* m_adblockIcon;
AdBlockUrlInterceptor* m_interceptor; AdBlockUrlInterceptor* m_interceptor;
QMutex m_mutex;
QString m_unifiedFiltersFile; QString m_unifiedFiltersFile;
}; };

View File

@ -16,40 +16,40 @@ QWebEngineUrlRequestInfo::ResourceType AdblockRequestInfo::resourceType() const
return m_resourceType; return m_resourceType;
} }
void AdblockRequestInfo::setResourceType(const QWebEngineUrlRequestInfo::ResourceType& resourceType) { void AdblockRequestInfo::setResourceType(const QWebEngineUrlRequestInfo::ResourceType& resource_type) {
m_resourceType = resourceType; m_resourceType = resource_type;
} }
QWebEngineUrlRequestInfo::NavigationType AdblockRequestInfo::navigationType() const { QWebEngineUrlRequestInfo::NavigationType AdblockRequestInfo::navigationType() const {
return m_navigationType; return m_navigationType;
} }
void AdblockRequestInfo::setNavigationType(const QWebEngineUrlRequestInfo::NavigationType& navigationType) { void AdblockRequestInfo::setNavigationType(const QWebEngineUrlRequestInfo::NavigationType& navigation_type) {
m_navigationType = navigationType; m_navigationType = navigation_type;
} }
QUrl AdblockRequestInfo::requestUrl() const { QUrl AdblockRequestInfo::requestUrl() const {
return m_requestUrl; return m_requestUrl;
} }
void AdblockRequestInfo::setRequestUrl(const QUrl& requestUrl) { void AdblockRequestInfo::setRequestUrl(const QUrl& request_url) {
m_requestUrl = requestUrl; m_requestUrl = request_url;
} }
QUrl AdblockRequestInfo::firstPartyUrl() const { QUrl AdblockRequestInfo::firstPartyUrl() const {
return m_firstPartyUrl; return m_firstPartyUrl;
} }
void AdblockRequestInfo::setFirstPartyUrl(const QUrl& firstPartyUrl) { void AdblockRequestInfo::setFirstPartyUrl(const QUrl& first_party_url) {
m_firstPartyUrl = firstPartyUrl; m_firstPartyUrl = first_party_url;
} }
QByteArray AdblockRequestInfo::requestMethod() const { QByteArray AdblockRequestInfo::requestMethod() const {
return m_requestMethod; return m_requestMethod;
} }
void AdblockRequestInfo::setRequestMethod(const QByteArray& requestMethod) { void AdblockRequestInfo::setRequestMethod(const QByteArray& request_method) {
m_requestMethod = requestMethod; m_requestMethod = request_method;
} }
void AdblockRequestInfo::initialize(const QWebEngineUrlRequestInfo& webengine_info) { void AdblockRequestInfo::initialize(const QWebEngineUrlRequestInfo& webengine_info) {

View File

@ -11,19 +11,19 @@ class AdblockRequestInfo {
explicit AdblockRequestInfo(const QUrl& url); explicit AdblockRequestInfo(const QUrl& url);
QWebEngineUrlRequestInfo::ResourceType resourceType() const; QWebEngineUrlRequestInfo::ResourceType resourceType() const;
void setResourceType(const QWebEngineUrlRequestInfo::ResourceType& resourceType); void setResourceType(const QWebEngineUrlRequestInfo::ResourceType& resource_type);
QWebEngineUrlRequestInfo::NavigationType navigationType() const; QWebEngineUrlRequestInfo::NavigationType navigationType() const;
void setNavigationType(const QWebEngineUrlRequestInfo::NavigationType& navigationType); void setNavigationType(const QWebEngineUrlRequestInfo::NavigationType& navigation_type);
QUrl requestUrl() const; QUrl requestUrl() const;
void setRequestUrl(const QUrl& requestUrl); void setRequestUrl(const QUrl& request_url);
QUrl firstPartyUrl() const; QUrl firstPartyUrl() const;
void setFirstPartyUrl(const QUrl& firstPartyUrl); void setFirstPartyUrl(const QUrl& first_party_url);
QByteArray requestMethod() const; QByteArray requestMethod() const;
void setRequestMethod(const QByteArray& requestMethod); void setRequestMethod(const QByteArray& request_method);
private: private:
void initialize(const QWebEngineUrlRequestInfo& webengine_info); void initialize(const QWebEngineUrlRequestInfo& webengine_info);