Skip to content

Commit

Permalink
Copy and Paste enabled for Chrome (#537)
Browse files Browse the repository at this point in the history
Applies novnc/noVNC#1301 solution for copy and paste.
  • Loading branch information
samirmansour authored Jun 23, 2020
1 parent 7b12434 commit 9142ae1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
53 changes: 53 additions & 0 deletions apps/dashboard/public/noVNC-1.1.0/app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ const UI = {
.addEventListener('click', UI.toggleClipboardPanel);
document.getElementById("noVNC_clipboard_text")
.addEventListener('change', UI.clipboardSend);
document.getElementById("noVNC_clipboard_text")
.addEventListener('input', UI.syncClipboardPanelToLocalClipboard);
document.getElementById("noVNC_clipboard_clear_button")
.addEventListener('click', UI.clipboardClear);
},
Expand Down Expand Up @@ -912,6 +914,49 @@ const UI = {
* CLIPBOARD
* ------v------*/

// Read and write text to local clipboard is currently only supported in Chrome 66+ and Opera53+
// further information at https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

writeLocalClipboard(text) {
if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.writeText !== "undefined" &&
typeof navigator.permissions !== "undefined" && typeof navigator.permissions.query !== "undefined"
) {
navigator.permissions.query({name: 'clipboard-write'})
.then(() => navigator.clipboard.writeText(text))
.then(() => {
let debugMessage = text.substr(0, 40) + "...";
Log.Debug('>> UI.setClipboardText: navigator.clipboard.writeText with ' + debugMessage);
})
.catch((err) => {
if(err.name !== 'TypeError'){
Log.Error(">> UI.setClipboardText: Failed to write system clipboard (trying to copy from NoVNC clipboard)");
}
});
}
},

readLocalClipboard() {
// navigator.clipboard and navigator.clipbaord.readText is not available in all browsers
if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.readText !== "undefined" &&
typeof navigator.permissions !== "undefined" && typeof navigator.permissions.query !== "undefined"
) {
navigator.permissions.query({name: 'clipboard-read'})
.then(() => navigator.clipboard.readText())
.then((clipboardText) => {
const text = document.getElementById('noVNC_clipboard_text').value;
if (clipboardText !== text) {
document.getElementById('noVNC_clipboard_text').value = clipboardText;
UI.clipboardSend();
}
})
.catch((err) => {
if(err.name !== 'TypeError'){
Log.Warn("<< UI.readLocalClipboard: Failed to read system clipboard-: " + err);
}
});
}
},

openClipboardPanel() {
UI.closeAllPanels();
UI.openControlbar();
Expand Down Expand Up @@ -941,14 +986,21 @@ const UI = {
clipboardReceive(e) {
Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0, 40) + "...");
document.getElementById('noVNC_clipboard_text').value = e.detail.text;
UI.writeLocalClipboard(e.detail.text);
Log.Debug("<< UI.clipboardReceive");
},

clipboardClear() {
document.getElementById('noVNC_clipboard_text').value = "";
UI.writeLocalClipboard("");
UI.rfb.clipboardPasteFrom("");
},

syncClipboardPanelToLocalClipboard() {
const text = document.getElementById('noVNC_clipboard_text').value;
UI.writeLocalClipboard(text);
},

