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

AdBlock: Make it as self-contained as possible

It could be easy now to turn it into real loadable plugin.
This commit is contained in:
David Rosca 2018-01-06 13:01:31 +01:00
parent 401e600821
commit ff1171abf0
17 changed files with 111 additions and 39 deletions

View File

@ -60,6 +60,7 @@ set(SRCS ${SRCS}
adblock/adblocksearchtree.cpp
adblock/adblocksubscription.cpp
adblock/adblocktreewidget.cpp
adblock/adblockplugin.cpp
app/autosaver.cpp
app/browserwindow.cpp
app/commandlineoptions.cpp
@ -290,6 +291,7 @@ qt5_add_resources(SRCS
data/html.qrc
data/icons.qrc
data/breeze-fallback.qrc
adblock/adblock.qrc
)
add_library(FalkonPrivate SHARED ${SRCS})

View File

@ -0,0 +1,7 @@
<RCC>
<qresource prefix="/adblock">
<file>data/adblock.png</file>
<file>data/adblock_big.png</file>
<file>data/adblock.html</file>
</qresource>
</RCC>

View File

@ -33,7 +33,7 @@ AdBlockIcon::AdBlockIcon(QObject *parent)
: AbstractButtonInterface(parent)
{
setTitle(tr("AdBlock"));
setIcon(QIcon(QSL(":icons/other/adblock.png")));
setIcon(QIcon(QSL(":adblock/data/adblock.png")));
updateState();

View File

@ -0,0 +1,61 @@
/* ============================================================
* QupZilla - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "adblockplugin.h"
#include "adblockmanager.h"
#include "adblockicon.h"
#include "scripts.h"
#include "webpage.h"
#include "pluginproxy.h"
#include "browserwindow.h"
#include "navigationbar.h"
#include "mainapplication.h"
AdBlockPlugin::AdBlockPlugin(QObject *parent)
: QObject(parent)
{
connect(mApp, &MainApplication::aboutToQuit, AdBlockManager::instance(), &AdBlockManager::save);
connect(mApp->plugins(), &PluginProxy::webPageCreated, this, &AdBlockPlugin::webPageCreated);
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &AdBlockPlugin::mainWindowCreated);
}
void AdBlockPlugin::webPageCreated(WebPage *page)
{
connect(page, &WebPage::loadFinished, this, [=]() {
AdBlockManager *manager = AdBlockManager::instance();
if (!manager->isEnabled()) {
return;
}
// Apply global element hiding rules
const QString elementHiding = manager->elementHidingRules(page->url());
if (!elementHiding.isEmpty()) {
page->runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld);
}
// Apply domain-specific element hiding rules
const QString siteElementHiding = manager->elementHidingRulesForDomain(page->url());
if (!siteElementHiding.isEmpty()) {
page->runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld);
}
});
}
void AdBlockPlugin::mainWindowCreated(BrowserWindow *window)
{
window->navigationBar()->addToolButton(new AdBlockIcon(window));
}

View File

@ -0,0 +1,33 @@
/* ============================================================
* QupZilla - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QObject>
class WebPage;
class BrowserWindow;
class AdBlockPlugin : public QObject
{
public:
explicit AdBlockPlugin(QObject *parent = nullptr);
private:
void webPageCreated(WebPage *page);
void mainWindowCreated(BrowserWindow *window);
};

View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

@ -38,7 +38,6 @@
#include "docktitlebarwidget.h"
#include "iconprovider.h"
#include "progressbar.h"
#include "adblockicon.h"
#include "closedwindowsmanager.h"
#include "statusbarmessage.h"
#include "browsinglibrary.h"
@ -389,8 +388,6 @@ void BrowserWindow::setupUi()
statusBar()->addPermanentWidget(m_progressBar);
statusBar()->addPermanentWidget(m_ipLabel);
m_navigationToolbar->addToolButton(new AdBlockIcon(this));
QDesktopWidget* desktop = mApp->desktop();
int windowWidth = desktop->availableGeometry().width() / 1.3;
int windowHeight = desktop->availableGeometry().height() / 1.3;

View File

@ -43,7 +43,6 @@ class AutoFill;
class MainApplication;
class WebView;
class WebPage;
class AdBlockIcon;
class SideBar;
class SideBarManager;
class ProgressBar;

View File

@ -35,7 +35,6 @@
#include "checkboxdialog.h"
#include "networkmanager.h"
#include "profilemanager.h"
#include "adblockmanager.h"
#include "restoremanager.h"
#include "browsinglibrary.h"
#include "downloadmanager.h"
@ -48,6 +47,7 @@
#include "scripts.h"
#include "sessionmanager.h"
#include "closedwindowsmanager.h"
#include "adblock/adblockplugin.h"
#include <QWebEngineSettings>
#include <QDesktopServices>
@ -310,6 +310,7 @@ MainApplication::MainApplication(int &argc, char** argv)
m_plugins = new PluginProxy;
m_autoFill = new AutoFill(this);
new AdBlockPlugin(this);
if (!noAddons)
m_plugins->loadPlugins();
@ -795,7 +796,6 @@ void MainApplication::saveSettings()
m_networkManager->shutdown();
qzSettings->saveSettings();
AdBlockManager::instance()->save();
QFile::remove(DataPaths::currentProfilePath() + QLatin1String("/WebpageIcons.db"));
sessionManager()->saveSettings();

View File

@ -1,7 +1,5 @@
<RCC>
<qresource prefix="/">
<file>html/adblock_big.png</file>
<file>html/adblock.html</file>
<file>html/about.html</file>
<file>html/copyright</file>
<file>html/reportbug.html</file>

View File

@ -18,11 +18,9 @@
<file>icons/other/login.png</file>
<file>icons/preferences/applications-graphics.png</file>
<file>icons/preferences/document-properties.png</file>
<file>icons/other/adblock.png</file>
<file>icons/other/download.svg</file>
<file>icons/other/bighistory.svg</file>
<file>icons/other/bighistory-selected.svg</file>
<file>icons/other/adblock-disabled.png</file>
<file>icons/menu/search-icon.svg</file>
<file>icons/browsers/firefox.png</file>
<file>icons/browsers/firefox@2x.png</file>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -476,9 +476,9 @@ QString FalkonSchemeReply::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("%IMAGE%"), QLatin1String("qrc:html/adblock_big.png"));
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);
}

View File

@ -28,7 +28,6 @@
#include "autofill.h"
#include "popupwebview.h"
#include "popupwindow.h"
#include "adblockmanager.h"
#include "iconprovider.h"
#include "qzsettings.h"
#include "useragentmanager.h"
@ -37,9 +36,9 @@
#include "html5permissions/html5permissionsmanager.h"
#include "javascript/externaljsobject.h"
#include "tabwidget.h"
#include "scripts.h"
#include "networkmanager.h"
#include "webhittestresult.h"
#include "adblock/adblockmanager.h"
#ifdef NONBLOCK_JS_DIALOGS
#include "ui_jsconfirm.h"
@ -232,9 +231,6 @@ void WebPage::finished()
m_fileWatcher->removePaths(m_fileWatcher->files());
}
// AdBlock
cleanBlockedObjects();
// AutoFill
m_passwordEntries = mApp->autoFill()->completePage(this, url());
}
@ -422,24 +418,6 @@ QVector<PasswordEntry> WebPage::autoFillData() const
return m_passwordEntries;
}
void WebPage::cleanBlockedObjects()
{
AdBlockManager* manager = AdBlockManager::instance();
if (!manager->isEnabled()) {
return;
}
// Apply global element hiding rules
const QString elementHiding = manager->elementHidingRules(url());
if (!elementHiding.isEmpty())
runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld);
// Apply domain-specific element hiding rules
const QString siteElementHiding = manager->elementHidingRulesForDomain(url());
if (!siteElementHiding.isEmpty())
runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld);
}
bool WebPage::javaScriptPrompt(const QUrl &securityOrigin, const QString &msg, const QString &defaultValue, QString* result)
{
Q_UNUSED(securityOrigin)

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -77,7 +77,6 @@ protected slots:
void finished();
private slots:
void cleanBlockedObjects();
void urlChanged(const QUrl &url);
void watchedFileChanged(const QString &file);
void windowCloseRequested();