From 6a5b7d30e37a8578d8b3b6b07c5ddea89d009f17 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 3 Jan 2018 13:19:56 +0100 Subject: [PATCH] ExternalJsObject: Add support for registering extra objects Extra objects will be available as window.external.extra.objectName --- src/lib/tools/scripts.cpp | 11 ++++++-- .../webengine/javascript/externaljsobject.cpp | 28 ++++++++++++++++++- .../webengine/javascript/externaljsobject.h | 9 +++++- src/lib/webengine/webpage.cpp | 4 +-- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/lib/tools/scripts.cpp b/src/lib/tools/scripts.cpp index 6d51f1be5..7c2fd5dcf 100644 --- a/src/lib/tools/scripts.cpp +++ b/src/lib/tools/scripts.cpp @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2015-2017 David Rosca +* Copyright (C) 2015-2018 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 @@ -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);" diff --git a/src/lib/webengine/javascript/externaljsobject.cpp b/src/lib/webengine/javascript/externaljsobject.cpp index 52cda127f..35d1e7d93 100644 --- a/src/lib/webengine/javascript/externaljsobject.cpp +++ b/src/lib/webengine/javascript/externaljsobject.cpp @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2014 David Rosca +* Copyright (C) 2014-2018 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 @@ -24,6 +24,10 @@ #include "autofilljsobject.h" #include "restoremanager.h" +#include + +static QHash 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)); diff --git a/src/lib/webengine/javascript/externaljsobject.h b/src/lib/webengine/javascript/externaljsobject.h index 9f01bc585..031282801 100644 --- a/src/lib/webengine/javascript/externaljsobject.h +++ b/src/lib/webengine/javascript/externaljsobject.h @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2014 David Rosca +* Copyright (C) 2014-2018 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 @@ -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); diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 54cb5fa98..5bbd1379e 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2017 David Rosca +* Copyright (C) 2010-2018 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 @@ -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);