Skip to content

Instantly share code, notes, and snippets.

@vnys
Last active December 29, 2015 10:18
Show Gist options
  • Select an option

  • Save vnys/7655700 to your computer and use it in GitHub Desktop.

Select an option

Save vnys/7655700 to your computer and use it in GitHub Desktop.
How to get high resolution images from Facebook without using access tokens. Empty functions and settimeout solves a bug in IE where xdr requests are aborted randomly. See http://cypressnorth.com/programming/internet-explorer-aborting-ajax-requests-fixed/ for the full story.
// append redirect=false to the url to get json-encoded data in return
var url = "http://graph.facebook.com/712766212076612/picture?type=large&redirect=false",
img = document.createElement('img'),
pre = document.createElement('pre'),
f = document.createDocumentFragment();
f.appendChild(img);
f.appendChild(pre);
getFacebookImage(url);
document.body.appendChild(f);
function createCORSRequest(url) {
var xhr = new XMLHttpRequest();
if ('withCredentials' in xhr) {
xhr.open('GET', url, true);
} else if (typeof XDomainRequest != 'undefined') {
xhr = new XDomainRequest();
xhr.open('GET', url)
} else {
xhr = null;
}
return xhr;
};
function getFacebookImage(url) {
var xhr = createCORSRequest(url);
if (!xhr) {
return;
}
xhr.onload = transferComplete;
xhr.onerror = transferFailed;
xhr.ontimeout = function(){};
xhr.onprogress = function(){};
setTimeout(function() {
xhr.send();
}, 0);
function transferComplete() {
var newurl = JSON.parse(this.responseText).data.url;
img.setAttribute('src', newurl);
pre.appendChild(document.createTextNode('old url: ' + url + '\nnew url: ' + newurl));
}
function transferFailed() {
console.log('transfer failed');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment