Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 307
m4tx Avatar asked Dec 09 '12 12:12

m4tx


People also ask

How do I enable copy to clipboard in Chrome?

To Enable Shared Clipboard In Google Chrome, Open the Google Chrome browser. Type the following text in the address bar: chrome://flags#shared-clipboard-receiver . Select Enabled from the drop down list next to the Enable receiver device to handle shared clipboard feature option.

How do I copy text to clipboard?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do I copy from Chrome clipboard?

Highlight the text you wish to copy. 2. Right click and select "Copy" or press on CTRL+C (Windows) / CMD+C (MAC).

How do I add clipboard to Chrome?

How to Enable Clipboard Sharing. This hidden feature is available as a flag. To find it, open a new tab, paste chrome://flags into Chrome's Omnibox and then press the Enter key. Search for “Clipboard” in the search box.


1 Answers

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.

like image 154
int3 Avatar answered Sep 30 '22 10:09

int3