forked from optimisme/gjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathegWebmsg.js
executable file
·95 lines (75 loc) · 2.71 KB
/
egWebmsg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env jsgtk
/*
JSGtk+ example showing how to build Gtk javascript applications
using WebKit2.WebView, also showing how to send messages from GTK
to WebKit2 and vice versa
Run it with:
jsgtk egWebmsg.js
*/
const
GLib = require('GLib'),
Gtk = require('Gtk'),
WebKit2 = require('WebKit2'),
channel = 'jsgtk'
;
const App = function () {
this.title = 'Example Webmsg';
GLib.setPrgname(this.title);
};
App.prototype.run = function () {
this.application = new Gtk.Application();
this.application.on('activate', this.onActivate.bind(this));
this.application.on('startup', this.onStartup.bind(this));
this.application.run([]);
};
App.prototype.onActivate = function () {
this.window.showAll();
};
App.prototype.onStartup = function() {
this.buildUI();
};
App.prototype.buildUI = function() {
this.window = new Gtk.ApplicationWindow({ application: this.application,
title: this.title,
defaultHeight: 200,
defaultWidth: 200,
windowPosition: Gtk.WindowPosition.CENTER });
try {
this.window.setIconFromFile(__dirname + '/assets/appIcon.png');
} catch (err) {
this.window.setIconName('application-x-executable');
}
this.window.add(this.getBody());
};
App.prototype.getBody = function() {
let defaultUri = __dirname + '/assets/egWebmsg.html';
let grid = new Gtk.Grid({ columnHomogeneous: true });
let webView = new WebKit2.WebView({ vexpand: true });
webView.loadUri(GLib.filenameToUri(defaultUri, null));
// intercept requests as these happen
webView.on('decide-policy', (webView, policy, type) => {
switch(type) {
case WebKit2.PolicyDecisionType.NAVIGATION_ACTION:
let uri = policy.getRequest().getUri();
if (uri.indexOf(channel + ':') === 0) {
label.label = JSON.parse(decodeURIComponent(uri.slice(channel.length + 1)));
policy.ignore();
}
break;
}
});
grid.attach(webView, 0, 0, 2, 1);
let button = new Gtk.Button({ label: 'GTK to Webkit message' }).on('clicked', () => {
// Execute one Webkit function to send a message from GTK to Webkit
webView.runJavaScript('messageFromGTK("Message from GTK!");', null, (webView, result) => {
webView.runJavaScriptFinish(result);
});
});
grid.attach(button, 0, 1, 1, 1);
let label = new Gtk.Label({ label: '' });
grid.attach(label, 1, 1, 1, 1);
return grid;
};
//Run the application
let app = new App();
app.run();