Skip to content

Commit 0f3d6fc

Browse files
authored
Merge pull request #8 from jardenliu/master
added qt web method 'runJavaScript' && QWebChannel
2 parents a7f7af4 + 361a873 commit 0f3d6fc

14 files changed

Lines changed: 303 additions & 2 deletions

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ add_library(${PLUGIN_ADDON_NAME} SHARED
2121
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineView/qwebengineview_wrap.cpp"
2222
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineView/nwebengineview.hpp"
2323
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineSettings/qwebenginesettings_wrap.cpp"
24+
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEnginePage/qwebenginepage_wrap.cpp"
25+
"${PROJECT_SOURCE_DIR}/src/cpp/QWebChannel/qwebchannel_wrap.cpp"
26+
"${PROJECT_SOURCE_DIR}/src/cpp/QWebChannel/nwebchannel.hpp"
2427
)
2528

2629
AddQtWebSupport(${PLUGIN_ADDON_NAME})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <nodegui/core/NodeWidget/nodewidget.h>
4+
5+
#include <QtWebChannel/QWebChannel>
6+
7+
#include "nodegui/core/Events/eventwidget.h"
8+
#include "nodegui/core/Events/eventwidget_macro.h"
9+
10+
class DLL_EXPORT NWebChannel : public QWebChannel, public EventWidget {
11+
Q_OBJECT
12+
EVENTWIDGET_IMPLEMENTATIONS(QWebChannel)
13+
public:
14+
using QWebChannel::QWebChannel;
15+
16+
void connectSignalsToEventEmitter() { }
17+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "qwebchannel_wrap.h"
2+
3+
#include <QDebug>
4+
#include <QObject>
5+
#include <QWebChannel>
6+
7+
#include "nodegui/Extras/Utils/nutils.h"
8+
#include "nodegui/QtCore/QObject/qobject_wrap.h"
9+
10+
Napi::FunctionReference QWebChannelWrap::constructor;
11+
12+
Napi::Object QWebChannelWrap::init(Napi::Env env, Napi::Object exports) {
13+
Napi::HandleScope scope(env);
14+
char CLASSNAME[] = "QWebChannel";
15+
Napi::Function func = DefineClass(
16+
env, CLASSNAME,
17+
{InstanceMethod("registerObject", &QWebChannelWrap::registerObject),
18+
InstanceMethod("deregisterObject", &QWebChannelWrap::deregisterObject),
19+
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebChannelWrap)});
20+
constructor = Napi::Persistent(func);
21+
exports.Set(CLASSNAME, func);
22+
return exports;
23+
}
24+
25+
QWebChannelWrap::QWebChannelWrap(const Napi::CallbackInfo& info)
26+
: Napi::ObjectWrap<QWebChannelWrap>(info) {
27+
Napi::Env env = info.Env();
28+
Napi::HandleScope scope(env);
29+
30+
if (info[0].IsExternal()) {
31+
this->instance = info[0].As<Napi::External<NWebChannel>>().Data();
32+
} else if (info.Length() == 1) {
33+
Napi::Object parentObject = info[0].As<Napi::Object>();
34+
QObjectWrap* parentWrap =
35+
Napi::ObjectWrap<QObjectWrap>::Unwrap(parentObject);
36+
this->instance = new NWebChannel(parentWrap->getInternalInstance());
37+
} else if (info.Length() == 0) {
38+
this->instance = new NWebChannel();
39+
} else {
40+
Napi::TypeError::New(env, "Incorrect initialization of QWebChannelWrap")
41+
.ThrowAsJavaScriptException();
42+
}
43+
this->rawData = extrautils::configureComponent(this->getInternalInstance());
44+
}
45+
46+
QWebChannelWrap::~QWebChannelWrap() { extrautils::safeDelete(this->instance); }
47+
48+
NWebChannel* QWebChannelWrap::getInternalInstance() { return this->instance; }
49+
50+
Napi::Value QWebChannelWrap::registerObject(const Napi::CallbackInfo& info) {
51+
Napi::Env env = info.Env();
52+
Napi::HandleScope scope(env);
53+
54+
Napi::String napiName = info[0].As<Napi::String>();
55+
std::string name = napiName.Utf8Value();
56+
57+
Napi::Object napiObject = info[1].As<Napi::Object>();
58+
QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject);
59+
60+
this->instance->registerObject(QString::fromUtf8(name.c_str()),
61+
objectWrap->getInternalInstance());
62+
return env.Null();
63+
}
64+
65+
Napi::Value QWebChannelWrap::deregisterObject(const Napi::CallbackInfo& info) {
66+
Napi::Env env = info.Env();
67+
Napi::HandleScope scope(env);
68+
69+
Napi::Object napiObject = info[0].As<Napi::Object>();
70+
QObjectWrap* objectWrap = Napi::ObjectWrap<QObjectWrap>::Unwrap(napiObject);
71+
72+
this->instance->deregisterObject(objectWrap->getInternalInstance());
73+
return env.Null();
74+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
#include <nodegui/Extras/Export/export.h>
5+
6+
#include <QPointer>
7+
8+
#include "QtCore/QObject/qobject_macro.h"
9+
#include "nwebchannel.hpp"
10+
11+
class DLL_EXPORT QWebChannelWrap : public Napi::ObjectWrap<QWebChannelWrap> {
12+
QOBJECT_WRAPPED_METHODS_DECLARATION
13+
private:
14+
QPointer<NWebChannel> instance;
15+
16+
public:
17+
static Napi::FunctionReference constructor;
18+
static Napi::Object init(Napi::Env env, Napi::Object exports);
19+
QWebChannelWrap(const Napi::CallbackInfo& info);
20+
~QWebChannelWrap();
21+
NWebChannel* getInternalInstance();
22+
// Wrapped methods
23+
Napi::Value registerObject(const Napi::CallbackInfo& info);
24+
Napi::Value deregisterObject(const Napi::CallbackInfo& info);
25+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "qwebenginepage_wrap.h"
2+
3+
#include "Extras/Utils/nutils.h"
4+
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"
5+
6+
Napi::FunctionReference QWebEnginePageWrap::constructor;
7+
8+
Napi::Object QWebEnginePageWrap::init(Napi::Env env, Napi::Object exports) {
9+
Napi::HandleScope scope(env);
10+
char CLASSNAME[] = "QWebEnginePage";
11+
Napi::Function func = DefineClass(
12+
env, CLASSNAME,
13+
{InstanceMethod("setWebChannel", &QWebEnginePageWrap::setWebChannel),
14+
InstanceMethod("webChannel", &QWebEnginePageWrap::webChannel),
15+
InstanceMethod("runJavaScript", &QWebEnginePageWrap::runJavaScript),
16+
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebEnginePageWrap)});
17+
constructor = Napi::Persistent(func);
18+
exports.Set(CLASSNAME, func);
19+
return exports;
20+
}
21+
22+
QWebEnginePageWrap::QWebEnginePageWrap(const Napi::CallbackInfo& info)
23+
: Napi::ObjectWrap<QWebEnginePageWrap>(info) {
24+
Napi::Env env = info.Env();
25+
Napi::HandleScope scope(env);
26+
if (info[0].IsExternal()) {
27+
this->instance = info[0].As<Napi::External<QWebEnginePage>>().Data();
28+
} else {
29+
Napi::TypeError::New(env, "Incorrect initialization of QWebEnginePageWrap")
30+
.ThrowAsJavaScriptException();
31+
}
32+
this->rawData = extrautils::configureComponent(this->getInternalInstance());
33+
}
34+
35+
QWebEnginePage* QWebEnginePageWrap::getInternalInstance() {
36+
return this->instance;
37+
}
38+
39+
Napi::Value QWebEnginePageWrap::runJavaScript(const Napi::CallbackInfo& info) {
40+
Napi::Env env = info.Env();
41+
Napi::HandleScope scope(env);
42+
43+
Napi::String napiScript = info[0].As<Napi::String>();
44+
std::string script = napiScript.Utf8Value();
45+
46+
this->instance->runJavaScript(QString::fromUtf8(script.c_str()));
47+
return env.Null();
48+
}
49+
50+
Napi::Value QWebEnginePageWrap::setWebChannel(const Napi::CallbackInfo& info) {
51+
Napi::Env env = info.Env();
52+
Napi::HandleScope scope(env);
53+
54+
Napi::Object channel = info[0].As<Napi::Object>();
55+
QWebChannelWrap* channelWrap =
56+
Napi::ObjectWrap<QWebChannelWrap>::Unwrap(channel);
57+
58+
this->instance->setWebChannel(channelWrap->getInternalInstance());
59+
return env.Null();
60+
}
61+
62+
Napi::Value QWebEnginePageWrap::webChannel(const Napi::CallbackInfo& info) {
63+
Napi::Env env = info.Env();
64+
Napi::HandleScope scope(env);
65+
66+
QWebChannel* webChannel = this->instance->webChannel();
67+
return QWebChannelWrap::constructor.New(
68+
{Napi::External<QWebChannel>::New(env, webChannel)});
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
#include <nodegui/core/Component/component_macro.h>
5+
#include <QWebEnginePage>
6+
7+
class QWebEnginePageWrap : public Napi::ObjectWrap<QWebEnginePageWrap> {
8+
COMPONENT_WRAPPED_METHODS_DECLARATION
9+
10+
private:
11+
QWebEnginePage* instance;
12+
13+
public:
14+
static Napi::FunctionReference constructor;
15+
static Napi::Object init(Napi::Env env, Napi::Object exports);
16+
QWebEnginePageWrap(const Napi::CallbackInfo& info);
17+
QWebEnginePage* getInternalInstance();
18+
// Wrapped methods
19+
Napi::Value runJavaScript(const Napi::CallbackInfo& info);
20+
Napi::Value setWebChannel(const Napi::CallbackInfo& info);
21+
Napi::Value webChannel(const Napi::CallbackInfo& info);
22+
};

src/cpp/QWebEngineView/qwebengineview_wrap.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "qwebengineview_wrap.h"
22

33
#include "src/cpp/QWebEngineSettings/qwebenginesettings_wrap.h"
4+
#include "src/cpp/QWebEnginePage/qwebenginepage_wrap.h"
45
#include <QDebug>
56
#include <QWidget>
67
#include "nodegui/Extras/Utils/nutils.h"
@@ -14,6 +15,7 @@ Napi::Object QWebEngineViewWrap::init(Napi::Env env, Napi::Object exports) {
1415
Napi::Function func =
1516
DefineClass(env, CLASSNAME,
1617
{InstanceMethod("settings", &QWebEngineViewWrap::settings),
18+
InstanceMethod("page", &QWebEngineViewWrap::page),
1719
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QWebEngineViewWrap)});
1820
constructor = Napi::Persistent(func);
1921
exports.Set(CLASSNAME, func);
@@ -56,4 +58,12 @@ Napi::Value QWebEngineViewWrap::settings(const Napi::CallbackInfo& info) {
5658
QWebEngineSettings* settings = this->instance->settings();
5759
return QWebEngineSettingsWrap::constructor.New(
5860
{Napi::External<QWebEngineSettings>::New(env, settings)});
61+
}
62+
63+
Napi::Value QWebEngineViewWrap::page(const Napi::CallbackInfo& info) {
64+
Napi::Env env = info.Env();
65+
Napi::HandleScope scope(env);
66+
QWebEnginePage* page = this->instance->page();
67+
return QWebEnginePageWrap::constructor.New(
68+
{Napi::External<QWebEnginePage>::New(env, page)});
5969
}

src/cpp/QWebEngineView/qwebengineview_wrap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ class QWebEngineViewWrap : public Napi::ObjectWrap<QWebEngineViewWrap> {
2020
static Napi::FunctionReference constructor;
2121
// wrapped methods
2222
Napi::Value settings(const Napi::CallbackInfo& info);
23+
Napi::Value page(const Napi::CallbackInfo& info);
2324
};

src/cpp/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include <napi.h>
22

33
#include "src/cpp/QWebEngineSettings/qwebenginesettings_wrap.h"
4+
#include "src/cpp/QWebEnginePage/qwebenginepage_wrap.h"
45
#include "src/cpp/QWebEngineView/qwebengineview_wrap.h"
6+
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"
57

68
Napi::Object Main(Napi::Env env, Napi::Object exports) {
79
QWebEngineViewWrap::init(env, exports);
810
QWebEngineSettingsWrap::init(env, exports);
11+
QWebEnginePageWrap::init(env, exports);
12+
QWebChannelWrap::init(env, exports);
913
return exports;
1014
}
1115

src/demo.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QWebEngineView } from "./index";
1+
import { QWebEngineView, QWebChannel } from "./index";
22
// import {
33
// QMainWindow,
44
// QWidget,
@@ -11,12 +11,20 @@ import { QWebEngineView } from "./index";
1111
const webview = new QWebEngineView();
1212
webview.setInlineStyle("align-self:'stretch';");
1313
webview.load("http://google.com");
14-
webview.addEventListener("urlChanged", url => {
14+
webview.addEventListener("urlChanged", (url) => {
1515
console.log("changed to", url);
1616
});
1717
webview.addEventListener("selectionChanged", () => {
1818
console.log("selection", webview.property("selectedText").toString());
1919
});
20+
webview.addEventListener("loadFinished", () => {
21+
const js = `alert('nodeui');`;
22+
const page = webview.page();
23+
page.runJavaScript(js);
24+
});
25+
26+
const channel = new QWebChannel();
27+
webview.page().setWebChannel(channel);
2028

2129
webview.show();
2230
(global as any).wv = webview;

0 commit comments

Comments
 (0)