Falkon Develop
Cross-platform Qt-based web browser
adblockplugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18
19#include "adblockplugin.h"
20#include "adblockmanager.h"
21#include "adblockicon.h"
22
23#include "scripts.h"
24#include "webpage.h"
25#include "pluginproxy.h"
26#include "browserwindow.h"
27#include "navigationbar.h"
28#include "mainapplication.h"
29#include "statusbar.h"
30#include "desktopfile.h"
31
33 : QObject()
34{
35}
36
37void AdBlockPlugin::init(InitState state, const QString &settingsPath)
38{
39 Q_UNUSED(settingsPath)
40
41 connect(mApp, &MainApplication::aboutToQuit, AdBlockManager::instance(), &AdBlockManager::save);
42 connect(mApp->plugins(), &PluginProxy::webPageCreated, this, &AdBlockPlugin::webPageCreated);
43 connect(mApp->plugins(), &PluginProxy::webPageDeleted, this, &AdBlockPlugin::webPageDeleted);
44 connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &AdBlockPlugin::mainWindowCreated);
45 connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, &AdBlockPlugin::mainWindowDeleted);
46
47 if (state == LateInitState) {
48 const auto windows = mApp->windows();
49 for (BrowserWindow *window : windows) {
50 mainWindowCreated(window);
51 }
52 }
53}
54
56{
57 const auto windows = mApp->windows();
58 for (BrowserWindow *window : windows) {
59 mainWindowDeleted(window);
60 }
61}
62
64{
65 return true;
66}
67
68void AdBlockPlugin::showSettings(QWidget *parent)
69{
71}
72
73void AdBlockPlugin::webPageCreated(WebPage *page)
74{
75 connect(page, &WebPage::loadFinished, this, [=]() {
77 if (!manager->isEnabled()) {
78 return;
79 }
80 // Apply global element hiding rules
81 const QString elementHiding = manager->elementHidingRules(page->url());
82 if (!elementHiding.isEmpty()) {
83 page->runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld);
84 }
85 // Apply domain-specific element hiding rules
86 const QString siteElementHiding = manager->elementHidingRulesForDomain(page->url());
87 if (!siteElementHiding.isEmpty()) {
88 page->runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld);
89 }
90 });
91}
92
93void AdBlockPlugin::webPageDeleted(WebPage *page)
94{
96}
97
98void AdBlockPlugin::mainWindowCreated(BrowserWindow *window)
99{
100 auto *icon = new AdBlockIcon(window);
101 m_icons[window] = icon;
102 window->statusBar()->addButton(icon);
103 window->navigationBar()->addToolButton(icon);
104}
105
106void AdBlockPlugin::mainWindowDeleted(BrowserWindow *window)
107{
108 AdBlockIcon *icon = m_icons.take(window);
109 window->statusBar()->removeButton(icon);
110 window->navigationBar()->removeToolButton(icon);
111 delete icon;
112}
113
114bool AdBlockPlugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
115{
116 Q_UNUSED(type)
117
119 if (isMainFrame) {
120 manager->clearBlockedRequestsForUrl(page->url());
121 }
122 if (url.scheme() == QL1S("abp") && AdBlockManager::instance()->addSubscriptionFromUrl(url)) {
123 return false;
124 }
125 return true;
126}
static AdBlockManager * instance()
QString elementHidingRules(const QUrl &url) const
void clearBlockedRequestsForUrl(const QUrl &url)
AdBlockDialog * showDialog(QWidget *parent=nullptr)
QString elementHidingRulesForDomain(const QUrl &url) const
bool isEnabled() const
void showSettings(QWidget *parent=nullptr) override
void unload() override
void init(InitState state, const QString &settingsPath) override
bool testPlugin() override
NavigationBar * navigationBar() const
StatusBar * statusBar() const
void removeToolButton(AbstractButtonInterface *button)
void addToolButton(AbstractButtonInterface *button)
void webPageCreated(WebPage *page)
void mainWindowDeleted(BrowserWindow *window)
void mainWindowCreated(BrowserWindow *window)
void webPageDeleted(WebPage *page)
static QString setCss(const QString &css)
Definition: scripts.cpp:200
void addButton(AbstractButtonInterface *button)
Definition: statusbar.cpp:217
void removeButton(AbstractButtonInterface *button)
Definition: statusbar.cpp:235
@ SafeJsWorld
Definition: webpage.h:45
#define mApp
State state
#define QL1S(x)
Definition: qzcommon.h:44