Copying text to the clipboard

4,830 views
Skip to first unread message

Tyler Wayne

unread,
Aug 22, 2011, 8:21:29 PM8/22/11
to Chromium-extensions
I've had an extension that's wanted the ability to do this for a while
now, but I never got it too far off the ground because it seemed like
there was no way to get it working. Lately I looked at the extension
development page, and I saw the new permissions "clipboardRead" and
"clipboardWrite." I also noticed that the experimental clipboard API
seems to have disappeared.

Anyway, I have tried every method I can think of (ZeroClipboard,
document.execCommand('copy'), an object created by Manticore [ ]) and
I haven't been able to get anything to work. I just want to put some
text into the clipboard when the user clicks a button. Does anyone
know how to do this?

Mihai Parparita

unread,
Aug 22, 2011, 8:27:01 PM8/22/11
to Tyler Wayne, Chromium-extensions
WIth Chrome 13, you should be able to add clipboardRead and/or clipboardWrite permissions to your manifest and call document.execCommand('paste') or document.execCommand('copy') (see http://code.google.com/chrome/extensions/manifest.html#permissions).

Mihai


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Tyler Wayne

unread,
Aug 23, 2011, 12:51:33 AM8/23/11
to Chromium-extensions
I am able to run the commands, yes, but I cannot seem to make them
have any effect. My main questions are:
Does document.contentEditable need to be true?
Does it copy whatever is selected?

On Aug 22, 6:27 pm, Mihai Parparita <mih...@chromium.org> wrote:
> WIth Chrome 13, you should be able to add clipboardRead
> and/or clipboardWrite permissions to your manifest and
> call document.execCommand('paste') or document.execCommand('copy') (seehttp://code.google.com/chrome/extensions/manifest.html#permissions).
>
> Mihai
>
>
>
>
>
>
>
> On Mon, Aug 22, 2011 at 5:21 PM, Tyler Wayne <maddog...@gmail.com> wrote:
> > I've had an extension that's wanted the ability to do this for a while
> > now, but I never got it too far off the ground because it seemed like
> > there was no way to get it working. Lately I looked at the extension
> > development page, and I saw the new permissions "clipboardRead" and
> > "clipboardWrite." I also noticed that the experimental clipboard API
> > seems to have disappeared.
>
> > Anyway, I have tried every method I can think of (ZeroClipboard,
> > document.execCommand('copy'), an object created by Manticore [ ]) and
> > I haven't been able to get anything to work. I just want to put some
> > text into the clipboard when the user clicks a button. Does anyone
> > know how to do this?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

Mihai Parparita

unread,
Aug 23, 2011, 1:12:13 AM8/23/11
to Tyler Wayne, Chromium-extensions
You don't need contentEditable, and anything selected should get copied. Keep in mind that if you're calling execCommand('copy') in the background page, then it's whatever selection is made in the background page that get copied. You can use a temporary DOM node that you set the contents of and then programmatically select to copy arbitrary things to the clipboard:

<div id="temp"></div>
<script>
function copyToClipboard(str) {
  var tempNode = document.getElementById("temp");
  tempNode.textValue = str;
  tempNode.select();
  document.execCommand("copy");
}
</script>

If you're like to do this based on content that's selected in a page, you'll need to use a content script to extract the selection and then pass the contents of that back to the background page where you can run copyToClipboard.

Mihai

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Tyler Wayne

unread,
Aug 23, 2011, 12:47:01 PM8/23/11
to Chromium-extensions
Hm. Thank you for telling me to put it in a background page. I had
previously been avoiding one because my extension only makes
modifications to a single website, and no persistent processing was
previously needed.

However, after putting document.execCommand('copy') into a background
page instead of the content script, it appears that the
"clipboardWrite" permission only applies to background pages. Yet
another crippling limitation for content scripts, but at least the
functionality is still there.

Again, thank you for your help.

Scott Fujan

unread,
Aug 23, 2011, 1:37:43 PM8/23/11
to Tyler Wayne, Chromium-extensions

This report looks related: https://code.google.com/p/chromium/issues/detail?id=82024; you might star it.

> To post to this group, send email to chromium-...@chromium.org.
> To unsubscribe from this group, send email to chromium-extens...@chromium.org.
Reply all
Reply to author
Forward
0 new messages