Last active
September 13, 2024 01:58
-
-
Save chriskyfung/6c3e97e31c71493b222e482ed1f15f56 to your computer and use it in GitHub Desktop.
Userscript for batch downloading Facebook photos from facebook with the post link and captions
This file contains 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
// ==UserScript== | |
// @name Facebook photos bulk downloader | |
// @namespace https://chriskyfung.github.io | |
// @version 1.0.0 | |
// @description Bulk download Facebook photos from facebook with the post link and captions | |
// @author Chris KY Fung | |
// @match https://www.facebook.com/*/photos/* | |
// @match https://www.facebook.com/photo.php?* | |
// @match https://www.facebook.com/photo?* | |
// @match https://www.facebook.com/photo/* | |
// @grant GM_download | |
// ==/UserScript== | |
const container = 'div[aria-label="放大"]'; | |
var iter = 0; | |
var MAXITER = 3; | |
const isDebug = false; | |
(function(){ | |
let timer = setInterval(addFetchBtn, 500); | |
function addFetchBtn() { | |
if (document.querySelector(container)) { | |
if (isDebug) console.log('Get the specified container!') ; | |
let myFetchBtn = document.createElement("div"); | |
myFetchBtn.id = "my-fetch-btn"; | |
myFetchBtn.innerHTML = "⇩ Fetch"; | |
myFetchBtn.style.cssText = 'z-index:100;border-radius:15px;background-color:lightgray;padding: 8px 16px;align-self:center;cursor: pointer;'; | |
myFetchBtn.addEventListener("click", callbatchprocess); | |
document.querySelector(container).parentElement.parentElement.prepend(myFetchBtn); | |
clearInterval(timer); | |
} else { | |
if (isDebug) console.error('Cannot query the specified container!') | |
}; | |
}; | |
})(); | |
function getImageUrl(){ | |
return document.querySelector('img[data-visualcompletion]').src; | |
}; | |
function expandThreeDots(){ | |
document.querySelector('[aria-label="可對此貼文採取的動作"]').click(); | |
}; | |
function getDateTime(){ | |
}; | |
function getCaption(){ | |
return document.querySelector("#fbPhotoSnowliftCaption > span").innerText; | |
}; | |
function nextImage(){ | |
document.querySelector('[aria-label="下一張相片"]').click(); | |
iter++; | |
if (iter < MAXITER) { | |
setTimeout(function(){ | |
batchprocess(); | |
}, 1000); | |
}; | |
} | |
function batchprocess(){ | |
expandThreeDots(); | |
let timer = setInterval(processEach, 500); | |
function processEach(){ | |
let imgUrl = getImageUrl(); | |
if (imgUrl) { | |
clearInterval(timer); | |
var filename = imgUrl.match(/\/.+\/(.+)\?/)[1]; | |
console.log(filename) | |
console.log("iter " + iter + ": " + imgUrl + ", " + filename); | |
GM_download({'url': imgUrl, 'name': filename, 'saveAs': false}); | |
setTimeout(function(){ | |
nextImage(); | |
}, 500); | |
} | |
}; | |
}; | |
function callbatchprocess(){ | |
iter = 0; | |
MAXITER = prompt("How many photos to fetch:", "3"); | |
batchprocess(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
相關文章: Tampermonkey自動化批量下載Facebook相片 - 數碼文明推廣教室 - Medium