Skip to content

Instantly share code, notes, and snippets.

@adis-io
Created November 26, 2011 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adis-io/1395907 to your computer and use it in GitHub Desktop.
Save adis-io/1395907 to your computer and use it in GitHub Desktop.
ASYNC
function upload(image){
var xhr = new XMLHttpRequest();
xhr.open('GET', image.srcUrl, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {
var mime = xhr.getResponseHeader('Content-type');
var blob = new WebKitBlobBuilder();
blob.append(xhr.response);
var file = blob.getBlob(mime);
var fd = new FormData();
fd.append("image[0]", file);
xhr.open('POST', 'http://image.torrent.kg/api/upload', true);
xhr.onload = function(e) {
if (this.status == 200) {
var obj=JSON.parse(xhr.responseText)
if (obj.error==false)
{
chrome.tabs.create({url: "http://image.torrent.kg/info/"+obj.data[0].id})
}
else
{
chrome.tabs.create({url: "http://image.torrent.kg"})
}
}
};
xhr.send(fd);
...
}
};
xhr.send();
}
chrome.contextMenus.create(
{
title: "Upload to image.torrent.kg",
contexts:["image"],
onclick: upload
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment