m4tx
m4tx

Reputation: 4511

Copying text to clipboard in Chrome extension

I'm writing a Google Chrome extension and I want to copy some text in clipboard in a content script. I tried selecting it and then document.execCommand('copy') - it doesn't work. I don't want Flash, because it's not easy and elegant way to achieve that. I tried background page and <input> - it doesn't work.

Is there any working, elegant and simple way to copy text to clipboard in Chrome extension? It may also use jQuery.

Regards

Upvotes: 6

Views: 4077

Answers (1)

int3
int3

Reputation: 13201

Here's some working (Coffeescript) code that does copy / paste: https://github.com/philc/vimium/blob/master/lib/clipboard.coffee

Note that the code above runs in the background page; there was a bug a while back which broke execCommand in content scripts, and I'm not sure if that was ever fixed.

Upvotes: 6

Related Questions