Greasy Fork

Greasy Fork is available in English.

Facebook Photos Bulk Downloader (Background Support)

Bulk download Facebook album photos in full resolution, supports background downloading

当前为 2024-11-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Facebook Photos Bulk Downloader (Background Support)
// @namespace    https://麦克.github.io
// @version      2.0.0
// @description  Bulk download Facebook album photos in full resolution, supports background downloading
// @author       麦克
// @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
// @grant        GM_notification
// @license      MIT
// ==/UserScript==

let iter = 0;
let MAXITER = 10;
const isDebug = true;
let isDownloading = false;
let stopDownload = false;

// 添加 Fetch 按钮
(function () {
    let timer = setInterval(() => {
        if (document.body && !document.querySelector('#fetch-button')) {
            let fetchBtn = document.createElement("div");
            fetchBtn.id = "fetch-button";
            fetchBtn.textContent = "⇩ Fetch Photos";
            fetchBtn.style.cssText = 'position:fixed;top:10px;right:10px;z-index:9999;padding:8px;background-color:#4caf50;color:white;border-radius:5px;cursor:pointer;';
            fetchBtn.onclick = startBatchDownload;
            document.body.appendChild(fetchBtn);
        }
    }, 1000);
})();

// 获取当前图片 URL
function getImageUrl() {
    const imgElement = document.querySelector('img[src*="scontent"]');
    return imgElement ? imgElement.src : null;
}

// 点击“下一张”按钮
function clickNextImage() {
    const nextBtn = document.querySelector('div[aria-label="下一张"], div[aria-label="Next"]');
    if (nextBtn) {
        nextBtn.click();
        return true;
    } else {
        const complexNextBtn = document.querySelector('div[role="button"][tabindex="0"] div[style*="background-image"]');
        if (complexNextBtn) {
            complexNextBtn.click();
            return true;
        }
    }
    return false;
}

// 下载图片并确保完成后翻页
function downloadImage(imgUrl, callback) {
    if (imgUrl) {
        const filename = imgUrl.split('?')[0].split('/').pop();
        console.log(`Downloading: ${filename}`);
        GM_download({
            url: imgUrl,
            name: filename,
            saveAs: false,
            onload: callback, // 确保下载完成后再翻页
            onerror: callback, // 即使下载失败也继续下一张
        });
    } else {
        console.error('Failed to fetch image URL.');
        callback();
    }
}

// 批量处理函数(支持后台运行)
function batchDownload() {
    if (iter >= MAXITER || stopDownload) {
        console.log('Download limit reached or stopped by user.');
        GM_notification({
            text: `Download completed: ${iter} photos`,
            title: 'Facebook Bulk Downloader',
            timeout: 5000
        });
        return;
    }

    const imgUrl = getImageUrl();
    if (imgUrl) {
        downloadImage(imgUrl, () => {
            iter++;
            if (clickNextImage()) {
                console.log(`Moving to image ${iter + 1}`);
                setTimeout(batchDownload, 1000); // 等待 1 秒后继续下载
            } else {
                console.log('No more images found or failed to click next.');
                GM_notification({
                    text: `Download completed: ${iter} photos`,
                    title: 'Facebook Bulk Downloader',
                    timeout: 5000
                });
            }
        });
    } else {
        console.error('Retry fetching image...');
        setTimeout(batchDownload, 1000);
    }
}

// 开始批量下载
function startBatchDownload() {
    iter = 0;
    isDownloading = true;
    stopDownload = false;
    MAXITER = parseInt(prompt("Enter the number of photos to download:", "10")) || 10;
    console.log(`Starting download of ${MAXITER} photos...`);
    GM_notification({
        text: 'Download started...',
        title: 'Facebook Bulk Downloader',
        timeout: 3000
    });
    batchDownload();
}

// 监听页面切换时继续执行
window.addEventListener('visibilitychange', function () {
    if (document.visibilityState === 'hidden' && isDownloading) {
        console.log('Running in the background...');
    } else if (document.visibilityState === 'visible' && isDownloading) {
        console.log('Resuming download...');
    }
});

// 用户随时可停止下载
document.addEventListener('keydown', function (e) {
    if (e.key === 'Escape' && isDownloading) {
        stopDownload = true;
        console.log('Download stopped by user.');
        GM_notification({
            text: 'Download stopped by user.',
            title: 'Facebook Bulk Downloader',
            timeout: 3000
        });
    }
});