Fanvue Image Download

directly download images from fanvue

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fanvue Image Download
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  directly download images from fanvue
// @author       x.scape
// @match        https://www.fanvue.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=fanvue.com
// @grant        none
// @license      Apache License, 2.0
// ==/UserScript==

(function() {
    'use strict';
    const ADDON_NAME = "tpmk_fanvue_img_dl";

    function createTailwindScript() {
        let script = document.createElement("script");
        script.setAttribute("src", "https://cdn.tailwindcss.com");
        return script;
    }
    document.querySelector("head").append(createTailwindScript());

    function elementVisible(elem) {
        const { top, left, bottom, right } = elem.getBoundingClientRect();
        const { innerHeight, innerWidth } = window;
        return ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) && ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth));
    }

    function getLoadedImages() {
        return Array.from(document.querySelectorAll("img.MuiBox-root"));
    }

    function reduceLoadedImages() {
        let imgList = getLoadedImages();
        let loaded = [];
        imgList.forEach((img) => {
            if (elementVisible(img) && !(img.classList.contains(".mui-14baudc")))
                loaded.push(img);
        });
        return loaded;
    }

    function downloadBtn() {
        let btn = document.createElement("button");
        btn.setAttribute("type", "button");
        btn.setAttribute("class", "absolute top-4 left-4 backdrop-filter backdrop-blur-sm bg-opacity-10 text-white font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2");
        btn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6"><path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" /></svg>';
        return btn;
    }

    async function download(url, profile) {
        const image = await fetch(url);
        const imageBlob = await image.blob();
        const imageURL = URL.createObjectURL(imageBlob);

        const link = document.createElement("a");
        link.href = imageURL;
        link.download = `${profile}_-_${Date.now()}`;
        link.target = "_blank";

        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }

    function getImageSource(sourceSet) {
        return sourceSet.srcset.split("1x, ")[1].split(" 2x")[0];
    }

    document.addEventListener("scrollend", () => {
        let visibleImages = reduceLoadedImages();
        visibleImages.forEach((img) => {
            if (!img.parentElement.classList.contains("relative")) {
                let btn = downloadBtn();
                img.parentElement.classList.add("relative");
                img.parentElement.append(btn);
                btn.addEventListener("click", (event) => {
                    console.log(event.target.parentElement.closest("div.MuiBox-root"), event.target.parentElement.closest("li.slide"));
                    let parent = event.target.parentElement.closest("li.slide") != null ? event.target.parentElement.closest("li.slide") : event.target.parentElement.closest("div.MuiBox-root");
                    console.log(parent);
                    let img = parent.querySelector("img");
                    let src = getImageSource(img);
                    download(src, document.querySelector("#profileHeader > section > div.MuiBox-root.mui-1kpdnj > p").textContent);
                });
            }
        });
    });
})();