-
-
Save davalapar/c3364e3ff8db61dd133c948fa030f56f to your computer and use it in GitHub Desktop.
Anonymous Upload images to Imgur V3 JS
This file contains hidden or 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