Skip to content

Instantly share code, notes, and snippets.

@peter-dolkens
Created March 14, 2017 17:35
Show Gist options
  • Save peter-dolkens/c3fdb949615e6e3397681839b725dbbd to your computer and use it in GitHub Desktop.
Save peter-dolkens/c3fdb949615e6e3397681839b725dbbd to your computer and use it in GitHub Desktop.
Snippet to allow for uploading images to Spectrum
document.onpaste = function(event){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
var result = JSON.parse(xmlhttp.responseText);
document.execCommand("insertHTML", false, result.data.link);
console.log(result.data.link);
}
};
var uploadToImgur = function(event) {
xmlhttp.open("POST", "https://api.imgur.com/3/image", true);
xmlhttp.setRequestHeader('Authorization', 'Client-ID c869d0b56cf742b');
var data = new FormData();
data.append('type', 'base64');
data.append('image', event.target.result.replace('data:image/png;base64,', ''));
xmlhttp.send(data);
}
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
// console.log(JSON.stringify(items)); // will give you the mime types
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
switch (item.type) {
case 'image/png':
case 'image/jpg':
case 'image/jpeg':
case 'image/gif':
var blob = item.getAsFile();
var reader = new FileReader();
reader.onload = uploadToImgur; // data url!
reader.readAsDataURL(blob);
break;
}
}
}
}
@peter-dolkens
Copy link
Author

Just need to figure out how to trigger the client embed detection now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment