0

In Safari desktop document.execCommand('copy') doesn't work, so I'm struggling with adding a text in the clipboard on Cmd+C key press

What I tried is to catch Cmd+C combination on keydown event and do the following:

  • create a hidden input with text I want to put in clipboard
  • set focus to the input
  • select the text in the input

but text is not copied to clipboard. This works however on Chrome for example.

The following question says it's not possible but I see apps like draw.io and others doing it, so there has to be a way:

Does Safari handle the paste event correctly?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Don Box
  • 2,948
  • 21
  • 47

1 Answers1

-1

It is actually quite difficult, not to say impossible, and hacky to modify the user's clipboard. Your try was nice but I think your selection is overriden each time by the user's. What you may want to try is to modify the text on detection of Cmd/Ctrl+C and then restore it.

More details available here : How does Trello access the user's clipboard?

Community
  • 1
  • 1
  • I don't understand what you said. And I was aware of that question too. The solution doesn't work on Safari. – Don Box Jul 27 '16 at 14:59