Greasy Fork

Greasy Fork is available in English.

Fanbox图片下载器

Download Pixiv Fanbox Images.

当前为 2019-03-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fanbox图片下载器
// @name:en      Fanbox Downloader
// @namespace    http://tampermonkey.net/
// @namespace    https://github.com/709924470/pixiv_fanbox_downloader
// @version      beta_1.14.514
// @description  Download Pixiv Fanbox Images.
// @description:en  Download Pixiv Fanbox Images.
// @author       [email protected]
// @match        https://www.pixiv.net/fanbox/creator/*/post/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';
    var dlList = [];
    var observer = new MutationObserver(rootObserver);
    var observeFlag = true;
    observer.observe(document.getElementById("root"), { childList: true });
    var count = 0;
    function rootObserver(mutations) {
        mutations.forEach(function(mutation) {
            for (var i = 0; i < mutation.addedNodes.length; i++){
                if(mutation.addedNodes[i].innerText.includes("点赞")){
                    mainFunc();
                    observeFlag = false;
                }
                if(!observeFlag){
                    break;
                }
                observer.observe(mutation.addedNodes[i],{ childList: true, characterData: true, subtree: true });
            }
        });
    }
    function mainFunc(){
        //observer.disconnect();
        count = 0;
        //console.log("Trying to add a button...");
        var buttons = document.getElementsByTagName("button");
        var button = null;
        for(var i = 0; i < buttons.length; i++){
            var b = buttons[i];
            if(b !== undefined && b.innerText.includes("点赞")){
                button = b;
                break;
            }
        }
        if(button === null){
            console.warn("An error caused by can not find element.");
            return;
        }
        var newButton = document.createElement("button");
        button.classList.forEach(function(item){
            newButton.classList.add(item);
        });
        newButton.id = "dl_images";
        newButton.innerText = "下载";
        newButton.onclick = function(){
            downloadImages(...getAllImageUrl());
        };
        button.parentNode.appendChild(newButton);
    }
    function downloadImages(...urls){
        urls.forEach(function(url){
            forceDownload(url,generateName(url));
        });
        return undefined;
    }
    function getAllImageUrl(){
        var elements = document.getElementsByClassName("lazyloaded");
        var result = [];
        for(var i = 0; i < elements.length; i++){
            var item = elements[i];
            if(item.tagName == "IMG"){
                result.push(item.parentElement.href);
            }
        }
        return result;
    }
    function generateName(url){
        return document.title.split("|")[0] + ( "_" + count++ ) + "." + url.split(".")[url.split(".").length - 1];
    }
    function forceDownload(url, fileName){
        if(dlList.includes(fileName)){
            return;
        }
        dlList.push(fileName);
        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            binary: true,
            responseType: "blob",
            onload: function(response) {
                var urlCreator = window.URL || window.webkitURL;
                var imageUrl = urlCreator.createObjectURL(response.response);
                var tag = document.createElement('a');
                tag.href = imageUrl;
                tag.download = fileName;
                document.body.appendChild(tag);
                tag.click();
                document.body.removeChild(tag);
            }
        });
    }
})();