download Lib Model With Pic And Info

下载模型时,以我的习惯命名下载的例图和信息,以便模型命名。

// ==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 下载模型时,以我的习惯命名下载的例图和信息,以便模型命名。
// @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;

loadFn();

function loadFn() {
  if (document.querySelector(downloadBtnSelecter) && maxTry !== 0) {
    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(
        "[id^=rc-tabs-0-panel] > div > div > div img",
      ),
    ]
      .map((i) => {
        let s = i.src;
        if (s.includes("?")) {
          s = s.split("?")[0];
        }
        return s;
      })
      .filter((s) => s.match(/png$/));

console.log(imageUrls);

    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);
        });}
    });
  });
}

QingJ © 2025

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