How can I copy text to the clipboard without flash in HTML5?

4k views Asked by At

On Google Drive, if I try to share a file and I use Google Chrome, this button appears: button copy url appears only if is public file

Now, if I press "copy link", it will select the textarea down the button. (it is a input type button) here the picture: enter image description here

And the question is .... How google do to copy ad text/url to te clipboard without using flash (zeroclipboard)? which API of chrome it used? I'm try to inspect this but see only js.

1

There are 1 answers

0
Max Zlotskiy On BEST ANSWER

It is not possible to do it without flash as far as I know. However, if you are using Google Chrome and you installed the Drive App(from the webstore) then you give the browser permission to use your copy and paste.

Try clicking on "right click menu" section in this article: google drive help

Basically, you need permission from the browser. If you can't, then trick the user. I once made a canvas which displayed text, and when the user dragged their mouse it would select the corresponding text in an invisible <div>, and when you press Control+C or Cmd+C you are copying the text using the native shortcut, javascript can't mimic these keys.

The best you can do with javascript is to select the text using document.createRange() and leave it to the user to press Control+C or Cmd+C.