LiblibAI Helper

Download model, preview, model description by one-click

目前为 2023-12-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         LiblibAI Helper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Download model, preview, model description by one-click
// @author       Sleepy & NewBing
// @match        https://www.liblib.art/modelinfo/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create a MutationObserver to monitor DOM changes
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                var generateButton = document.querySelector('.ModelActionCard_runPic__0I9wi');
                if (generateButton && !document.querySelector('.one-click-download')) {
                    var downloadButton = document.createElement('div');
                    downloadButton.innerHTML = '一键下载';
                    downloadButton.className = 'one-click-download';
                    downloadButton.onclick = autoDownload;
                    generateButton.parentNode.insertBefore(downloadButton, generateButton.nextSibling);
                    observer.disconnect();
                }
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });

    function autoDownload() {
        var version = getSelectedTabName();
        var modelName = document.querySelector('.ModelInfoHead_title__p5txd').innerText;

        modelName += "_" + version

        downloadModel();
        saveAsHTML(modelName);
        // saveAsMarkdown(modelName);
        saveAsPlainText(modelName);
        downloadImages(modelName)
    }

    function getSelectedTabName() {
        // 获取所有的tab
        var tabs = document.querySelectorAll('.ant-tabs-tab');

        // 遍历所有的tab
        for (var i = 0; i < tabs.length; i++) {
            // 检查tab是否被选中
            if (tabs[i].classList.contains('ant-tabs-tab-active')) {
                // 获取tab的标题
                var title = tabs[i].textContent;
                return title;  // 返回标题
            }
        }
    }

    function downloadModel() {
        var downloadButton = document.querySelector('.ModelActionCard_inner__XBdzk');
        if (downloadButton) {
            downloadButton.click();
        }
    }

    async function downloadImages(modelName) {
        var images = document.querySelectorAll('img');
        var count = 0;  // 添加一个新的变量来计数需要下载的图片
        if (images.length > 0) {
            for (var i = 0; i < images.length; i++) {
                // 只处理src属性是URL的img元素
                if (images[i].src.startsWith('http')) {
                    var response = await fetch(images[i].src);
                    var blob = await response.blob();
                    var url = window.URL.createObjectURL(blob);
                    var link = document.createElement('a');
                    link.href = url;
                    link.download = modelName + (count === 0 ? '' : '_preview_' + count) + '.png';  // 使用count而不是i
                    link.style.display = 'none';
                    document.body.appendChild(link);
                    // 使用 setTimeout 函数来延迟点击事件
                    await new Promise(resolve => setTimeout(function() {
                        link.click();
                        document.body.removeChild(link);
                        window.URL.revokeObjectURL(url);
                        resolve();
                    }, 1000));  // 这里的 1000 是延迟的毫秒数,你可以根据需要调整这个值
                    count++;  // 每下载一张图片,就增加count的值
                }
            }
        }
    }

    /*
    function downloadImages(modelName) {
        var images = document.querySelectorAll('.flex img');
        if (images.length > 0) {
            fetch(images[0].src)
                .then(response => response.blob())
                .then(blob => {
                var url = window.URL.createObjectURL(blob);
                var link = document.createElement('a');
                link.href = url;
                link.download = modelName + '.png';
                link.click();
            });
            for (var i = 1; i < images.length; i++) {
                fetch(images[i].src)
                    .then(response => response.blob())
                    .then(blob => {
                    var url = window.URL.createObjectURL(blob);
                    var link = document.createElement('a');
                    link.href = url;
                    link.download = modelName + '_preview_' + i + '.png';
                    window.setTimeout(function() {
                        link.click();
                        window.URL.revokeObjectURL(url);
                    }, 0);
                });
            }
        }
    }*/

    function selectReadme() {
        var mainElement = document.querySelector('.mantine-AppShell-main');
        return mainElement.querySelector('[class^="ModelDescription_desc"]');
    }

    function saveAsHTML(modelName) {
        var descriptionElement = selectReadme();
        if (descriptionElement) {
            var htmlText = descriptionElement.innerHTML;
            var blob = new Blob([htmlText], {type: 'text/html'});
            var url = window.URL.createObjectURL(blob);
            var link = document.createElement('a');
            link.href = url;
            link.download = modelName + '.html';
            link.style.display = 'none';
            document.body.appendChild(link);
            console.log('Attempting to download HTML file:', link.download);
            link.click();
            document.body.removeChild(link);
        } else {
            console.log('Description element not found.');
        }
    }

    function saveAsMarkdown(modelName) {
        var descriptionElement = selectReadme();
        if (descriptionElement) {
            var markdownText = convertHtmlToMarkdown(descriptionElement.innerHTML);
            var blob = new Blob([markdownText], {type: 'text/markdown'});
            var url = window.URL.createObjectURL(blob);
            var link = document.createElement('a');
            link.href = url;
            link.download = modelName + '.md';
            link.style.display = 'none';
            document.body.appendChild(link);
            console.log('Attempting to download markdown file:', link.download);
            link.click();
            document.body.removeChild(link);
        } else {
            console.log('Description element not found.');
        }
    }

    function saveAsPlainText(modelName) {
        var descriptionElement = selectReadme();
        if (descriptionElement) {
            var plainText = descriptionElement.innerText;
            var blob = new Blob([plainText], {type: 'text/plain'});
            var url = window.URL.createObjectURL(blob);
            var link = document.createElement('a');
            link.href = url;
            link.download = modelName + '.txt';
            link.style.display = 'none';
            document.body.appendChild(link);
            console.log('Attempting to download text file:', link.download);
            link.click();
            document.body.removeChild(link);
        } else {
            console.log('Description element not found.');
        }
    }

    function convertHtmlToMarkdown(html) {
        // This is a very basic implementation and might not work for all HTML.
        // Consider using a library like Turndown for a more robust solution.
        var tempDiv = document.createElement('div');
        tempDiv.innerHTML = html;
        return tempDiv.innerText;
    }


})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址