Created
March 14, 2017 17:35
-
-
Save peter-dolkens/c3fdb949615e6e3397681839b725dbbd to your computer and use it in GitHub Desktop.
Snippet to allow for uploading images to Spectrum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just need to figure out how to trigger the client embed detection now!