How does the paste image from clipboard functionality work in Gmail and Google Chrome 12+? How does the paste image from clipboard functionality work in Gmail and Google Chrome 12+? google-chrome google-chrome

How does the paste image from clipboard functionality work in Gmail and Google Chrome 12+?


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.


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

// window.addEventListener('paste', ... ordocument.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 :$ :)


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 apior using a signed applet