clipboardSend() {
const text = document.getElementById('noVNC_clipboard_text').value;
Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
Expand Down Expand Up @@ -1026,6 +1078,7 @@ const UI = {
UI.rfb.addEventListener("securityfailure", UI.securityFailed);
UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
UI.rfb.oncanvasfocus = UI.readLocalClipboard;
UI.rfb.addEventListener("bell", UI.bell);
UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
UI.rfb.clipViewport = UI.getSetting('view_clip');
Expand Down
3 changes: 3 additions & 0 deletions apps/dashboard/public/noVNC-1.1.0/core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ export default class RFB extends EventTargetMixin {
// time to set up callbacks
setTimeout(this._updateConnectionState.bind(this, 'connecting'));

this.oncanvasfocus = () => {}; // Handler for canvas focused

Log.Debug("<< RFB.constructor");

// ===== PROPERTIES =====
Expand Down Expand Up @@ -406,6 +408,7 @@ export default class RFB extends EventTargetMixin {

focus() {
this._canvas.focus();
this.oncanvasfocus();
}

blur() {
Expand Down
46 changes: 46 additions & 0 deletions apps/dashboard/public/noVNC-1.1.0/legacy/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ var UI = {
addClipboardHandlers: function addClipboardHandlers() {
document.getElementById("noVNC_clipboard_button").addEventListener('click', UI.toggleClipboardPanel);
document.getElementById("noVNC_clipboard_text").addEventListener('change', UI.clipboardSend);
document.getElementById("noVNC_clipboard_text").addEventListener('input', UI.syncClipboardPanelToLocalClipboard);
document.getElementById("noVNC_clipboard_clear_button").addEventListener('click', UI.clipboardClear);
},

Expand Down Expand Up @@ -1054,6 +1055,41 @@ var UI = {
* CLIPBOARD
* ------v------*/

// Read and write text to local clipboard is currently only supported in Chrome 66+ and Opera53+
// further information at https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

writeLocalClipboard: function writeLocalClipboard(text) {
if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.writeText !== "undefined" && typeof navigator.permissions !== "undefined" && typeof navigator.permissions.query !== "undefined") {
navigator.permissions.query({ name: 'clipboard-write' }).then(function () {
return navigator.clipboard.writeText(text);
}).then(function () {
var debugMessage = text.substr(0, 40) + "...";
Log.Debug('>> UI.setClipboardText: navigator.clipboard.writeText with ' + debugMessage);
}).catch(function (err) {
if (err.name !== 'TypeError') {
Log.Error(">> UI.setClipboardText: Failed to write system clipboard (trying to copy from NoVNC clipboard)");
}
});
}
},
readLocalClipboard: function readLocalClipboard() {
// navigator.clipboard and navigator.clipbaord.readText is not available in all browsers
if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.readText !== "undefined" && typeof navigator.permissions !== "undefined" && typeof navigator.permissions.query !== "undefined") {
navigator.permissions.query({ name: 'clipboard-read' }).then(function () {
return navigator.clipboard.readText();
}).then(function (clipboardText) {
var text = document.getElementById('noVNC_clipboard_text').value;
if (clipboardText !== text) {
document.getElementById('noVNC_clipboard_text').value = clipboardText;
UI.clipboardSend();
}
}).catch(function (err) {
if (err.name !== 'TypeError') {
Log.Warn("<< UI.readLocalClipboard: Failed to read system clipboard-: " + err);
}
});
}
},
openClipboardPanel: function openClipboardPanel() {
UI.closeAllPanels();
UI.openControlbar();
Expand All @@ -1075,12 +1111,18 @@ var UI = {
clipboardReceive: function clipboardReceive(e) {
Log.Debug(">> UI.clipboardReceive: " + e.detail.text.substr(0, 40) + "...");
document.getElementById('noVNC_clipboard_text').value = e.detail.text;
UI.writeLocalClipboard(e.detail.text);
Log.Debug("<< UI.clipboardReceive");
},
clipboardClear: function clipboardClear() {
document.getElementById('noVNC_clipboard_text').value = "";
UI.writeLocalClipboard("");
UI.rfb.clipboardPasteFrom("");
},
syncClipboardPanelToLocalClipboard: function syncClipboardPanelToLocalClipboard() {
var text = document.getElementById('noVNC_clipboard_text').value;
UI.writeLocalClipboard(text);
},
clipboardSend: function clipboardSend() {
var text = document.getElementById('noVNC_clipboard_text').value;
Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
Expand Down Expand Up @@ -1154,6 +1196,7 @@ var UI = {
UI.rfb.addEventListener("securityfailure", UI.securityFailed);
UI.rfb.addEventListener("capabilities", UI.updatePowerButton);
UI.rfb.addEventListener("clipboard", UI.clipboardReceive);
UI.rfb.oncanvasfocus = UI.readLocalClipboard;
UI.rfb.addEventListener("bell", UI.bell);
UI.rfb.addEventListener("desktopname", UI.updateDesktopName);
UI.rfb.clipViewport = UI.getSetting('view_clip');
Expand Down Expand Up @@ -7589,6 +7632,8 @@ var RFB = function (_EventTargetMixin) {
// time to set up callbacks
setTimeout(_this._updateConnectionState.bind(_this, 'connecting'));

_this.oncanvasfocus = function () {}; // Handler for canvas focused

Log.Debug("<< RFB.constructor");

// ===== PROPERTIES =====
Expand Down Expand Up @@ -7697,6 +7742,7 @@ var RFB = function (_EventTargetMixin) {
key: 'focus',
value: function focus() {
this._canvas.focus();
this.oncanvasfocus();
}
}, {
key: 'blur',
Expand Down

0 comments on commit 9142ae1

Please sign in to comment.