Falkon Develop
Cross-platform Qt-based web browser
gm_plugin.cpp
Go to the documentation of this file.
1/* ============================================================
2* GreaseMonkey plugin for Falkon
3* Copyright (C) 2012-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#include "gm_plugin.h"
19#include "gm_manager.h"
20#include "browserwindow.h"
21#include "webpage.h"
22#include "pluginproxy.h"
23#include "mainapplication.h"
24#include "tabwidget.h"
25#include "webtab.h"
26#include "../config.h"
27
28#include <QtWebEngineWidgetsVersion>
29
31 : QObject()
32 , m_manager(nullptr)
33{
34}
35
36void GM_Plugin::init(InitState state, const QString &settingsPath)
37{
38 m_manager = new GM_Manager(settingsPath, this);
39
40 connect(mApp->plugins(), &PluginProxy::mainWindowCreated, m_manager, &GM_Manager::mainWindowCreated);
41 connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, m_manager, &GM_Manager::mainWindowDeleted);
42
43 // Make sure userscripts works also with already created WebPages
44 if (state == LateInitState) {
45 const auto windows = mApp->windows();
46 for (BrowserWindow *window : windows) {
47 m_manager->mainWindowCreated(window);
48 }
49 }
50}
51
53{
54 m_manager->unloadPlugin();
55 delete m_manager;
56}
57
59{
60 // Require the version that the plugin was built with
61 return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
62}
63
64void GM_Plugin::showSettings(QWidget* parent)
65{
66 m_manager->showSettings(parent);
67}
68
69bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
70{
71 Q_UNUSED(page)
72 Q_UNUSED(isMainFrame)
73
74 bool navigationType = type == QWebEnginePage::NavigationTypeLinkClicked || type == QWebEnginePage::NavigationTypeRedirect;
75
76 if (navigationType && url.toString().endsWith(QLatin1String(".user.js"))) {
77 m_manager->downloadScript(url);
78 return false;
79 }
80 return true;
81}
void downloadScript(const QUrl &url)
Definition: gm_manager.cpp:65
void mainWindowDeleted(BrowserWindow *window)
Definition: gm_manager.cpp:306
void showSettings(QWidget *parent)
Definition: gm_manager.cpp:55
void mainWindowCreated(BrowserWindow *window)
Definition: gm_manager.cpp:298
void unloadPlugin()
Definition: gm_manager.cpp:136
bool testPlugin() override
Definition: gm_plugin.cpp:58
void unload() override
Definition: gm_plugin.cpp:52
bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override
Definition: gm_plugin.cpp:69
void showSettings(QWidget *parent=nullptr) override
Definition: gm_plugin.cpp:64
void init(InitState state, const QString &settingsPath) override
Definition: gm_plugin.cpp:36
void mainWindowDeleted(BrowserWindow *window)
void mainWindowCreated(BrowserWindow *window)
#define mApp
FALKON_EXPORT const char * VERSION
Definition: qzcommon.cpp:26
State state