Skip to content

Commit 361a873

Browse files
committed
Added QWebChannel
1 parent 431611b commit 361a873

11 files changed

Lines changed: 195 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ add_library(${PLUGIN_ADDON_NAME} SHARED
2222
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineView/nwebengineview.hpp"
2323
"${PROJECT_SOURCE_DIR}/src/cpp/QWebEngineSettings/qwebenginesettings_wrap.cpp"
2424
"${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"
2527
)
2628

2729
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+
};

src/cpp/QWebEnginePage/qwebenginepage_wrap.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "qwebenginepage_wrap.h"
22

33
#include "Extras/Utils/nutils.h"
4+
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"
45

56
Napi::FunctionReference QWebEnginePageWrap::constructor;
67

@@ -9,7 +10,9 @@ Napi::Object QWebEnginePageWrap::init(Napi::Env env, Napi::Object exports) {
910
char CLASSNAME[] = "QWebEnginePage";
1011
Napi::Function func = DefineClass(
1112
env, CLASSNAME,
12-
{InstanceMethod("runJavaScript", &QWebEnginePageWrap::runJavaScript),
13+
{InstanceMethod("setWebChannel", &QWebEnginePageWrap::setWebChannel),
14+
InstanceMethod("webChannel", &QWebEnginePageWrap::webChannel),
15+
InstanceMethod("runJavaScript", &QWebEnginePageWrap::runJavaScript),
1316
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QWebEnginePageWrap)});
1417
constructor = Napi::Persistent(func);
1518
exports.Set(CLASSNAME, func);
@@ -43,3 +46,24 @@ Napi::Value QWebEnginePageWrap::runJavaScript(const Napi::CallbackInfo& info) {
4346
this->instance->runJavaScript(QString::fromUtf8(script.c_str()));
4447
return env.Null();
4548
}
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+
}

src/cpp/QWebEnginePage/qwebenginepage_wrap.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ class QWebEnginePageWrap : public Napi::ObjectWrap<QWebEnginePageWrap> {
1717
QWebEnginePage* getInternalInstance();
1818
// Wrapped methods
1919
Napi::Value runJavaScript(const Napi::CallbackInfo& info);
20+
Napi::Value setWebChannel(const Napi::CallbackInfo& info);
21+
Napi::Value webChannel(const Napi::CallbackInfo& info);
2022
};

src/cpp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include "src/cpp/QWebEngineSettings/qwebenginesettings_wrap.h"
44
#include "src/cpp/QWebEnginePage/qwebenginepage_wrap.h"
55
#include "src/cpp/QWebEngineView/qwebengineview_wrap.h"
6+
#include "src/cpp/QWebChannel/qwebchannel_wrap.h"
67

78
Napi::Object Main(Napi::Env env, Napi::Object exports) {
89
QWebEngineViewWrap::init(env, exports);
910
QWebEngineSettingsWrap::init(env, exports);
1011
QWebEnginePageWrap::init(env, exports);
12+
QWebChannelWrap::init(env, exports);
1113
return exports;
1214
}
1315

src/demo.ts

Lines changed: 6 additions & 3 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,18 +11,21 @@ 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
});
2020
webview.addEventListener("loadFinished", () => {
21-
const js = `document.querySelector('input[name=q]').value = 'nodegui';`;
21+
const js = `alert('nodeui');`;
2222
const page = webview.page();
2323
page.runJavaScript(js);
2424
});
2525

26+
const channel = new QWebChannel();
27+
webview.page().setWebChannel(channel);
28+
2629
webview.show();
2730
(global as any).wv = webview;
2831

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import "../build/Release/nodegui_plugin_webview_init.node";
33

44
export { QWebEngineView, QWebEngineViewSignals } from "./lib/QWebEngineView";
5+
export { QWebChannel } from "./lib/QWebChannel";
56
export {
67
QWebEngineSettings,
78
UnknownUrlSchemePolicy

src/lib/QWebChannel.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
QObject,
3+
Component,
4+
NativeElement,
5+
checkIfNativeElement,
6+
} from "@nodegui/nodegui";
7+
import addon from "./utils/addon";
8+
9+
export class QWebChannel extends Component {
10+
native: NativeElement;
11+
constructor(arg?: NativeElement | QObject) {
12+
super();
13+
if (checkIfNativeElement(arg)) {
14+
this.native = arg as NativeElement;
15+
} else if (arg instanceof QObject) {
16+
this.native = new addon.QWebChannel(arg.native);
17+
} else if (!arg) {
18+
this.native = new addon.QWebChannel(new QObject().native);
19+
} else {
20+
throw new Error(
21+
"QWebChannel cannot be initialised this way. Use webengineview.settings()"
22+
);
23+
}
24+
}
25+
registerObject(name: string, obj: QObject): void {
26+
this.native.registerObject(name, obj.native);
27+
}
28+
29+
deregisterObject(obj: QObject): void {
30+
this.native.deregisterObject(obj.native);
31+
}
32+
}

0 commit comments

Comments
 (0)