Created
May 20, 2018 10:11
-
-
Save Lewiscowles1986/6cac3472336a1cf009a88bde56a65fe3 to your computer and use it in GitHub Desktop.
Anonymous Upload images to Imgur V3 JS
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
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://apidocs.imgur.com/#949d6cb0-5e55-45f7-8853-8c44a108399c