Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Created May 20, 2018 10:11
Show Gist options
  • Save Lewiscowles1986/6cac3472336a1cf009a88bde56a65fe3 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/6cac3472336a1cf009a88bde56a65fe3 to your computer and use it in GitHub Desktop.
Anonymous Upload images to Imgur V3 JS
var ImgurAPIKey = 'YEAH-IM-NOT-GIVING-THAT';
window.addEventListener('paste', function(e) {
function eventPreventDefault(e) {
e.preventDefault();
}
function getClipboardData(e) {
return window.clipboardData || e.clipboardData;
}
function resolveItem(item) {
return {
"data": item.getAsFile(),
"tmpname": getTypeFromClipboardItem(item)
};
}
function getTypeFromClipboardItem(item) {
switch (item.type) {
case "image/gif":
return "image.gif";
case "image/png":
return "image.png";
case "image/jpeg":
return "image.jpg"
}
}
function removeEmpty(item) {
if (!(item.data && item.tmpname)) {
return false;
}
return (item.data instanceof File);
}
function getClipboardFileData(data) {
if(data.getData) {
return Array.from(data.items)
.map(resolveItem)
.filter(removeEmpty)
.forEach(function(item) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://api.imgur.com/3/image', true);
xhr.setRequestHeader('Authorization', 'Client-ID '+ImgurAPIKey);
xhr.onload = function(e) {
console.log(xhr.response);
}
var fd = new FormData();
fd.append("image", item.data);
xhr.send(fd);
});
}
return [];
}
eventPreventDefault(e);
items = getClipboardFileData( getClipboardData( e ) );
return false;
}, true );
@Lewiscowles1986
Copy link
Author

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