Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Last active June 13, 2022 09:24
Show Gist options
  • Save phatnguyenuit/4d1bc6e533f67bb6d96787e21f75833a to your computer and use it in GitHub Desktop.
Save phatnguyenuit/4d1bc6e533f67bb6d96787e21f75833a to your computer and use it in GitHub Desktop.
Download facebook photos
const fetchedPhotoUrls = [];
function getCurrentPhoto() {
const ele = document.querySelector(
'[data-visualcompletion="media-vc-image"]',
);
return ele.src;
}
async function downloadPhoto(photoUrl) {
const image = await fetch(photoUrl);
const imageBlog = await image.blob();
const imageURL = URL.createObjectURL(imageBlog);
const link = document.createElement('a');
link.href = imageURL;
link.download = /.*\/(.+\.jpg).*/.exec(photoUrl)[1];
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
async function downloadAllPhotos() {
let photoUrl = getCurrentPhoto();
// Loop through all photos
while (!fetchedPhotoUrls.includes(photoUrl)) {
await downloadPhoto(photoUrl);
fetchedPhotoUrls.push(photoUrl);
const nextBtn = document.querySelector('[aria-label="Next photo"]');
nextBtn.click();
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
photoUrl = getCurrentPhoto();
}
}
downloadAllPhotos()
.then(() => {
console.log(`Downloaded all (${fetchedPhotoUrls.length}) photos!`);
})
.catch(console.error);
const fetchedPhotoUrls=[];function getCurrentPhoto(){return document.querySelector('[data-visualcompletion="media-vc-image"]').src}async function downloadPhoto(o){const e=await fetch(o),t=await e.blob(),c=URL.createObjectURL(t),l=document.createElement("a");l.href=c,l.download=/.*\/(.+\.jpg).*/.exec(o)[1],document.body.appendChild(l),l.click(),document.body.removeChild(l)}async function downloadAllPhotos(){let o=getCurrentPhoto();for(;!fetchedPhotoUrls.includes(o);){await downloadPhoto(o),fetchedPhotoUrls.push(o);document.querySelector('[aria-label="Next photo"]').click(),await new Promise((o=>{setTimeout(o,1e3)})),o=getCurrentPhoto()}}downloadAllPhotos().then((()=>{console.log(`Downloaded all (${fetchedPhotoUrls.length}) photos!`)})).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment