Skip to content

Commit 4468513

Browse files
committed
support automatic clipboard support. see novnc/noVNC#1347
1 parent 2fa7a77 commit 4468513

File tree

3 files changed

+3089
-0
lines changed

3 files changed

+3089
-0
lines changed

Dockerfile.split

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ COPY ./assets/scripts/set-resolution /usr/local/bin/
200200
# Pach VNC. See https://github.com/novnc/noVNC/pull/1451
201201
COPY ./assets/novnc/vnc.html $NO_VNC_HOME/vnc.html
202202
COPY ./assets/novnc/launch.sh $NO_VNC_HOME/utils/launch.sh
203+
# Pach Clipboard Copy/Paste See https://github.com/novnc/noVNC/pull/1347
204+
COPY ./assets/novnc/clipboard.js $NO_VNC_HOME/core/clipboard.js
205+
COPY ./assets/novnc/rfb.js $NO_VNC_HOME/core/rfb.js
203206
#endif
204207
#ifdef NGINX
205208
COPY ./assets/supervisor/nginx.ini /etc/supervisord.d/

assets/novnc/clipboard.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* noVNC: HTML5 VNC client
3+
* Copyright (c) 2021 Juanjo Díaz
4+
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
5+
*/
6+
7+
export default class Clipboard {
8+
constructor(target) {
9+
this._target = target;
10+
11+
this._eventHandlers = {
12+
'copy': this._handleCopy.bind(this),
13+
'focus': this._handleFocus.bind(this)
14+
};
15+
16+
// ===== EVENT HANDLERS =====
17+
18+
this.onpaste = () => {};
19+
}
20+
21+
// ===== PRIVATE METHODS =====
22+
23+
async _handleCopy(e) {
24+
try {
25+
if (navigator.permissions && navigator.permissions.query) {
26+
const permission = await navigator.permissions.query({ name: "clipboard-write", allowWithoutGesture: false });
27+
if (permission.state === 'denied') return;
28+
}
29+
} catch (err) {
30+
// Some browsers might error due to lack of support, e.g. Firefox.
31+
}
32+
33+
if (navigator.clipboard.writeText) {
34+
try {
35+
await navigator.clipboard.writeText(e.clipboardData.getData('text/plain'));
36+
} catch (e) {
37+
/* Do nothing */
38+
}
39+
}
40+
}
41+
42+
async _handleFocus() {
43+
try {
44+
if (navigator.permissions && navigator.permissions.query) {
45+
const permission = await navigator.permissions.query({ name: "clipboard-read", allowWithoutGesture: false });
46+
if (permission.state === 'denied') return;
47+
}
48+
} catch (err) {
49+
// Some browsers might error due to lack of support, e.g. Firefox.
50+
}
51+
52+
if (navigator.clipboard.readText) {
53+
try {
54+
const data = await navigator.clipboard.readText();
55+
this.onpaste(data);
56+
} catch (e) {
57+
/* Do nothing */
58+
return;
59+
}
60+
}
61+
}
62+
63+
// ===== PUBLIC METHODS =====
64+
65+
grab() {
66+
if (!Clipboard.isSupported) return;
67+
this._target.addEventListener('copy', this._eventHandlers.copy);
68+
this._target.addEventListener('focus', this._eventHandlers.focus);
69+
}
70+
71+
ungrab() {
72+
if (!Clipboard.isSupported) return;
73+
this._target.removeEventListener('copy', this._eventHandlers.copy);
74+
this._target.removeEventListener('focus', this._eventHandlers.focus);
75+
}
76+
}
77+
78+
Clipboard.isSupported = (navigator && navigator.clipboard) ? true : false;

0 commit comments

Comments
 (0)