1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

GreaseMonkey: Use url interceptor for downloading scripts

This commit is contained in:
David Rosca 2015-10-08 23:32:18 +02:00
parent 03399c7b2c
commit d0f662d599
7 changed files with 99 additions and 22 deletions

View File

@ -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 \

View File

@ -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 <QTimer>
@ -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")

View File

@ -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<GM_Settings> m_settings;
GM_UrlInterceptor *m_interceptor;
QStringList m_disabledScripts;
//GM_JSObject* m_jsObject;

View File

@ -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

View File

@ -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;
};

View File

@ -0,0 +1,37 @@
/* ============================================================
* QupZilla - QtWebEngine based browser
* Copyright (C) 2015 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#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;
}

View File

@ -0,0 +1,38 @@
/* ============================================================
* QupZilla - QtWebEngine based browser
* Copyright (C) 2015 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#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