0

I need to implement a feature that copy-pastes some custom data across our websites using javascript.

After a lot of stumbling blocks, this question's lead answer pointed me in the right direction.

Basically, to copy, I add a new textarea to my document, set its value to my desired text, and run document.execCommand('copy').

So far so good.

I attempting to implement paste similarly, and there lies the problem. I add a textarea, focus and select it, and run document.execCommand('paste'); but it doesn't work.

Here's a fiddle demonstration:

https://jsfiddle.net/tusharx23/7zpjfh2h/1/

As you can see, the log says that 'paste was unsuccessful'

Can anyone help me get the paste to work?

Thank you.

Community
  • 1
  • 1
Snowman
  • 2,305
  • 6
  • 17
  • 30
  • It doesn't work because it shouldn't work. Websites are not supposed to have read access to the contents of your clipboard. It would be a security nightmare. – Robby Cornelissen May 13 '16 at 08:54
  • @RobbyCornelissen: I know, and I agree. But if it's my site and my source code, and if I'm listening to the paste event triggered by user (not maliciously reading data in the background), I think it's fair to expect a way to paste my data. – Snowman May 13 '16 at 09:54
  • The problem is that the browser has no way to detect that it's your site and that what you're doing is not malicious in nature. If you really need this, you're best bet is to develop or search for a Chrome extension that allows this. You could try to post a feature request for Chrome, but I can pretty much guarantee that it will be shot down. – Robby Cornelissen May 13 '16 at 10:02

0 Answers0