download Lib Model With Pic And Info

方便下载 lib 模型的时候,同时自动下载作者示例图片,以及保存作者说明。

当前为 2025-04-25 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        download Lib Model With Pic And Info
// @namespace   www.leizingyiu.net
// @match       http*://www.liblib.art/modelinfo/*
// @grant       none
// @version     20250425
// @author      leizingyiu
// @description 方便下载 lib 模型的时候,同时自动下载作者示例图片,以及保存作者说明。
// @license     GNU AGPLv3
// ==/UserScript==

let maxTry = 50,  delay = 500; // 加载页面绑定点击事件的重试次数,和重试间隔 ms

let imgMaxDownloadNum = 5; // 作者例图最大下载数量

let downloadBtnSelecter =
  "div[class^=ModelInfoBody_modelInfoBody] > div.model-info-right > div[class^=ModelActionCard_modelActionCard] > div[class^=ModelActionCard_addDownModelWrap] > div[class^=ModelActionCard_downModel]";

window.onload = loadFn;

function loadFn() {
  if (document.querySelector(downloadBtnSelecter)) {
    main();
    maxTry = 0;
  } else {
    if (maxTry > 0) {
      setTimeout(loadFn, delay);
      maxTry = maxTry - 1;
    }
  }
}

function main() {
  "use strict";

const   titleSelector = "div[class^=ModelInfoHead_modelInfoHead] > div[class^=ModelInfoHead_first] > div > div.flex.w-full.items-center > span",
      baseSelector = "div.ModelInfoBody_modelInfoBody__FaPZ9 > div.model-info-right > div:nth-child(4) > div[class^=ModelDetailCard_body] > div:nth-child(4) > div[class^=ModelDetailCard_value]",
wordsSelector =      "div[class^=ModelInfoBody_modelInfoBody] > div.model-info-right > div:nth-child(4) > div.ModelDetailCard_body__aO14c > div:nth-child(6) > div[class^=ModelDetailCard_value] > div > span";
  const title = document.querySelector(titleSelector    ).innerText,
    base = document      .querySelector(baseSelector)      .innerText.replace(/基础模型/, "")      .replace(/[ \.]/g, ""),
    words = document.querySelector(wordsSelector)?[
      ...document.querySelectorAll(wordsSelector)
    ]
      .map((i) => i.innerText)
      .join(" + "):'',
    libId = window.location.href.replace(/.*modelinfo\/([^\?]*).*/, "$1"),
    txt = [base, words, title, "lib", libId].join(" - ") + " ";

  document.querySelector(downloadBtnSelecter).addEventListener("click", function (event) {
    const fileName = txt + ".html";
    const fileContent = document.querySelector('[class^=ModelDescription_desc]').innerHTML;

    const a = document.createElement("a");
    a.href = "data:text/plain;charset=utf-8," + encodeURIComponent(fileContent);
    a.download = fileName;
    a.style.display = "none";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

    let imageUrls = [
      ...document.querySelectorAll(
        "#rc-tabs-0-panel-19252784 > div > div.flex.flex-nowrap.w-full > div img",
      ),
    ]
      .map((i) => {
        let s = i.src;
        if (s.includes("?")) {
          s = s.split("?")[0];
        }
        return s;
      })
      .filter((s) => s.match(/png/g));

    imageUrls.map((imageUrl, idx) => {
      if (idx >= imgMaxDownloadNum) {
        return;
      }else{
      fetch(imageUrl)
        .then((response) => response.blob())
        .then((blob) => {
          const blobUrl = URL.createObjectURL(blob);
          const aImg = document.createElement("a");
          aImg.href = blobUrl;
          aImg.download = txt + ".png";
          document.body.appendChild(aImg);
          aImg.click();
          document.body.removeChild(aImg);
          URL.revokeObjectURL(blobUrl);
        });}
    });
  });
}