14

Is it possible to copy an image to the clipboard in javascript? I know how to copy text, but can you copy images?

Is it a security limitation?

Lance
  • 75,200
  • 93
  • 289
  • 503

4 Answers4

10

The last answer is from 2010 and browsers have changed a lot since then. With this simple function, you can copy whatever you want (text, images, tables, etc) (on your page) to the clipboard. The function receives the element id or the element itself.

function copyElementToClipboard(element) {
  window.getSelection().removeAllRanges();
  let range = document.createRange();
  range.selectNode(typeof element === 'string' ? document.getElementById(elementName) : element);
  window.getSelection().addRange(range);
  document.execCommand('copy');
  window.getSelection().removeAllRanges();
 }
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Theo
  • 476
  • 6
  • 4
4

Use Clipboard API. There is an example on MDN specifically for images.

Note: today in 2021 ClipboardItem can be enabled in Firefox by setting dom.events.asyncClipboard.clipboardItem to true. The rest major browsers support it. So, add some checks and inform a user how the feature can be turned on.

Here is an example:

async function writeClipImg() {
  if (!('ClipboardItem' in window)) {
    return alert(
      "Your browser doesn't support copying images into the clipboard."
      + " If you use Firefox you can enable it"
      + " by setting dom.events.asyncClipboard.clipboardItem to true."
    )
  }
  try {
    const imgURL = document.getElementById('image').src
    const data = await fetch(imgURL)
    const blob = await data.blob()

    await navigator.clipboard.write([
      new ClipboardItem({
        [blob.type]: blob
      })
    ])
    console.log('Fetched image copied.')
  } catch(err) {
    console.error(err.name, err.message)
  }
}

Lana
  • 1,199
  • 7
  • 16
3

Yes, most of the scripts supports text only.

http://forums.mozillazine.org/viewtopic.php?f=25&t=1195035&start=0

The above site also discussing the same issue.

The following site said related to security issues,

http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard

but this won't work in latest version of Mozilla.

Sajin
  • 223
  • 3
  • 9
3

No, you can't copy images to the clipboard. Copying anything to the clipboard is a security limitation of every browser, but you may able to copy text to the clipboard in IE if they have the proper security settings. Here Mozilla lists some of the problems caused by programmatic access to the clipboard.

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • @hobodave Then you must be browsing with IE, and you have changed your settings to allow it. Either that or you have a special FireFox add-on that allows it. – Josh Stodola Feb 23 '10 at 20:55
  • 2
    @hobodave visit github with Firefox and report back about what's in your clipboard. – Pointy Feb 23 '10 at 20:57
  • @Josh: Apologies, you are correct. There are ways to do it in FF though apparently: http://www.febooti.com/support/website-help/website-javascript-copy-clipboard.html . The functionality on github is actually achieved using Flash; I had assumed :-D it was javascript. – hobodave Feb 23 '10 at 20:58
  • I should say that I've seen Flash and Java Applet implementations that assist with copying to the clipboard (using Javascript to invoke them). Both options, however, are not suited for any production environment, IMO. – Josh Stodola Feb 23 '10 at 20:59
  • 1
    thanks, just wanted to establish this in stone so people don't have to search for hours with false hope :) – Lance Feb 23 '10 at 22:05