Civitai Text Downloader Mod

Make Donload button click to save a description text file.

目前為 2024-07-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

(function() {
    'use strict';
    setInterval(function(){
        document.querySelectorAll('main a[type^="button"]').forEach(button => {
            if(!button.classList.contains("ctd-done")){
                button.addEventListener("click", function(){
                    let _id = location.pathname.split("/")[2];

                    const codeElements = document.querySelectorAll('table code');
                    let file_id = null;
                    codeElements.forEach((element, index) => {
                        if (element.textContent === '@') {
                            if (codeElements[index + 1]) {
                                file_id = codeElements[index + 1].textContent;
                            }
                        }
                    });

                    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": "",
                            "sd version": "Unknown",
                            "activation text": "",
                            "preferred weight": 0,
                            "notes": document.URL
                        };

                        if (/^SD 1/.test(file.baseModel)) {
                            text["sd version"] = "SD1";
                        } else if (/^SD 2/.test(file.baseModel)) {
                            text["sd version"] = "SD2";
                        } else if (/^SDXL/.test(file.baseModel) || /^Pony/.test(file.baseModel)) {
                            text["sd version"] = "SDXL";
                        } else if (/^SD 3/.test(file.baseModel)) {
                            text["sd version"] = "SD3";
                        }

                        if(j.description && j.description.textContent){
                            text.description = j.description.textContent;
                        }
                        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);
})();