Created
April 11, 2016 22:09
-
-
Save smj10j/d2f2331a098b9f5e87c325c0c634d552 to your computer and use it in GitHub Desktop.
Download your Facebook photos in Chrome
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
// Fetch photo URLs | |
urlEls = jQuery('.fbPhotoStarGridElement') | |
urls = [] | |
urlEls.each(function(i, el) { | |
url = $(el).attr('data-starred-src') | |
urls.push(url); | |
}) | |
// Helper for downloading files with a specified filename | |
// https://github.com/smj10j/download-data-uri | |
var isWebkit = 'WebkitAppearance' in document.documentElement.style | |
var downloadDataURI = function(options) { | |
if(!options) { | |
return; | |
} | |
$.isPlainObject(options) || (options = {data: options}); | |
if(!isWebkit) { | |
location.href = options.data; | |
} | |
options.filename || (options.filename = "download." + options.data.split(",")[0].split(";")[0].substring(5).split("/")[1]); | |
options.url || (options.url = "http://download-data-uri.appspot.com/"); | |
$('<form method="post" action="'+options.url+'" style="display:none"><input type="hidden" name="filename" value="'+options.filename+'"/><input type="hidden" name="data" value="'+options.data+'"/></form>').submit().remove(); | |
} | |
var photoCount = urls.length; | |
// Downloads a photo and starts the next download on completion | |
function downloadNextPhoto(photoIndex) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
var reader = new window.FileReader(); | |
reader.readAsDataURL(this.response); | |
reader.onloadend = function() { | |
data = reader.result.split('base64,')[1]; | |
downloadDataURI({ filename: "Facebook Photo " + photoIndex + ".jpeg", data: "data:image/png;base64," + data }); | |
if(photoIndex < photoCount) { | |
downloadNextPhoto(photoIndex+1); | |
} | |
} | |
} | |
} | |
xhr.open('GET', urls[photoIndex]); | |
xhr.responseType = 'blob'; | |
xhr.send(); | |
} | |
// Go! | |
downloadNextPhoto(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment