chandreshkoyani
chandreshkoyani

Reputation: 183

Copy/Paste Not Working in Chrome Extension

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");
            });
        });
      }

Upvotes: 1

Views: 8290

Answers (2)

neocotic
neocotic

Reputation: 2131

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

Upvotes: 5

Chris McFarland
Chris McFarland

Reputation: 6169

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.

Upvotes: 8

Related Questions