-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathWebView.js
More file actions
249 lines (207 loc) · 7.78 KB
/
Copy pathWebView.js
File metadata and controls
249 lines (207 loc) · 7.78 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import Gtk from "gi://Gtk";
import WebKit2 from "gi://WebKit";
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Gdk from "gi://Gdk";
import { gettext as _ } from "gettext";
import { connect, getEnum } from "./util.js";
import { MODES } from "./constants.js";
Gio._promisify(Gtk.FileDialog.prototype, "save", "save_finish");
const {
WebContext,
CookiePersistentStorage,
CookieAcceptPolicy,
Settings,
NotificationPermissionRequest,
SecurityOrigin,
UserContentManager,
TLSErrorsPolicy,
WebView,
PolicyDecisionType,
} = WebKit2;
const {
build_filenamev,
get_user_special_dir,
UserDirectory,
get_language_names,
} = GLib;
const { AppInfo, ResourceLookupFlags, resources_open_stream } = Gio;
export function buildWebView({ instance, onNotification, window }) {
const { data_dir, cache_dir, url, id } = instance;
const network_session = WebKit2.NetworkSession.new(data_dir, cache_dir);
network_session.set_tls_errors_policy(TLSErrorsPolicy.FAIL);
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webcontext#signal-download-started
network_session.connect("download-started", (self, download) => {
// Beware of https://bugs.webkit.org/show_bug.cgi?id=201868
download.set_allow_overwrite(false);
const request = download.get_request();
const uri = request.get_uri();
if (uri.startsWith("http")) {
download.cancel();
AppInfo.launch_default_for_uri(uri, null);
return;
}
// We cannot open blob: and file: uris in an other application
// so we download them ourselves, it's okay because they are very
// quick to download so we don't need a progress UI for them
download.connect("failed", (self, err) => {
logError(err);
});
// https://webkitgtk.org/reference/webkit2gtk/stable/signal.Download.decide-destination.html
download.connect("decide-destination", (self, suggested_filename) => {
if (!suggested_filename || suggested_filename === "unknown.asc") {
suggested_filename = "";
}
const download_dir = get_user_special_dir(
UserDirectory.DIRECTORY_DOWNLOAD,
);
const dest_dir = download_dir
? Gio.File.new_for_path(download_dir)
: null;
const dialog = new Gtk.FileDialog({
title: _("Save File"),
initial_folder: dest_dir,
initial_name: suggested_filename,
});
dialog
.save(window, null)
.then((file) => {
download.set_destination(file.get_path());
})
.catch(logError);
return true;
});
});
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webcontext
const web_context = new WebContext();
web_context.set_spell_checking_enabled(true);
web_context.set_spell_checking_languages(get_language_names());
web_context.add_path_to_sandbox(data_dir, true);
web_context.add_path_to_sandbox(cache_dir, true);
const security_manager = web_context.get_security_manager();
security_manager.register_uri_scheme_as_local("tangram-resource");
web_context.register_uri_scheme("tangram-resource", (schemeRequest) => {
const stream = resources_open_stream(
schemeRequest.get_path(),
ResourceLookupFlags.NONE,
);
schemeRequest.finish(stream, -1, null);
});
/*
* Notifications
*/
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webcontext#signal-initialize-notification-permissions
if (url) {
web_context.connect("initialize-notification-permissions", () => {
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webcontext#method-initialize_notification_permissions
web_context.initialize_notification_permissions(
[SecurityOrigin.new_for_uri(url)],
[],
);
});
}
const website_data_manager = network_session.get_website_data_manager();
website_data_manager.set_favicons_enabled(true);
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.cookiemanager
const cookieManager = network_session.get_cookie_manager();
cookieManager.set_accept_policy(CookieAcceptPolicy.NO_THIRD_PARTY);
cookieManager.set_persistent_storage(
build_filenamev([data_dir, "cookies.sqlite"]),
CookiePersistentStorage.SQLITE,
);
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.usercontentmanager
const user_content_manager = new UserContentManager();
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.settings
const settings = new Settings({
enable_smooth_scrolling: true,
media_playback_requires_user_gesture: true,
// https://gitlab.gnome.org/GNOME/epiphany/-/blob/master/embed/ephy-embed-prefs.c
enable_back_forward_navigation_gestures: true,
enable_developer_extras: true,
javascript_can_open_windows_automatically: true,
allow_top_navigation_to_data_urls: false,
});
settings.set_user_agent_with_application_details("Tangram", pkg.version);
// user-agent
const userAgent = instance.settings.get_string("user-agent");
if (userAgent) settings.set_user_agent(userAgent);
instance.settings.connect(`changed::user-agent`, () => {
settings.set_user_agent(instance.settings.get_string("user-agent"));
});
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webcontext
const webView = new WebView({
web_context,
user_content_manager,
settings,
network_session,
});
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webinspector
// const webInspector = webView.get_inspector();
// webInspector.show();
connect(webView, {
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webview#signal-create
create(navigation_action) {
const request_url = navigation_action.get_request().get_uri();
console.debug("create", request_url);
if (webView.mode === MODES.TEMPORARY) {
webView.load_uri(request_url);
return;
}
// Open URL in default browser
Gtk.show_uri(window, request_url, Gdk.CURRENT_TIME);
},
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webview#signal-permission-request
["permission-request"](request) {
if (request instanceof NotificationPermissionRequest) {
request.allow();
return;
}
request.deny();
},
// https://gjs-docs.gnome.org/webkit240~4.0_api/webkit2.webview#signal-show-notification
["show-notification"](notification) {
onNotification(notification, id);
return true;
},
// https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#WebKitWebView-decide-policy
["decide-policy"](decision, decision_type) {
console.debug(
"decide-policy",
getEnum(PolicyDecisionType, decision_type),
);
if (decision_type === PolicyDecisionType.NAVIGATION_ACTION) {
// https://webkitgtk.org/reference/webkit2gtk/stable/WebKitNavigationAction.html
const navigation_action = decision.get_navigation_action();
const request_url = navigation_action.get_request().get_uri();
console.debug(
"navigation",
getEnum(WebKit2.NavigationType),
request_url,
);
if (didUserRequestOpenInBrowser(navigation_action)) {
decision.ignore();
Gtk.show_uri(window, request_url, Gdk.CURRENT_TIME);
return true;
}
}
return false;
},
// https://webkitgtk.org/reference/webkit2gtk/stable/signal.WebView.load-changed.html
// ["load-changed"](load_event) {
// console.debug("load-changed", getEnum(WebKit2.LoadEvent, load_event));
// },
});
webView.instance_id = id;
webView.load_uri(url);
return webView;
}
function didUserRequestOpenInBrowser(navigation_action) {
if (navigation_action.get_mouse_button() === Gdk.BUTTON_MIDDLE) {
return true;
}
const { CONTROL_MASK } = Gdk.ModifierType;
if ((navigation_action.get_modifiers() & CONTROL_MASK) === CONTROL_MASK) {
return true;
}
return false;
}