Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active February 2, 2023 06:21
Show Gist options
  • Save kopiro/4343403c243704509fd6f17a50ef7cbe to your computer and use it in GitHub Desktop.
Save kopiro/4343403c243704509fd6f17a50ef7cbe to your computer and use it in GitHub Desktop.
Download all the photos of a Facebook Album / "Photos of you"
/*
Download all the photos of a Facebook Album / "Photos of you"
If you want to download "the photos that you uploaded",
use the simpler "Your Facebook Information" feature on https://www.facebook.com/settings?tab=your_facebook_information
This script is only useful to download the photos you've been tagged into
You can find this page on: https://www.facebook.com/photos
Make sure you scroll 'til the end of the page, and then paste it into the console
At the end, you'll just have to download the entire page using "File > Save Page As > Web Page, complete"
and you'll have all the photos on your file-system
*/
// Declare a global array in case you want to use it in any other way
window.__PHOTOLIST__ = [];
(function() {
// This is the index of the current thumbnail
var index = 0;
// This is the "src" of the current image - it's here to keep track of the previous src
var src = "";
// This is a stepper variable to make sure we don't load indefinitely
var tooSmallChk = 0;
// How often should check if the image has been loaded - set this to a greater value in slower connections
var FPS = 100;
// The max amount of time we should wait for a photo to load - set this to a greater value in slower connections
var MAX_WAIT = 5000;
// Take all the thumbnails in the page
var thumbnails = document.querySelectorAll('.uiMediaThumb');
// This is the function that will take care of click the next thumbnail
function clickNextPhoto() {
// If we reached the end, print all the img collected and print onto the page
if (index >= thumbnails.length) {
document.write( '<h1>Just save this page now<h1>' + __PHOTOLIST__.map(p => `<img src="${p}" />`).join("") )
return;
}
// Reset vars
tooSmallChk = 0;
// Click the photo and increment counter
thumbnails[index++].click();
// Define the interval fn
var checkerIntv = setInterval(() => {
// Check for the modal and the image inside
var modal = document.querySelector('.fbPhotoSnowliftContainer img.spotlight');
// Check if we have the img and its src
if (modal && modal.src) {
// Check if the src of the image changed since the previous one to avoid that we download same photo again
if (src != modal.src) {
// Check if the photo is big enough to NOT be considered a low-res image or a thumbnail, but avoid checking indefinitely
if (Math.max(modal.naturalWidth, modal.naturalHeight) > 300 || tooSmallChk > (MAX_WAIT / FPS)) {
if (tooSmallChk > (MAX_WAIT / FPS)) {
console.warn("Okay, downloading a small image anyway, it's taking up too long");
}
// Clear the checker fn
clearInterval(checkerIntv);
// Assign the global "src" var to make sure we can match on the next loop
src = modal.src;
console.log(src);
// Push into the global array
window.__PHOTOLIST__.push(src);
// And go to the next!
clickNextPhoto();
} else {
// Keep track of how many times we've been waiting for a high-res photo
tooSmallChk++;
console.warn(`Photo looks like too small (${modal.naturalWidth}, ${modal.naturalHeight}), waiting a bit`);
}
} else {
console.warn("Photo is still the previous one, waiting for the new one to come");
}
} else {
console.warn("No modal found, waiting for it");
}
}, FPS);
};
clickNextPhoto();
})();
@joshenders
Copy link

No longer works as of 2023-02-02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment