padarom / guacamole-common-js

:avocado: The JavaScript Guacamole client library modified for easy installation and usage via npm
http://guac-dev.org/
Apache License 2.0
41 stars 18 forks source link

How can I send clipboard data? #22

Closed cedricbieder closed 1 year ago

cedricbieder commented 1 year ago

Hi, how can I send data to the clipboard. In older versions, there was the this.client.setClipboard(text) function for this.

     * Sets the clipboard of the remote client to the given text data.
     *
     * @deprecated Use createClipboardStream() instead.
     * @param {String} data The data to send as the clipboard contents.
     */
    this.setClipboard = function(data) {

        // Do not send requests if not connected
        if (!isConnected())
            return;

        // Open stream
        var stream = guac_client.createClipboardStream("text/plain");
        var writer = new Guacamole.StringWriter(stream);

        // Send text chunks
        for (var i=0; i<data.length; i += 4096)
            writer.sendText(data.substring(i, i+4096));

        // Close stream
        writer.sendEnd();

    };

In the newer version I cannot find something similar. How can I send there data to my connection clipboard? The function is precated since 0.9.1 and was removed here https://github.com/padarom/guacamole-common-js/commit/57d81544ea46fc4f7e68836d146ca60022c430fa. But I dont know what to use now.

Found this implementation in the guacamole git:

https://github.com/apache/guacamole-client/blob/7c502fdd0e28cd7c9d16bfab5c0edf01c8e47122/guacamole/src/main/frontend/src/app/client/types/ManagedClient.js#L737 Overall the similar stuff, but I can not find this in the npm package.

padarom commented 1 year ago

The JSDoc of the block you pasted mentions how to use it now, which is also pretty much exactly what the linked ManagedClient does.

They merely removed the convenience method setClipboard, but the underlying implementation was kept. You'll just have to write some more boilerplate yourself now in order to re-implement setClipboard, e.g.

function setClipboardText(client, text) {
    // Create stream with proper mimetype
    var stream = client.createClipboardStream('text/plain');

    const writer = new Guacamole.StringWriter(stream);
    writer.sendText(text);
    writer.sendEnd();
}