Liblib tool

Download model, preview, model description by one-click

נכון ליום 12-01-2024. ראה הגרסה האחרונה.

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

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

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.

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

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         Liblib tool
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Download model, preview, model description by one-click
// @author       宇泽同学
// @license      MIT
// @match        https://www.liblib.art/modelinfo/*
// @grant        GM_download
// ==/UserScript==

(function () {
    'use strict';

    // 添加按钮和选择器的样式
    var style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = `
    .custom-button {
        background-color: #4CAF50; /* Green */
        color: white;
        width: 100px;
        height: 35px;
        padding: 5px 15px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 5px 2px;
        cursor: pointer;
        border: none;
        border-radius: 20px;
     }
    .image-count-selector {
        width: 65px; /* 边框宽度 */
        height: 35px;
        padding: 5px;
        border: 1px solid #4CAF50; /* Green border */
        border-radius: 20px;
        margin-left: 5px;
        display: inline-block;
        border-width:1.5px 1.5px 1.5px 1.5px
    }
    `;
    document.head.appendChild(style);

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

        // 遍历所有的tab
        for (var i = 0; i < tabs.length; i++) {
            // 获取aria-selected属性
            var isSelected = tabs[i].querySelector('.ant-tabs-tab-btn').getAttribute('aria-selected');

            // 检查tab是否被选中
            if (isSelected === 'true') {
                // 获取tab的标题
                var title = tabs[i].textContent;
                return title;  // 返回标题
            }
        }
    }

    function getModelName() {
        var version = getSelectedTabName();
        var modelName = document.querySelector('.ModelInfoHead_title__p5txd').innerText;
        modelName += "_" + version;
        return modelName;
    }

    // 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('button');
                    downloadButton.innerHTML = '下载全部';
                    downloadButton.className = 'custom-button one-click-download';
                    downloadButton.onclick = autoDownload;

                    // 创建"下载图片"按钮
                    var downloadImageButton = document.createElement('button');
                    downloadImageButton.innerHTML = '下载图片';
                    downloadImageButton.className = 'custom-button download-images-only';
                    downloadImageButton.onclick = function () {
                        var modelName = getModelName();
                        var imageCount = document.querySelector('.image-count-selector').value;
                        downloadImages(modelName, imageCount);
                    };

                    // 创建图片数量选择器
                    var imageCountSelector = document.createElement('input');
                    imageCountSelector.type = 'number';
                    imageCountSelector.min = '1';
                    imageCountSelector.value = '1'; // 默认值为1
                    imageCountSelector.className = 'image-count-selector';

                    // 将"下载图片"按钮和图片数量选择器添加到页面上
                    generateButton.parentNode.insertBefore(downloadButton, generateButton.nextSibling);
                    generateButton.parentNode.insertBefore(downloadImageButton, downloadButton.nextSibling);
                    generateButton.parentNode.insertBefore(imageCountSelector, downloadImageButton.nextSibling);

                    // 创建"下载简介"按钮
                    var downloadDocButton = document.createElement('button');
                    downloadDocButton.innerHTML = '下载简介';
                    downloadDocButton.className = 'custom-button download-doc-only';
                    downloadDocButton.onclick = function () {
                        var modelName = getModelName();
                        saveAsPlainText(modelName);

                    };

                    generateButton.parentNode.insertBefore(downloadDocButton, imageCountSelector.nextSibling);

                    observer.disconnect();
                }
            }
        });
    });

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

    function autoDownload() {
        var modelName = getModelName();
        downloadModel();
        saveAsPlainText(modelName);

        var imageCount = document.querySelector('.image-count-selector').value;
        downloadImages(modelName, imageCount);
    }

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

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

    function recordURL(modelName) {
        var url = window.location.href;
        var blob = new Blob([url], { type: 'text/plain' });
        var urlObject = window.URL.createObjectURL(blob);
        var downloadLink = document.createElement('a');
        downloadLink.href = urlObject;
        downloadLink.download = modelName + '_URL.txt';
        downloadLink.click();
        window.URL.revokeObjectURL(urlObject);
    }

    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 saveAsPlainText(modelName) {
        var descriptionElement = selectReadme();
        if (descriptionElement) {
            var plainText = descriptionElement.innerText;
            // Append the model URL and empty lines
            plainText += "\n\n☑️本模型地址:" + window.location.href + "\n\n";
            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.');
        }
    }

    async function downloadImages(modelName, maxImages) {
        var images = document.querySelectorAll('img');
        var count = 0;
        if (images.length > 0) {
            for (var i = 0; i < images.length && count < maxImages; i++) {
                if (images[i].src.startsWith('https://liblibai-online.vibrou.com/web/image/')) {
                    var url = new URL(images[i].src);
                    var cleanUrl = url.protocol + "//" + url.hostname + url.pathname;
                    GM_download({
                        url: cleanUrl,
                        name: modelName + '.png',
                        saveAs: true
                    });
                    count++;
                    await new Promise(resolve => setTimeout(resolve, 1000));
                }
            }
        }
    }
})();