1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

ExternalJsObject: Add support for registering extra objects

Extra objects will be available as window.external.extra.objectName
This commit is contained in:
David Rosca 2018-01-03 13:19:56 +01:00
parent 87527762f8
commit 6a5b7d30e3
4 changed files with 46 additions and 6 deletions

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2015-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2015-2018 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
@ -48,7 +48,14 @@ QString Scripts::setupWebChannel()
"function registerWebChannel() {"
" try {"
" new QWebChannel(qt.webChannelTransport, function(channel) {"
" registerExternal(channel.objects.qz_object);"
" var external = channel.objects.qz_object;"
" external.extra = {};"
" for (var key in channel.objects) {"
" if (key != 'qz_object' && key.startsWith('qz_')) {"
" external.extra[key.substr(3)] = channel.objects[key];"
" }"
" }"
" registerExternal(external);"
" });"
" } catch (e) {"
" setTimeout(registerWebChannel, 100);"

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
* Copyright (C) 2014-2018 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
@ -24,6 +24,10 @@
#include "autofilljsobject.h"
#include "restoremanager.h"
#include <QWebChannel>
static QHash<QString, QObject*> s_extraObjects;
ExternalJsObject::ExternalJsObject(WebPage *page)
: QObject(page)
, m_page(page)
@ -36,6 +40,28 @@ WebPage *ExternalJsObject::page() const
return m_page;
}
// static
void ExternalJsObject::setupWebChannel(QWebChannel *webChannel, WebPage *page)
{
webChannel->registerObject(QSL("qz_object"), new ExternalJsObject(page));
for (auto it = s_extraObjects.constBegin(); it != s_extraObjects.constEnd(); ++it) {
webChannel->registerObject(QSL("qz_") + it.key(), it.value());
}
}
// static
void ExternalJsObject::registerExtraObject(const QString &id, QObject *object)
{
s_extraObjects[id] = object;
}
// static
void ExternalJsObject::unregisterExtraObject(const QString &id)
{
s_extraObjects.remove(id);
}
void ExternalJsObject::AddSearchProvider(const QString &engineUrl)
{
mApp->searchEnginesManager()->addEngine(QUrl(engineUrl));

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
* Copyright (C) 2014-2018 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
@ -23,6 +23,8 @@
class WebPage;
class AutoFillJsObject;
class QWebChannel;
class ExternalJsObject : public QObject
{
Q_OBJECT
@ -35,6 +37,11 @@ public:
WebPage *page() const;
static void setupWebChannel(QWebChannel *webChannel, WebPage *page);
static void registerExtraObject(const QString &id, QObject *object);
static void unregisterExtraObject(const QString &id);
public slots:
void AddSearchProvider(const QString &engineUrl);
int IsSearchProviderInstalled(const QString &engineURL);

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 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
@ -73,7 +73,7 @@ WebPage::WebPage(QObject* parent)
, m_secureStatus(false)
{
QWebChannel *channel = new QWebChannel(this);
channel->registerObject(QSL("qz_object"), new ExternalJsObject(this));
ExternalJsObject::setupWebChannel(channel, this);
setWebChannel(channel);
connect(this, &QWebEnginePage::loadProgress, this, &WebPage::progress);