Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto copy-paste with navigator.clipboard API #1301

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ const UI = {
addClipboardHandlers() {
document.getElementById("noVNC_clipboard_button")
.addEventListener('click', UI.toggleClipboardPanel);
document.getElementById("noVNC_clipboard_text")
.addEventListener('input', UI.syncClipboardPanelToLocalClipboard);
akamos marked this conversation as resolved.
Show resolved Hide resolved
document.getElementById("noVNC_clipboard_text")
.addEventListener('change', UI.clipboardSend);
document.getElementById("noVNC_clipboard_clear_button")
Expand Down Expand Up @@ -915,6 +917,35 @@ 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") {
navigator.clipboard.writeText(text).then(() => {
let debugMessage = text.substr(0, 40) + "...";
Log.Debug('>> UI.setClipboardText: navigator.clipboard.writeText with ' + debugMessage);
}).catch(() => {
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") {
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 => Log.Warn("<< UI.readLocalClipboard: Failed to read system clipboard-: " + err));
}
},

openClipboardPanel() {
UI.closeAllPanels();
UI.openControlbar();
Expand Down Expand Up @@ -944,11 +975,13 @@ 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("");
},

Expand All @@ -959,6 +992,13 @@ const UI = {
Log.Debug("<< UI.clipboardSend");
},

syncClipboardPanelToLocalClipboard() {
// Reads text from clipboard panel and set it to local clipboard
// Mainly used to synchronize clipboard panel with local clipboard
const text = document.getElementById('noVNC_clipboard_text').value;
UI.writeLocalClipboard(text);
},

/* ------^-------
* /CLIPBOARD
* ==============
Expand Down Expand Up @@ -1028,6 +1068,9 @@ 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 core/rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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 @@ -381,6 +383,7 @@ export default class RFB extends EventTargetMixin {
}

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

Expand Down
2 changes: 2 additions & 0 deletions tests/test.rfb.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ describe('Remote Frame Buffer Protocol Client', function () {
describe('#focus', function () {
it('should move focus to canvas object', function () {
client._canvas.focus = sinon.spy();
client.oncanvasfocus = sinon.spy();
client.focus();
expect(client._canvas.focus).to.have.been.calledOnce;
expect(client.oncanvasfocus).to.have.been.calledOnce;
});
});

Expand Down