0

I discovered the copy() function in Chrome by accident. When I tested it out, it copied an inputted string to my clipboard.

I've seen more complicated ways to copy a string to clipboard, and many of which are barely full-browser compatible. However, this copy() function is by far the simplest solution because it appears to be native code. I know this because typing this function without the parentheses returns this:

ƒ copy(value) { [Command Line API] }

This seems like a super useful function for the project I'm working on at the moment, but I don't want to implement it right away. Before I utilize this, I want to know its compatibility with other browsers. I have tried googling this command but yielded no results to my amazement. This also has me wondering, is this a new function or API or whatever? Why am I only learning about this now? Why can I find no information about this incredibly useful function?

Also, I think it is important to note that I am using chrome, presumably the latest version, with no extensions that, to my knowledge, add function like these to the console.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
Diriector_Doc
  • 582
  • 1
  • 12
  • 28
  • 4
    Where did you find that? MDN documents `document.execCommand("copy")` https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard – Barmar Aug 31 '20 at 03:56
  • Can you link to used Command Line API? There's an implementation behind the command, ex.`execCommand('copy')` is widely supported, and modern browsers have [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API). Both of these are more or less user permission dependent, Clipboard API asks user a permission, and the permission to use copy command is behind the browser (configuration) options. – Teemu Aug 31 '20 at 04:25
  • Related: https://stackoverflow.com/questions/4559180/secret-copy-to-clipboard-javascript-function-in-chrome-and-firefox Also: https://developers.google.com/web/tools/chrome-devtools/console/utilities#copy It looks like these functions are provided for use when debugging in the console, and not intended to be used in web pages. – JLRishe Aug 31 '20 at 04:49
  • This is a function provided by the devtools, not a browser api. – ray Aug 31 '20 at 04:59

1 Answers1

3

No, don't use this function in production code.

The copy function is provided for debugging purposes within the Chrome dev tools. It's not intended for use in a page. It can't even be called from page script.

See the following MDN page for information of various ways to interact with the clipboard and some information about cross-browser concerns:

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • That MDN article is for chrome:// extensions, web scripts suffer from even more restrictions which are not covered by this article. – Kaiido Aug 31 '20 at 05:34