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 1 commit
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
32 changes: 32 additions & 0 deletions app/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,32 @@ const UI = {
* CLIPBOARD
* ------v------*/

writeLocalClipboard(text) {
if (typeof navigator.clipboard !== "undefined" && typeof navigator.clipboard.readText !== "undefined") {
akamos marked this conversation as resolved.
Show resolved Hide resolved
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,17 +970,20 @@ 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("");
},

clipboardSend() {
const text = document.getElementById('noVNC_clipboard_text').value;
Log.Debug(">> UI.clipboardSend: " + text.substr(0, 40) + "...");
UI.writeLocalClipboard(text);
akamos marked this conversation as resolved.
Show resolved Hide resolved
UI.rfb.clipboardPasteFrom(text);
Log.Debug("<< UI.clipboardSend");
},
Expand Down Expand Up @@ -1028,6 +1057,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