Sleazy Fork is available in English.

E621 Quick Download

Adds A Quick Download Button

当前为 2022-08-24 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         E621 Quick Download
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds A Quick Download Button
// @author       rafa_br34#9060
// @match        https://e621.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=e621.net
// @grant        GM.xmlHttpRequest
// @license      GPL-2.0
// ==/UserScript==

(async function () {
    const E621_Images = "https://static1.e621.net/data"



    var g_DataElement = undefined
    var g_Buttons = []
    var g_TotalDownloads = 0
    var g_DownloadQueue = []

    function Sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    function RequestWriteFile(Data, FileName) {
        var TempElement = document.createElement("a");
        var URL = window.URL.createObjectURL(Data/*new Blob([Data], "octet/stream")*/)
        TempElement.setAttribute("href", URL);
        TempElement.setAttribute("download", FileName);
        document.body.appendChild(TempElement);
        TempElement.click();
        document.body.removeChild(TempElement);
        window.URL.revokeObjectURL(URL)

    }


    


    function DownloadImage(ImageHashMD5, FileFormat, SelfId) {
        var FirstByte = ImageHashMD5.substr(0, 2)
        var SecondByte = ImageHashMD5.substr(2, 2)

        // CDN/FirstByte/SecondByte/ImageHash.FileFormat
        var Url = `${E621_Images}/${FirstByte}/${SecondByte}/${ImageHashMD5}.${FileFormat}`
        //console.log(Buttons[SelfId])
        g_Buttons[SelfId][1].innerHTML = "Downloading..."
        
        
        GM.xmlHttpRequest({
            binary: true,
            method: "GET",
            responseType: "blob",
            url: Url,
            onload: async function (Response) {
                console.log(Response)
                try {
                    if (Response.status != 200) {
                        g_Buttons[SelfId][1].innerHTML = "Failed(Http), Retrying..."
                        await Sleep(1000)
                        DownloadImage(ImageHashMD5, FileFormat, SelfId)
                    }
                    else {
                        g_Buttons[SelfId][1].innerHTML = "Done!"
                        g_DownloadQueue.push([`${ImageHashMD5}.${FileFormat}`, Response.response])
                        g_TotalDownloads++
                        await Sleep(1000)
                        g_Buttons[SelfId][1].innerHTML = "Download"
                    }
                }
                catch (Exception) {
                    //console.log(Exception)
                    g_Buttons[SelfId][1].innerHTML = "Failed(Exception), Retrying..."
                    await Sleep(1000)
                    DownloadImage(ImageHashMD5, FileFormat, SelfId)
                }

                
            }
        });

    }

    var RootDiv = document.createElement("div")
    var Posts = document.getElementById("posts")
    Posts.insertBefore(RootDiv, Posts.firstChild)

    var DownloadAllButton = document.createElement("button")
    RootDiv.appendChild(DownloadAllButton)
    DownloadAllButton.innerHTML = "Download All In Page"
    DownloadAllButton.setAttribute("style", "user-select: auto; border-radius: 0px; background-color: rgb(123, 37, 71); color: #ffffff;")

    DownloadAllButton.addEventListener("click",
        () => {
            for (Button of g_Buttons) {
                Button[1].click()
            }
        }
    )

    var g_DataElement = document.createElement("div")

    RootDiv.appendChild(g_DataElement);


    var Runner = async function () {
        while (true) {
            if (g_DownloadQueue.length > 0) {
                var Data = g_DownloadQueue.pop()
                RequestWriteFile(Data[1], Data[0])
            }
            await Sleep(100);
        }
    }
    Runner();

    while (true) {
        for (var Object of document.getElementsByTagName("article")) {
            var NewButton = null


            if (g_Buttons.find((Button) => { return Button[0] == Object })) {
                continue
            }
            

            const ImageHash = Object.getAttribute("data-md5");
            const FileFormat = Object.getAttribute("data-file-ext");
            const TableSize = g_Buttons.length

            if (!GM || !GM.xmlHttpRequest) {
                NewButton = document.createElement("a")
                NewButton.setAttribute("href", `${E621_Images}/${ImageHash.substr(0, 2)}/${ImageHash.substr(2, 2)}/${ImageHash}.${FileFormat}`)
                NewButton.setAttribute("download", `${ImageHash}.${FileFormat}`)
                NewButton.setAttribute("target", "_blank")
            }
            else {
                NewButton = document.createElement("button")
                NewButton.addEventListener("click", () => { DownloadImage(ImageHash, FileFormat, TableSize) })
                NewButton.setAttribute("style", "user-select: auto; border-radius: 0px; background-color: rgb(37, 71, 123); color: #ffffff;")
            }

            NewButton.innerHTML = "Download"
            Object.appendChild(NewButton)
            g_Buttons.push([Object, NewButton])
        }

        g_DataElement.innerHTML = ` Posts: ${g_Buttons.length} Total Downloads: ${g_TotalDownloads} Queued: ${g_DownloadQueue.length}`
        
        await Sleep(1000);
    }



})();