From 44913c32844b7f78945fb3840f818daad6fe040d Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sat, 21 Jan 2017 21:04:50 +0100 Subject: [PATCH] GreaseMonkey: Use acceptNavigationRequest for handling userscript downloads --- src/plugins/GreaseMonkey/GreaseMonkey.pro | 2 - src/plugins/GreaseMonkey/gm_manager.cpp | 9 ----- src/plugins/GreaseMonkey/gm_manager.h | 5 +-- src/plugins/GreaseMonkey/gm_plugin.cpp | 9 +++++ src/plugins/GreaseMonkey/gm_plugin.h | 4 +- .../GreaseMonkey/gm_urlinterceptor.cpp | 40 ------------------- src/plugins/GreaseMonkey/gm_urlinterceptor.h | 38 ------------------ 7 files changed, 13 insertions(+), 94 deletions(-) delete mode 100644 src/plugins/GreaseMonkey/gm_urlinterceptor.cpp delete mode 100644 src/plugins/GreaseMonkey/gm_urlinterceptor.h diff --git a/src/plugins/GreaseMonkey/GreaseMonkey.pro b/src/plugins/GreaseMonkey/GreaseMonkey.pro index 905c28e43..b2afb5a77 100644 --- a/src/plugins/GreaseMonkey/GreaseMonkey.pro +++ b/src/plugins/GreaseMonkey/GreaseMonkey.pro @@ -16,7 +16,6 @@ SOURCES += gm_plugin.cpp \ settings/gm_settingslistwidget.cpp \ # gm_jsobject.cpp \ gm_icon.cpp \ - gm_urlinterceptor.cpp HEADERS += gm_plugin.h \ gm_manager.h \ @@ -30,7 +29,6 @@ HEADERS += gm_plugin.h \ settings/gm_settingslistwidget.h \ # gm_jsobject.h \ gm_icon.h \ - gm_urlinterceptor.h FORMS += \ gm_addscriptdialog.ui \ diff --git a/src/plugins/GreaseMonkey/gm_manager.cpp b/src/plugins/GreaseMonkey/gm_manager.cpp index 66c263e77..42d4bc617 100644 --- a/src/plugins/GreaseMonkey/gm_manager.cpp +++ b/src/plugins/GreaseMonkey/gm_manager.cpp @@ -19,7 +19,6 @@ #include "gm_script.h" #include "gm_downloader.h" #include "gm_icon.h" -#include "gm_urlinterceptor.h" #include "gm_addscriptdialog.h" #include "settings/gm_settings.h" @@ -40,18 +39,10 @@ GM_Manager::GM_Manager(const QString &sPath, QObject* parent) : QObject(parent) , m_settingsPath(sPath) - , m_interceptor(new GM_UrlInterceptor(this)) { - mApp->networkManager()->installUrlInterceptor(m_interceptor); - QTimer::singleShot(0, this, SLOT(load())); } -GM_Manager::~GM_Manager() -{ - mApp->networkManager()->removeUrlInterceptor(m_interceptor); -} - void GM_Manager::showSettings(QWidget* parent) { if (!m_settings) { diff --git a/src/plugins/GreaseMonkey/gm_manager.h b/src/plugins/GreaseMonkey/gm_manager.h index 644ff799e..e66bd6509 100644 --- a/src/plugins/GreaseMonkey/gm_manager.h +++ b/src/plugins/GreaseMonkey/gm_manager.h @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2013-2014 David Rosca +* Copyright (C) 2013-2017 David Rosca * * 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 @@ -30,14 +30,12 @@ class BrowserWindow; class GM_Script; class GM_Settings; class GM_Icon; -class GM_UrlInterceptor; class GM_Manager : public QObject { Q_OBJECT public: explicit GM_Manager(const QString &sPath, QObject* parent = 0); - ~GM_Manager(); void showSettings(QWidget* parent); void downloadScript(const QUrl &url); @@ -80,7 +78,6 @@ private: QString m_bootstrapScript; QString m_valuesScript; QPointer m_settings; - GM_UrlInterceptor *m_interceptor; QStringList m_disabledScripts; //GM_JSObject* m_jsObject; diff --git a/src/plugins/GreaseMonkey/gm_plugin.cpp b/src/plugins/GreaseMonkey/gm_plugin.cpp index b6e8d528b..119dcb1bb 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.cpp +++ b/src/plugins/GreaseMonkey/gm_plugin.cpp @@ -86,3 +86,12 @@ void GM_Plugin::showSettings(QWidget* parent) { m_manager->showSettings(parent); } + +bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) +{ + if (type == QWebEnginePage::NavigationTypeLinkClicked && url.toString().endsWith(QLatin1String(".user.js"))) { + m_manager->downloadScript(url); + return false; + } + return true; +} diff --git a/src/plugins/GreaseMonkey/gm_plugin.h b/src/plugins/GreaseMonkey/gm_plugin.h index 3289a7156..5a255be71 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.h +++ b/src/plugins/GreaseMonkey/gm_plugin.h @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012-2014 David Rosca +* Copyright (C) 2012-2017 David Rosca * * 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 @@ -40,6 +40,8 @@ public: QTranslator* getTranslator(const QString &locale); void showSettings(QWidget* parent = 0); + bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override; + private: GM_Manager* m_manager; }; diff --git a/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp b/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp deleted file mode 100644 index 833e9c358..000000000 --- a/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* ============================================================ -* QupZilla - QtWebEngine based browser -* Copyright (C) 2015-2016 David Rosca -* -* 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 . -* ============================================================ */ -#include "gm_urlinterceptor.h" -#include "gm_manager.h" - -GM_UrlInterceptor::GM_UrlInterceptor(GM_Manager *manager) - : UrlInterceptor(manager) - , m_manager(manager) -{ -} - -void GM_UrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) -{ - bool xhr = info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeXhr; - bool clickedLink = info.navigationType() == QWebEngineUrlRequestInfo::NavigationTypeLink; - - if (xhr || !clickedLink) - return; - - if (info.requestUrl().toString().endsWith(QLatin1String(".user.js"))) { - m_manager->downloadScript(info.requestUrl()); - info.block(true); - } -} - diff --git a/src/plugins/GreaseMonkey/gm_urlinterceptor.h b/src/plugins/GreaseMonkey/gm_urlinterceptor.h deleted file mode 100644 index 349923bc3..000000000 --- a/src/plugins/GreaseMonkey/gm_urlinterceptor.h +++ /dev/null @@ -1,38 +0,0 @@ -/* ============================================================ -* QupZilla - QtWebEngine based browser -* Copyright (C) 2015 David Rosca -* -* 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 . -* ============================================================ */ - -#ifndef GM_URLINTERCEPTOR_H -#define GM_URLINTERCEPTOR_H - -#include "urlinterceptor.h" - -class GM_Manager; - -class GM_UrlInterceptor : public UrlInterceptor -{ -public: - explicit GM_UrlInterceptor(GM_Manager* manager); - - void interceptRequest(QWebEngineUrlRequestInfo &info); - -private: - GM_Manager *m_manager; - -}; - -#endif // GM_URLINTERCEPTOR_H