E621 Quick Download

Adds A Quick Download Button

目前為 2022-08-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         E621 Quick Download
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @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_WriteFileQueue = []
    var g_TotalDownloads = 0
    var g_DownloadQueue = 0
    

    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)

    }


    


    async 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}`

               

        var Done = false
        var IsDownloading = false
        var State = [false]
        g_DownloadQueue++;
        while (!Done) {
            g_Buttons[SelfId][1].innerHTML = "Downloading..." 
            IsDownloading = true
            try {
                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..."
                            }
                            else {
                                g_Buttons[SelfId][1].innerHTML = "Done!"
                                State = [true, `${ImageHashMD5}.${FileFormat}`, Response.response]
                            }
                        }
                        catch (Exception) {
                            console.log(Exception)
                            g_Buttons[SelfId][1].innerHTML = "Failed(Exception), Retrying..."
                        }

                        IsDownloading = false
                    },
                    onerror: async function (Response) {
                        g_Buttons[SelfId][1].innerHTML = "Failed(Error), Retrying..."
                        IsDownloading = false
                    },
                    onabort: async function (Response) {
                        g_Buttons[SelfId][1].innerHTML = "Failed(Aborted), Retrying..."
                        IsDownloading = false
                    }
                });
            }
            catch (Exception) {
                console.log(Exception)
                IsDownloading = false
            }

            while (IsDownloading) {
                await Sleep(10)
            }

            if (State[0]) {
                Done = true
                State.splice(0, 1)
                g_WriteFileQueue.push(State)
                g_TotalDownloads++
                g_Buttons[SelfId][1].innerHTML = "Done"
                await Sleep(1000)
                g_Buttons[SelfId][1].innerHTML = "Download"
            }
            else {
                await Sleep(3000)
            }

        }


        g_DownloadQueue--;
    }

    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",
        async () => {
            for (Button of g_Buttons) {
                Button[1].click()
                await Sleep(100);
            }
        }
    )

    var g_DataElement = document.createElement("div")

    RootDiv.appendChild(g_DataElement);


    var Runner = async function () {
        while (true) {
            if (g_WriteFileQueue.length > 0) {
                var Data = g_WriteFileQueue.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} Download Queue: ${g_DownloadQueue} Write Queue: ${g_WriteFileQueue.length}`
        
        await Sleep(1000);
    }



})();