diff --git a/src/plugins/GreaseMonkey/GreaseMonkey.pro b/src/plugins/GreaseMonkey/GreaseMonkey.pro index 1a4a76a70..a002ed9dd 100644 --- a/src/plugins/GreaseMonkey/GreaseMonkey.pro +++ b/src/plugins/GreaseMonkey/GreaseMonkey.pro @@ -16,7 +16,8 @@ SOURCES += gm_plugin.cpp \ settings/gm_settingsscriptinfo.cpp \ settings/gm_settingslistwidget.cpp \ # gm_jsobject.cpp \ - gm_icon.cpp + gm_icon.cpp \ + gm_urlinterceptor.cpp HEADERS += gm_plugin.h \ gm_manager.h \ @@ -30,7 +31,8 @@ HEADERS += gm_plugin.h \ settings/gm_settingsscriptinfo.h \ settings/gm_settingslistwidget.h \ # gm_jsobject.h \ - gm_icon.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 4ef8a860f..1f0411b80 100644 --- a/src/plugins/GreaseMonkey/gm_manager.cpp +++ b/src/plugins/GreaseMonkey/gm_manager.cpp @@ -19,12 +19,14 @@ #include "gm_script.h" #include "gm_downloader.h" #include "gm_icon.h" +#include "gm_urlinterceptor.h" #include "settings/gm_settings.h" #include "browserwindow.h" #include "webpage.h" #include "qztools.h" #include "mainapplication.h" +#include "networkmanager.h" #include "desktopnotificationsfactory.h" #include @@ -37,11 +39,18 @@ GM_Manager::GM_Manager(const QString &sPath, QObject* parent) : QObject(parent) , m_settingsPath(sPath) - //, m_jsObject(new GM_JSObject(this)) + , 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) { @@ -54,7 +63,7 @@ void GM_Manager::showSettings(QWidget* parent) void GM_Manager::downloadScript(const QUrl &url) { - new GM_Downloader(url, this); + QMetaObject::invokeMethod(this, "doDownloadScript", Qt::QueuedConnection, Q_ARG(QUrl, url)); } QString GM_Manager::settinsPath() const @@ -232,8 +241,6 @@ void GM_Manager::load() mApp->webProfile()->scripts()->insert(script->webScript()); } } - - //m_jsObject->setSettingsFile(m_settingsPath + QL1S("/extensions.ini")); } void GM_Manager::scriptChanged() @@ -247,6 +254,11 @@ void GM_Manager::scriptChanged() collection->insert(script->webScript()); } +void GM_Manager::doDownloadScript(const QUrl &url) +{ + new GM_Downloader(url, this); +} + bool GM_Manager::canRunOnScheme(const QString &scheme) { return (scheme == QLatin1String("http") || scheme == QLatin1String("https") diff --git a/src/plugins/GreaseMonkey/gm_manager.h b/src/plugins/GreaseMonkey/gm_manager.h index 71c70e239..644ff799e 100644 --- a/src/plugins/GreaseMonkey/gm_manager.h +++ b/src/plugins/GreaseMonkey/gm_manager.h @@ -30,12 +30,14 @@ 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); @@ -71,12 +73,14 @@ public slots: private slots: void load(); void scriptChanged(); + void doDownloadScript(const QUrl &url); private: QString m_settingsPath; 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 88be9379c..f78b8cc07 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.cpp +++ b/src/plugins/GreaseMonkey/gm_plugin.cpp @@ -87,20 +87,6 @@ void GM_Plugin::showSettings(QWidget* parent) m_manager->showSettings(parent); } -bool GM_Plugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) -{ - Q_UNUSED(page) - Q_UNUSED(type) - Q_UNUSED(isMainFrame) - - if (url.toString().endsWith(QL1S(".user.js"))) { - m_manager->downloadScript(url); - return false; - } - - return true; -} - #if QT_VERSION < 0x050000 Q_EXPORT_PLUGIN2(GreaseMonkey, GM_Plugin) #endif diff --git a/src/plugins/GreaseMonkey/gm_plugin.h b/src/plugins/GreaseMonkey/gm_plugin.h index ba89d8ece..7d6066029 100644 --- a/src/plugins/GreaseMonkey/gm_plugin.h +++ b/src/plugins/GreaseMonkey/gm_plugin.h @@ -43,8 +43,6 @@ public: QTranslator* getTranslator(const QString &locale); void showSettings(QWidget* parent = 0); - bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame); - private: GM_Manager* m_manager; }; diff --git a/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp b/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp new file mode 100644 index 000000000..1b7b917ac --- /dev/null +++ b/src/plugins/GreaseMonkey/gm_urlinterceptor.cpp @@ -0,0 +1,37 @@ +/* ============================================================ +* 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 . +* ============================================================ */ +#include "gm_urlinterceptor.h" +#include "gm_manager.h" + +GM_UrlInterceptor::GM_UrlInterceptor(GM_Manager *manager) + : UrlInterceptor(manager) + , m_manager(manager) +{ +} + +bool GM_UrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) +{ + if (info.requestUrl().toString().endsWith(QLatin1String(".user.js"))) { + m_manager->downloadScript(info.requestUrl()); + info.block(true); + return true; + } + + return false; +} + diff --git a/src/plugins/GreaseMonkey/gm_urlinterceptor.h b/src/plugins/GreaseMonkey/gm_urlinterceptor.h new file mode 100644 index 000000000..60a832143 --- /dev/null +++ b/src/plugins/GreaseMonkey/gm_urlinterceptor.h @@ -0,0 +1,38 @@ +/* ============================================================ +* 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); + + bool interceptRequest(QWebEngineUrlRequestInfo &info); + +private: + GM_Manager *m_manager; + +}; + +#endif // GM_URLINTERCEPTOR_H