173

I noticed a blog post from Google that mentions the ability to paste images directly from the clipboard into a Gmail message if you're using the latest version of Chrome. I tried this with my version of Chrome (12.0.742.91 beta-m) and it works great using control keys or the context menu.

From that behavior I need to assume that the latest version of webkit used in Chrome is able to deal with images in the Javascript paste event, but I have been unable to locate any references to such an enhancement. I believe ZeroClipboard binds to keypress events to trigger its flash functionality and as such wouldn't work through the context menu (also, ZeroClipboard is cross-browser and the post says this works only with Chrome).

So, how does this work and where the enhancement was made to Webkit (or Chrome) that enables the functionality?

1
  • 3
    It seems that it works randomly with Firefox as well. Anybody knows if this is supposed to be supported with Firefox ? Commented Sep 30, 2013 at 13:36

5 Answers 5

286

I spent some time experimenting with this. It seems to sort of follow the new Clipboard API spec. You can define a "paste" event handler and look at event.clipboardData.items, and call getAsFile() on them to get a Blob. Once you have a Blob, you can use FileReader on it to see what's in it. This is how you can get a data url for the stuff you just pasted in Chrome:

document.onpaste = function (event) {
    var items = (event.clipboardData || event.originalEvent.clipboardData).items;
    console.log(JSON.stringify(items)); // might give you mime types
    for (var index in items) {
        var item = items[index];
        if (item.kind === 'file') {
            var blob = item.getAsFile();
            var reader = new FileReader();
            reader.onload = function (event) {
                console.log(event.target.result); // data url!
            }; 
            reader.readAsDataURL(blob);
        }
    }
};

Once you have a data url you can display the image on the page. If you want to upload it instead, you could use readAsBinaryString, or you could put it into an XHR using FormData.

Edit: Note that the item is of type DataTransferItem. JSON.stringify might not work on the items list, but you should be able to get mime type when you loop over items.

Sign up to request clarification or add additional context in comments.

11 Comments

Clutching at straws here, but any ideas why event.clipboardData.items seems to be 'undefined' in Safari 5.1? Or even how to get the clipboard contents for a file/blob in Safari? Works great in Chrome. You'd think webkit would be webkit :(
@SenicaGonzalez that is because the data only exists for the duration of the event. After the event, it is gone, so when you try to flip open the object in the inspector you will see nothing.
Would you mind making an example how to submit a XMLHttpRequest with that image data? That would be real nice :D
Here's how you can submit that using XMLHttpRequest, I wrote it up in a blog after I implemented it: blog.securevideo.com/2013/11/27/…
Now that the first item in the list isn't always the file you pasted, I've updated it to loop through the items to find any pasted files.
|
76

The answer by Nick seems to need small changes to still work :)

// window.addEventListener('paste', ... or
document.onpaste = function (event) {
  // use event.originalEvent.clipboard for newer chrome versions
  var items = (event.clipboardData  || event.originalEvent.clipboardData).items;
  console.log(JSON.stringify(items)); // will give you the mime types
  // find pasted image among pasted items
  var blob = null;
  for (var i = 0; i < items.length; i++) {
    if (items[i].type.indexOf("image") === 0) {
      blob = items[i].getAsFile();
    }
  }
  // load image if there is a pasted image
  if (blob !== null) {
    var reader = new FileReader();
    reader.onload = function(event) {
      console.log(event.target.result); // data url!
    };
    reader.readAsDataURL(blob);
  }
}

Example running code: http://jsfiddle.net/bt7BU/225/

So the changes to nicks answer were:

var items = event.clipboardData.items;

to

var items = (event.clipboardData  || event.originalEvent.clipboardData).items;

Also I had to take the second element from the pasted items (first one seems to be text/html if you copy an image from another web page into the buffer). So I changed

  var blob = items[0].getAsFile();

to a loop finding the item containing the image (see above)

I didn't know how to answer directly to Nick's answer, hope it is fine here :$ :)

10 Comments

How should we submit the image data as XMLHttpRequest?
To others reading this, answer to this question may be included there now: stackoverflow.com/questions/18055422/… :)
I don't understand. When I paste files in browser, the clipboardData.items is always empty in google chrome (Firefox works). The chrome now needs almost as much optimization as IE used to.
Small edit: if (blob != null) { (or set blob = null in initialization)
event.clipboardData.items worked fine for me on the latest Chrome, not sure when event.originalEvent... is useful?
|
1

As far as I know -

With HTML 5 features(File Api and the related) - accessing clipboard image data is now possible with plain javascript.

This however fails to work on IE (anything less than IE 10). Don't know much about IE10 support also.

For IE the optiens that I believe are the 'fallback' options are either using Adobe's AIR api or using a signed applet

Comments

1
<html><body>
<p> Right click on any image in chrome and press copy image.<br>
Press Ctrl + V to pase your image. Works on chrome </p>

<div id='MyDiv1'></div>

<script>
document.onpaste = function (event) {
  var items = (event.clipboardData || event.originalEvent.clipboardData).items;
  var blob = items[items.length - 1].getAsFile();
  var reader = new FileReader();
  reader.readAsDataURL(blob);
  reader.onload = function (event) {
    const img1 = document.createElement("img");
    img1.src = event.target.result; // Appending image data to img tag
    document.getElementById('MyDiv1').appendChild(img1);
  } 
}

</script>
</body></html>

Paste this and save as .html and you are good to go

2 Comments

Your answer could be improved with the help of supporting information such as tutorials and links to documentation
Okay feedback taken. Will take care of these while answering next time.
1

As of Jan 2024 Chrome seems to fire onpaste event only in certain circumstances, but not always. If you need to handle onpaste event without a focused <input> element, the clipboard can be accessed using the navigator.clipboard property. For example:

if (navigator.clipboard && navigator.clipboard.read) {
    document.addEventListener("keydown", event => {
        if (event.key == "v" && (event.ctrlKey || event.metaKey)) {
            try {
                const items = await navigator.clipboard.read();

                let result;
                for (let i = 0, len = items.length; i < len; i++) {
                    const item = items[i];
                    if (item.types.includes("image/png")) {
                        const blob = await item.getType("image/png");
                        if (!result)
                            result = [];
                        result.push(blob);
                    }
                }
                if (result) {
                    // Do something with the pasted blobs
                }
            } catch (ignored) {
            }
        }
    });
}

The code above works in Chrome-based browsers and Safari. It doesn't work in Firefox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.