E621 Quick Download

Adds A Quick Download Button

Tính đến 24-08-2022. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
    }



})();