File indexing completed on 2024-05-19 04:59:16

0001 /* ============================================================
0002 * GreaseMonkey plugin for Falkon
0003 * Copyright (C) 2012-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "gm_plugin.h"
0019 #include "gm_manager.h"
0020 #include "browserwindow.h"
0021 #include "webpage.h"
0022 #include "pluginproxy.h"
0023 #include "mainapplication.h"
0024 #include "tabwidget.h"
0025 #include "webtab.h"
0026 #include "../config.h"
0027 
0028 #include <QtWebEngineWidgetsVersion>
0029 
0030 GM_Plugin::GM_Plugin()
0031     : QObject()
0032     , m_manager(nullptr)
0033 {
0034 }
0035 
0036 void GM_Plugin::init(InitState state, const QString &settingsPath)
0037 {
0038     m_manager = new GM_Manager(settingsPath, this);
0039 
0040     connect(mApp->plugins(), &PluginProxy::mainWindowCreated, m_manager, &GM_Manager::mainWindowCreated);
0041     connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, m_manager, &GM_Manager::mainWindowDeleted);
0042 
0043     // Make sure userscripts works also with already created WebPages
0044     if (state == LateInitState) {
0045         const auto windows = mApp->windows();
0046         for (BrowserWindow *window : windows) {
0047             m_manager->mainWindowCreated(window);
0048         }
0049     }
0050 }
0051 
0052 void GM_Plugin::unload()
0053 {
0054     m_manager->unloadPlugin();
0055     delete m_manager;
0056 }
0057 
0058 bool GM_Plugin::testPlugin()
0059 {
0060     // Require the version that the plugin was built with
0061     return (QString::fromLatin1(Qz::VERSION) == QLatin1String(FALKON_VERSION));
0062 }
0063 
0064 void GM_Plugin::showSettings(QWidget* parent)
0065 {
0066     m_manager->showSettings(parent);
0067 }
0068 
0069 bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
0070 {
0071     Q_UNUSED(page)
0072     Q_UNUSED(isMainFrame)
0073 
0074     bool navigationType = type == QWebEnginePage::NavigationTypeLinkClicked || type == QWebEnginePage::NavigationTypeRedirect;
0075 
0076     if (navigationType && url.toString().endsWith(QLatin1String(".user.js"))) {
0077         m_manager->downloadScript(url);
0078         return false;
0079     }
0080     return true;
0081 }