Greasy Fork 还支持 简体中文。

LiblibAI Helper

Download model, preview, model description by one-click

目前為 2023-12-13 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name LiblibAI Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Download model, preview, model description by one-click
  6. // @author Sleepy & NewBing
  7. // @match https://www.liblib.art/modelinfo/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create a MutationObserver to monitor DOM changes
  16. var observer = new MutationObserver(function(mutations) {
  17. mutations.forEach(function(mutation) {
  18. if (mutation.type === 'childList') {
  19. var generateButton = document.querySelector('.ModelActionCard_runPic__0I9wi');
  20. if (generateButton && !document.querySelector('.one-click-download')) {
  21. var downloadButton = document.createElement('div');
  22. downloadButton.innerHTML = '一键下载';
  23. downloadButton.className = 'one-click-download';
  24. downloadButton.onclick = autoDownload;
  25. generateButton.parentNode.insertBefore(downloadButton, generateButton.nextSibling);
  26. observer.disconnect();
  27. }
  28. }
  29. });
  30. });
  31.  
  32. observer.observe(document.body, { childList: true, subtree: true });
  33.  
  34. function autoDownload() {
  35. var version = getSelectedTabName();
  36. var modelName = document.querySelector('.ModelInfoHead_title__p5txd').innerText;
  37.  
  38. modelName += "_" + version
  39.  
  40. downloadModel();
  41. saveAsHTML(modelName);
  42. // saveAsMarkdown(modelName);
  43. saveAsPlainText(modelName);
  44. downloadImages(modelName)
  45. }
  46.  
  47. function getSelectedTabName() {
  48. // 获取所有的tab
  49. var tabs = document.querySelectorAll('.ant-tabs-tab');
  50.  
  51. // 遍历所有的tab
  52. for (var i = 0; i < tabs.length; i++) {
  53. // 检查tab是否被选中
  54. if (tabs[i].classList.contains('ant-tabs-tab-active')) {
  55. // 获取tab的标题
  56. var title = tabs[i].textContent;
  57. return title; // 返回标题
  58. }
  59. }
  60. }
  61.  
  62. function downloadModel() {
  63. var downloadButton = document.querySelector('.ModelActionCard_inner__XBdzk');
  64. if (downloadButton) {
  65. downloadButton.click();
  66. }
  67. }
  68.  
  69. async function downloadImages(modelName) {
  70. var images = document.querySelectorAll('img');
  71. var count = 0; // 添加一个新的变量来计数需要下载的图片
  72. if (images.length > 0) {
  73. for (var i = 0; i < images.length; i++) {
  74. // 只处理src属性是URL的img元素
  75. if (images[i].src.startsWith('http')) {
  76. var response = await fetch(images[i].src);
  77. var blob = await response.blob();
  78. var url = window.URL.createObjectURL(blob);
  79. var link = document.createElement('a');
  80. link.href = url;
  81. link.download = modelName + (count === 0 ? '' : '_preview_' + count) + '.png'; // 使用count而不是i
  82. link.style.display = 'none';
  83. document.body.appendChild(link);
  84. // 使用 setTimeout 函数来延迟点击事件
  85. await new Promise(resolve => setTimeout(function() {
  86. link.click();
  87. document.body.removeChild(link);
  88. window.URL.revokeObjectURL(url);
  89. resolve();
  90. }, 1000)); // 这里的 1000 是延迟的毫秒数,你可以根据需要调整这个值
  91. count++; // 每下载一张图片,就增加count的值
  92. }
  93. }
  94. }
  95. }
  96.  
  97. /*
  98. function downloadImages(modelName) {
  99. var images = document.querySelectorAll('.flex img');
  100. if (images.length > 0) {
  101. fetch(images[0].src)
  102. .then(response => response.blob())
  103. .then(blob => {
  104. var url = window.URL.createObjectURL(blob);
  105. var link = document.createElement('a');
  106. link.href = url;
  107. link.download = modelName + '.png';
  108. link.click();
  109. });
  110. for (var i = 1; i < images.length; i++) {
  111. fetch(images[i].src)
  112. .then(response => response.blob())
  113. .then(blob => {
  114. var url = window.URL.createObjectURL(blob);
  115. var link = document.createElement('a');
  116. link.href = url;
  117. link.download = modelName + '_preview_' + i + '.png';
  118. window.setTimeout(function() {
  119. link.click();
  120. window.URL.revokeObjectURL(url);
  121. }, 0);
  122. });
  123. }
  124. }
  125. }*/
  126.  
  127. function selectReadme() {
  128. var mainElement = document.querySelector('.mantine-AppShell-main');
  129. return mainElement.querySelector('[class^="ModelDescription_desc"]');
  130. }
  131.  
  132. function saveAsHTML(modelName) {
  133. var descriptionElement = selectReadme();
  134. if (descriptionElement) {
  135. var htmlText = descriptionElement.innerHTML;
  136. var blob = new Blob([htmlText], {type: 'text/html'});
  137. var url = window.URL.createObjectURL(blob);
  138. var link = document.createElement('a');
  139. link.href = url;
  140. link.download = modelName + '.html';
  141. link.style.display = 'none';
  142. document.body.appendChild(link);
  143. console.log('Attempting to download HTML file:', link.download);
  144. link.click();
  145. document.body.removeChild(link);
  146. } else {
  147. console.log('Description element not found.');
  148. }
  149. }
  150.  
  151. function saveAsMarkdown(modelName) {
  152. var descriptionElement = selectReadme();
  153. if (descriptionElement) {
  154. var markdownText = convertHtmlToMarkdown(descriptionElement.innerHTML);
  155. var blob = new Blob([markdownText], {type: 'text/markdown'});
  156. var url = window.URL.createObjectURL(blob);
  157. var link = document.createElement('a');
  158. link.href = url;
  159. link.download = modelName + '.md';
  160. link.style.display = 'none';
  161. document.body.appendChild(link);
  162. console.log('Attempting to download markdown file:', link.download);
  163. link.click();
  164. document.body.removeChild(link);
  165. } else {
  166. console.log('Description element not found.');
  167. }
  168. }
  169.  
  170. function saveAsPlainText(modelName) {
  171. var descriptionElement = selectReadme();
  172. if (descriptionElement) {
  173. var plainText = descriptionElement.innerText;
  174. var blob = new Blob([plainText], {type: 'text/plain'});
  175. var url = window.URL.createObjectURL(blob);
  176. var link = document.createElement('a');
  177. link.href = url;
  178. link.download = modelName + '.txt';
  179. link.style.display = 'none';
  180. document.body.appendChild(link);
  181. console.log('Attempting to download text file:', link.download);
  182. link.click();
  183. document.body.removeChild(link);
  184. } else {
  185. console.log('Description element not found.');
  186. }
  187. }
  188.  
  189. function convertHtmlToMarkdown(html) {
  190. // This is a very basic implementation and might not work for all HTML.
  191. // Consider using a library like Turndown for a more robust solution.
  192. var tempDiv = document.createElement('div');
  193. tempDiv.innerHTML = html;
  194. return tempDiv.innerText;
  195. }
  196.  
  197.  
  198. })();