Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.