liblib助手

只提供www.liblib.ai保存模型信息

当前为 2024-02-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         liblib助手
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  只提供www.liblib.ai保存模型信息
// @author       You
// @match        https://www.liblib.ai/modelinfo/*
// @grant        none
// @license      
// ==/UserScript==

(function() {
    'use strict';

    function htmlToText(html) {
        var tempDiv = document.createElement('div');
        tempDiv.innerHTML = html;
        var text = '';
        for (var i = 0; i < tempDiv.childNodes.length; i++) {
            if (tempDiv.childNodes[i].nodeName === 'P') {
                text += tempDiv.childNodes[i].textContent + '\n';
            }
        }
        return text;
    }

    // 定义全局变量
    var textDesc, uuid, buildId, webid, modelId, modelName, modelVersionId, downloadUrl;

    function saveTextAsJson() {
        var allElements = document.querySelectorAll('div');
        allElements.forEach(function(element) {
            var classNames = element.className.split(/\s+/);
            for (var i = 0; i < classNames.length; i++) {
                if (classNames[i].startsWith('ModelDescription_desc')) {
                    textDesc = htmlToText(element.innerHTML);
                    textDesc = textDesc.replace(/\\n/g, '\n');
                    break;
                }
            }
        });
        if(textDesc){
            // Get the content of the script element
            var scriptContent = document.getElementById('__NEXT_DATA__').textContent;
            var scriptJson = JSON.parse(scriptContent);

            // Extract uuid, buildId, and webid
            uuid = scriptJson.query.uuid;
            buildId = scriptJson.buildId;
            webid = scriptJson.props.webid;

            //------------

            // Get the current timestamp
            var timestamp0 = Date.now();
            var timestamp1 = Date.now();

            var url_acceptor = "https://liblib-api.vibrou.com/api/www/log/acceptor/f";
            // Construct the URL
            var url = "https://liblib-api.vibrou.com/api/www/model/getByUuid/" + uuid;

            // Send the POST request to url_acceptor
            fetch(url_acceptor, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({timestamp: timestamp0})
            })
                .then(response => response.json())
                .catch(error => console.error('Error:', error));

            // Send the POST request to url
            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({timestamp: timestamp1})
            })
                .then(response => response.json())
                .then(data => {
                console.log("---------------------");
                console.log(data);
                if(data.code===0){
                    modelId = data.data.id
                    modelName = data.data.name;
                    // Add these values to the JSON object
                    var dataRst = {
                        description: textDesc,
                        uuid: uuid,
                        buildId: buildId,
                        webid: webid
                    };

                    var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(dataRst));
                    var downloadAnchorNode = document.createElement('a');
                    downloadAnchorNode.setAttribute("href", dataStr);
                    downloadAnchorNode.setAttribute("download", modelName+".json");
                    document.body.appendChild(downloadAnchorNode); // required for firefox
                    downloadAnchorNode.click();
                    downloadAnchorNode.remove();
                }
                console.log("---------------------");
            })
                .catch(error => console.error('Error:', error));

            //----------------

        }
    }



    var button1 = document.createElement('button');
    button1.textContent = '保存模型信息';
    button1.onclick = saveTextAsJson;
    button1.style.padding = '10px';
    button1.style.width = "100%";
    button1.style.backgroundColor = 'red';
    button1.style.color = 'white';
    button1.style.display = 'none'; // Initially hide the button


    var observer = new MutationObserver(function(mutations) {
        var found = false;
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList' && !found) {
                var allElements = document.querySelectorAll('div');
                allElements.forEach(function(element) {
                    var classNames = element.className.split(/\s+/);
                    for (var i = 0; i < classNames.length; i++) {
                        if (classNames[i].startsWith('ModelDescription_desc')) {
                            found = true;
                            observer.disconnect(); // 停止观察
                            var actionCard = document.querySelector('[class^="ModelActionCard_modelActionCard"]');
                            if (actionCard) {
                                actionCard.parentNode.insertBefore(button1, actionCard);
                                button1.style.display = 'block';
                            }
                            break;
                        }
                    }
                });
            }
        });
    });

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