Copying asynchronous text

(Adam Havelka) #1

Hello is there going to be possibility to use document.execCommand("copy") in asynchronous function, so ideally, user could just enable with prompt that he wants this function to be enabled just like sharing location/webcam/…

At this time when i call it without any user interaction, it doesnt allow me to make it,
so i have to have just for this clipboard action small extension which is some other mess to install for every user using my app.

Firefox just throws this error:
document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.

(Michael Kohler) #2

The Clipboard API spec says in 5.3.1:

Event handlers may write to the clipboard if any of the following is true:

  • The action which triggers the event is invoked from the user-agent’s own user interface, e.g. from a “Copy” menu entry or shortcut key.
  • The action which triggers the event is invoked from a scripting thread which is allowed to show a popup.

You are hitting the second case. The part for “allowed to show a popup” is not very easy to understand, but I follow from that that it definitely needs to be triggered from inside the trusted handler.

This basically makes sure that scripts on a website can’t just copy arbitrary content to the system clipboard. I might have misunderstood something from the spec, but I’m quite confident this is why this is the case. Hope this helps.

What use case do you have specifically that you can’t use a button with a click handler?

(Adam Havelka) #3

My use case is quite simple,
using project guacamole i need to copy text from remote server clipboard, so when user in rdp/ssh selects some text, press copy (there are many ways to do it), so i can put all of that text trough browser so user can make his job just like when using Remmina/RemoteDesktop client without clicking on any other button.