Skip to content

Instantly share code, notes, and snippets.

@jerrydeng
Created March 15, 2019 21:46
Show Gist options
  • Save jerrydeng/91cf41c98be44c3ec5a47c988e23a62c to your computer and use it in GitHub Desktop.
Save jerrydeng/91cf41c98be44c3ec5a47c988e23a62c to your computer and use it in GitHub Desktop.
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!
document.getElementById("pastedImage").src = event.target.result;
};
reader.readAsDataURL(blob);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment