1

Following Copy/Paste code not working in Chrome Extension, I need to write Chrome Extension that copy and paste data using clipboard.

I write following code in Backgroung.html page, but its not working.

    function buttonClick(){

               document.getElementById('initialText').select();


        chrome.experimental.clipboard.executeCopy(1, function() {
            alert("Copy");
            document.getElementById('nameText').focus();


            chrome.experimental.clipboard.executePaste(1, function() {
                alert("Paste");
            });
        });
      }
chandreshkoyani
  • 183
  • 4
  • 11

2 Answers2

8

As of Chrome 13, clipboard access is no longer experimental.

The commands are now document.execCommand('paste'), document.execCommand('copy') and document.execCommand('cut').

However, permissions need to be added to your manifest: "clipboardRead" and "clipboardWrite".

Try implementing the above and see how you get on.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
Chris McFarland
  • 6,059
  • 5
  • 43
  • 63
  • Nice, I didn't know this. Also it seems `document.execCommand()` is available only in background pages. – serg Aug 04 '11 at 02:50
  • I've used `document.execCommand('copy')` for my [extension](https://chrome.google.com/webstore/detail/dcjnfaoifoefmnbhhlbppaebgnccfddf) for a long time and it's always worked (though only ever from my background page). I'm wondering if I'll now have to add the new permission. When are these changes going in? I'm using Chrome 13 and my extension still works without the additional permissions. – neocotic Aug 04 '11 at 13:06
  • Just added these to my extension and it's worth noting that, although no additional access is displayed on the [extension's details page](https://chrome.google.com/webstore/detail/dcjnfaoifoefmnbhhlbppaebgnccfddf), my extension was disabled when I updated until I accepted the new "Data you copy and paste" access. – neocotic Aug 04 '11 at 15:03
  • 1
    As of 2014, copy/paste are accessible to content_scripts in-page. However, these actions only work from non-hidden, editable fields. If you wish to work with plaintext, use TEXTAREA. If you wish to work with formatted text use DIV contentEditable=true. If you are moving formatted text from the page to the clipboard, do it in a content_script, as you don't want to inject untrusted HTML into a background page DOM. – David Jeske Apr 12 '17 at 17:24
5

To eliminate the obvious; have you added the "experimental" permission to your manifest and are you using the latest dev build of Chrome as listed in the official documentation?

Otherwise, I'm not sure what may help you as I don't use the experimental API due to them not being usable in production. There is a workaround for copying without using the experimental API (using an input field and document.execCommand) but I'm not sure how to paste without it.

EDIT:

I've just noticed that experimental.clipboard is not longer listed on the experimental API page. It may be that this namespace has been deprecated/abandoned as can happen when using the experimental API. A simple test for this would be inserting;

console.log(typeof chrome.experimental.clipboard);
console.log(typeof chrome.experimental.clipboard.executeCopy);
console.log(typeof chrome.experimental.clipboard.executePaste);

Which should output the following the console for your background page;

> object
> function
> function
neocotic
  • 2,131
  • 18
  • 18