fapodrop.com gallery downloader

fapodrop.com gallery downloader. Ads a button on top to Download all imagenes of the gallery (including pagination)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         fapodrop.com gallery downloader
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  fapodrop.com gallery downloader. Ads a button on top to Download all imagenes of the gallery (including pagination)
// @author       ReallyAmateur
// @match        https://fapodrop.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=fapodrop.com
// @grant        none
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.js
// @license MIT

// ==/UserScript==

function getOriginalImageUrl(thumbnailUrl) {
    return thumbnailUrl.replace('thumbnails', 'photo').replace('_thumbnail.jpeg', '.jpeg');
}

async function getImageUrlsFromPage(pageUrl) {
    let response = await fetch(pageUrl);
    let htmlString = await response.text();
    let parser = new DOMParser();
    let doc = parser.parseFromString(htmlString, 'text/html');
    let thumbnails = Array.from(doc.querySelectorAll('img.thumbnail-img'));
    return thumbnails.map(img => getOriginalImageUrl(img.src));
}

async function getImageBlob(url) {
    let response = await fetch(url);
    return await response.blob();
}
function getSafeFilename(filename) {
    return filename.replace(/[\/\\:*?"<>|]/g, '_');
}
async function getAllImageUrls() {
    let pageUrl = window.location.href;
    let allImageUrls = [];
    while (pageUrl) {
        let imageUrls = await getImageUrlsFromPage(pageUrl);
        allImageUrls.push(...imageUrls);

        let response = await fetch(pageUrl);
        let htmlString = await response.text();
        let parser = new DOMParser();
        let doc = parser.parseFromString(htmlString, 'text/html');
        let nextButton = Array.from(doc.querySelectorAll('a.btn.mx-auto.d-inline.page-btn')).reduce((resultado, el) => {
            if (!resultado && el.textContent.includes('Next')) {
                return el;
            }
            return resultado;
        }, null);

        pageUrl = nextButton ? nextButton.href : null;
    }

    let zip = new JSZip();

    for (let i = 0; i < allImageUrls.length; i++) {
        let url = allImageUrls[i];
        let blob = await getImageBlob(url);
        zip.file(`image${i}.jpeg`, blob, {binary:true});
    }

    zip.generateAsync({type:"blob"})
        .then(function(blob) {
            saveAs(blob, getSafeFilename(document.title)+".zip");
        });
}

function generateButton(){
    const button = document.createElement('button');
    button.textContent = 'Download all!';

    button.style.position = 'fixed';
    button.style.top = '0%';
    button.style.left = '50%';
    button.style.transform = 'translate(-50%, 8px)';
    button.style.zIndex = 9999;
    button.onclick = getAllImageUrls
    document.body.appendChild(button);
}


function startGalleryDownloaderScript(){
    if(!!document.querySelectorAll('img.thumbnail-img').length){
        generateButton()
    } else {
        console.log("No image gallery")
    }
}
window.addEventListener('load', startGalleryDownloaderScript);