Civitai Text Downloader

Make Donload button click to save a description text file.

28.07.2023 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         Civitai Text Downloader
// @namespace    http://tampermonkey.net/
// @version      4
// @description  Make Donload button click to save a description text file.
// @author       SenY
// @match        https://civitai.com/*
// @icon         https://civitai.com/favicon.ico
// @grant        none
// @license      BSD
// ==/UserScript==

(function() {
    'use strict';
    setInterval(function(){
        document.querySelectorAll('*[href^="/api/download/"]').forEach(button => {
            if(!button.classList.contains("ctd-done")){
                button.addEventListener("click", function(){
                    let _id = location.pathname.split("/")[2];
                    let file_id = button.getAttribute("href").split("/")[4];
                    fetch("https://civitai.com/api/v1/models/" + _id).then(x => x.json()).then(j => {
                        let file = j.modelVersions.find(x => x.id == file_id);
                        let link = document.createElement('a');
                        let text = {
                            "description": j.description.textContent,
                            "sd version": {
                                "SD 1.5": "SD1"
                            }[file.baseModel] || "Unknown",
                            "activation text": "",
                            "preferred weight": 0,
                            "notes": document.URL
                        };
                        if(file.trainedWords){
                            text["activation text"] = file.trainedWords.join(" ");
                        }
                        text = [JSON.stringify(text)];
                        link.href = window.URL.createObjectURL(new Blob(text));
                        let filename = file.files.find(x => x).name || file_id + ".json";
                        filename = filename.replace(/\.[a-z]*$/, ".json")
                        link.download = filename;
                        link.click();

                        let image = file.images.find(x => x);
                        if(image){
                            let xhr = new XMLHttpRequest();
                            let src = image.url;
                            let suffix = "." + src.slice(src.lastIndexOf('.') + 1);
                            suffix = ".png"; // URLの拡張子がjpegになってても実際にimage.civitai.comが返す画像はpngっぽいのでハードコーディングで決め打ち
                            xhr.open('GET', src, true);
                            xhr.responseType = "blob";
                            xhr.onload = function () {
                                let dlLink = document.createElement("a");
                                const dataUrl = URL.createObjectURL(this.response);
                                dlLink.href = dataUrl;
                                filename = filename.replace(".json", suffix)
                                dlLink.download = filename;
                                document.body.insertAdjacentElement("beforeEnd", dlLink);
                                dlLink.click();
                                dlLink.remove();
                                setTimeout(function () {
                                    window.URL.revokeObjectURL(dataUrl);
                                }, 1000);
                            };
                            xhr.send();
                        }
                    });
                });
                button.style.color = "#ffff00";
                button.classList.add("ctd-done");
            }
        });
    }, 500);
})();