Skip to content

Commit

Permalink
support automatic clipboard support. see novnc/noVNC#1347
Browse files Browse the repository at this point in the history
  • Loading branch information
t-matsuo committed Jun 11, 2023
1 parent 2fa7a77 commit 4468513
Show file tree
Hide file tree
Showing 3 changed files with 3,089 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Dockerfile.split
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ COPY ./assets/scripts/set-resolution /usr/local/bin/
# Pach VNC. See https://github.com/novnc/noVNC/pull/1451
COPY ./assets/novnc/vnc.html $NO_VNC_HOME/vnc.html
COPY ./assets/novnc/launch.sh $NO_VNC_HOME/utils/launch.sh
# Pach Clipboard Copy/Paste See https://github.com/novnc/noVNC/pull/1347
COPY ./assets/novnc/clipboard.js $NO_VNC_HOME/core/clipboard.js
COPY ./assets/novnc/rfb.js $NO_VNC_HOME/core/rfb.js

This comment has been minimized.

Copy link
@hh

hh Jul 16, 2023

Does this actually work? I'm interested to know!

This comment has been minimized.

Copy link
@t-matsuo

t-matsuo via email Jul 16, 2023

Author Owner
#endif
#ifdef NGINX
COPY ./assets/supervisor/nginx.ini /etc/supervisord.d/
Expand Down
78 changes: 78 additions & 0 deletions assets/novnc/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* noVNC: HTML5 VNC client
* Copyright (c) 2021 Juanjo Díaz
* Licensed under MPL 2.0 or any later version (see LICENSE.txt)
*/

export default class Clipboard {
constructor(target) {
this._target = target;

this._eventHandlers = {
'copy': this._handleCopy.bind(this),
'focus': this._handleFocus.bind(this)
};

// ===== EVENT HANDLERS =====

this.onpaste = () => {};
}

// ===== PRIVATE METHODS =====

async _handleCopy(e) {
try {
if (navigator.permissions && navigator.permissions.query) {
const permission = await navigator.permissions.query({ name: "clipboard-write", allowWithoutGesture: false });
if (permission.state === 'denied') return;
}
} catch (err) {
// Some browsers might error due to lack of support, e.g. Firefox.
}

if (navigator.clipboard.writeText) {
try {
await navigator.clipboard.writeText(e.clipboardData.getData('text/plain'));
} catch (e) {
/* Do nothing */
}
}
}

async _handleFocus() {
try {
if (navigator.permissions && navigator.permissions.query) {
const permission = await navigator.permissions.query({ name: "clipboard-read", allowWithoutGesture: false });
if (permission.state === 'denied') return;
}
} catch (err) {
// Some browsers might error due to lack of support, e.g. Firefox.
}

if (navigator.clipboard.readText) {
try {
const data = await navigator.clipboard.readText();
this.onpaste(data);
} catch (e) {
/* Do nothing */
return;
}
}
}

// ===== PUBLIC METHODS =====

grab() {
if (!Clipboard.isSupported) return;
this._target.addEventListener('copy', this._eventHandlers.copy);
this._target.addEventListener('focus', this._eventHandlers.focus);
}

ungrab() {
if (!Clipboard.isSupported) return;
this._target.removeEventListener('copy', this._eventHandlers.copy);
this._target.removeEventListener('focus', this._eventHandlers.focus);
}
}

Clipboard.isSupported = (navigator && navigator.clipboard) ? true : false;
Loading

0 comments on commit 4468513

Please sign in to comment.