From 619963277dd325745575a9f4332ab355618e2827 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 2 May 2018 22:44:04 -0700 Subject: [PATCH] GUACAMOLE-559: Add support for reading/writing clipboard contents using the Asynchronous Clipboard API. --- .../clipboard/services/clipboardService.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/guacamole/src/main/webapp/app/clipboard/services/clipboardService.js b/guacamole/src/main/webapp/app/clipboard/services/clipboardService.js index 9000c1848..cae6c0b28 100644 --- a/guacamole/src/main/webapp/app/clipboard/services/clipboardService.js +++ b/guacamole/src/main/webapp/app/clipboard/services/clipboardService.js @@ -179,6 +179,23 @@ angular.module('clipboard').factory('clipboardService', ['$injector', var deferred = $q.defer(); + try { + + // Attempt to read the clipboard using the Asynchronous Clipboard + // API, if it's available + if (navigator.clipboard && navigator.clipboard.writeText) { + if (data.type === 'text/plain') { + navigator.clipboard.writeText(data.data).then(deferred.resolve, deferred.reject); + return deferred.promise; + } + } + + } + + // Ignore any hard failures to use Asynchronous Clipboard API, falling + // back to traditional document.execCommand() + catch (ignore) {} + // Track the originally-focused element prior to changing focus var originalElement = document.activeElement; pushSelection(); @@ -415,6 +432,29 @@ angular.module('clipboard').factory('clipboardService', ['$injector', var deferred = $q.defer(); + try { + + // Attempt to read the clipboard using the Asynchronous Clipboard + // API, if it's available + if (navigator.clipboard && navigator.clipboard.readText) { + + navigator.clipboard.readText().then(function textRead(text) { + deferred.resolve(new ClipboardData({ + type : 'text/plain', + data : text + })); + }, deferred.reject); + + return deferred.promise; + + } + + } + + // Ignore any hard failures to use Asynchronous Clipboard API, falling + // back to traditional document.execCommand() + catch (ignore) {} + // Track the originally-focused element prior to changing focus var originalElement = document.activeElement;