LiblibAI Helper

Download model, preview, model description by one-click

当前为 2023-12-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name LiblibAI Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Download model, preview, model description by one-click
  6. // @author Sleepy & NewBing
  7. // @license MIT
  8. // @match https://www.liblib.art/modelinfo/*
  9. // @grant GM_download
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function getSelectedTabName() {
  16. // 获取所有的tab
  17. var tabs = document.querySelectorAll('.ant-tabs-tab');
  18.  
  19. // 遍历所有的tab
  20. for (var i = 0; i < tabs.length; i++) {
  21. // 检查tab是否被选中
  22. if (tabs[i].classList.contains('ant-tabs-tab-active')) {
  23. // 获取tab的标题
  24. var title = tabs[i].textContent;
  25. return title; // 返回标题
  26. }
  27. }
  28. }
  29. function getModelName() {
  30. var version = getSelectedTabName();
  31. var modelName = document.querySelector('.ModelInfoHead_title__p5txd').innerText;
  32. modelName += "_" + version;
  33. return modelName;
  34. }
  35.  
  36. // Create a MutationObserver to monitor DOM changes
  37. var observer = new MutationObserver(function(mutations) {
  38. mutations.forEach(function(mutation) {
  39. if (mutation.type === 'childList') {
  40. var generateButton = document.querySelector('.ModelActionCard_runPic__0I9wi');
  41. if (generateButton && !document.querySelector('.one-click-download')) {
  42. var downloadButton = document.createElement('button');
  43. downloadButton.innerHTML = '一键下载';
  44. downloadButton.className = 'one-click-download';
  45. downloadButton.onclick = autoDownload;
  46. var modelName = getModelName();
  47. // 创建"仅下载图片"按钮
  48. var downloadImageButton = document.createElement('button');
  49. downloadImageButton.innerHTML = '仅下载图片';
  50. downloadImageButton.className = 'download-images-only';
  51. downloadImageButton.style = 'margin-left: 10px; display: inline-block;';
  52. downloadImageButton.onclick = function() {
  53. var imageCount = document.querySelector('.image-count-selector').value;
  54. downloadImages(modelName, imageCount);
  55. };
  56.  
  57. // 创建"仅下载文档"按钮
  58. var downloadDocButton = document.createElement('button');
  59. downloadDocButton.innerHTML = '仅下载文档';
  60. downloadDocButton.className = 'download-doc-only';
  61. downloadDocButton.style = 'margin-left: 10px; display: inline-block;';
  62. downloadDocButton.onclick = function() {
  63. recordURL(modelName);
  64. saveAsHTML(modelName);
  65. // saveAsMarkdown(modelName);
  66. saveAsPlainText(modelName);
  67. };
  68.  
  69. // 创建图片数量选择器
  70. var imageCountSelector = document.createElement('input');
  71. imageCountSelector.type = 'number';
  72. imageCountSelector.min = '1';
  73. imageCountSelector.value = '10'; // 默认值为10
  74. imageCountSelector.className = 'image-count-selector';
  75. imageCountSelector.style = 'margin-left: 10px; display: inline-block;';
  76.  
  77. // 将新的按钮和选择器添加到页面上
  78. generateButton.parentNode.insertBefore(downloadButton, generateButton.nextSibling);
  79. generateButton.parentNode.insertBefore(downloadImageButton, downloadButton.nextSibling);
  80. generateButton.parentNode.insertBefore(downloadDocButton, downloadImageButton.nextSibling);
  81. generateButton.parentNode.insertBefore(imageCountSelector, downloadImageButton.nextSibling);
  82. observer.disconnect();
  83. }
  84. }
  85. });
  86. });
  87.  
  88. observer.observe(document.body, { childList: true, subtree: true });
  89.  
  90. function autoDownload() {
  91. var modelName = getModelName();
  92.  
  93. downloadModel();
  94. recordURL(modelName);
  95. saveAsHTML(modelName);
  96. // saveAsMarkdown(modelName);
  97. saveAsPlainText(modelName);
  98. var imageCount = document.querySelector('.image-count-selector').value;
  99. downloadImages(modelName, imageCount);
  100. }
  101.  
  102. function downloadModel() {
  103. var downloadButton = document.querySelector('.ModelActionCard_inner__XBdzk');
  104. if (downloadButton) {
  105. downloadButton.click();
  106. }
  107. }
  108.  
  109. async function downloadImages(modelName, maxImages) {
  110. var images = document.querySelectorAll('img');
  111. var count = 0;
  112. if (images.length > 0) {
  113. for (var i = 0; i < images.length && count < maxImages; i++) {
  114. if (images[i].src.startsWith('https://liblibai-online.vibrou.com/img/')) {
  115. var url = new URL(images[i].src);
  116. var cleanUrl = url.protocol + "//" + url.hostname + url.pathname;
  117. GM_download({
  118. url: cleanUrl,
  119. name: modelName + (count === 0 ? '' : '_preview_' + count) + '.png',
  120. saveAs: true
  121. });
  122. count++;
  123. await new Promise(resolve => setTimeout(resolve, 1000));
  124. }
  125. }
  126. }
  127. }
  128.  
  129. function selectReadme() {
  130. var mainElement = document.querySelector('.mantine-AppShell-main');
  131. return mainElement.querySelector('[class^="ModelDescription_desc"]');
  132. }
  133. function recordURL(modelName) {
  134. var url = window.location.href;
  135. var blob = new Blob([url], {type: 'text/plain'});
  136. var urlObject = window.URL.createObjectURL(blob);
  137. var downloadLink = document.createElement('a');
  138. downloadLink.href = urlObject;
  139. downloadLink.download = modelName + '_URL.txt';
  140. downloadLink.click();
  141. window.URL.revokeObjectURL(urlObject);
  142. }
  143.  
  144. function saveAsHTML(modelName) {
  145. var descriptionElement = selectReadme();
  146. if (descriptionElement) {
  147. var htmlText = descriptionElement.innerHTML;
  148. var blob = new Blob([htmlText], {type: 'text/html'});
  149. var url = window.URL.createObjectURL(blob);
  150. var link = document.createElement('a');
  151. link.href = url;
  152. link.download = modelName + '.html';
  153. link.style.display = 'none';
  154. document.body.appendChild(link);
  155. console.log('Attempting to download HTML file:', link.download);
  156. link.click();
  157. document.body.removeChild(link);
  158. } else {
  159. console.log('Description element not found.');
  160. }
  161. }
  162.  
  163. function saveAsMarkdown(modelName) {
  164. var descriptionElement = selectReadme();
  165. if (descriptionElement) {
  166. var markdownText = convertHtmlToMarkdown(descriptionElement.innerHTML);
  167. var blob = new Blob([markdownText], {type: 'text/markdown'});
  168. var url = window.URL.createObjectURL(blob);
  169. var link = document.createElement('a');
  170. link.href = url;
  171. link.download = modelName + '.md';
  172. link.style.display = 'none';
  173. document.body.appendChild(link);
  174. console.log('Attempting to download markdown file:', link.download);
  175. link.click();
  176. document.body.removeChild(link);
  177. } else {
  178. console.log('Description element not found.');
  179. }
  180. }
  181.  
  182. function saveAsPlainText(modelName) {
  183. var descriptionElement = selectReadme();
  184. if (descriptionElement) {
  185. var plainText = descriptionElement.innerText;
  186. var blob = new Blob([plainText], {type: 'text/plain'});
  187. var url = window.URL.createObjectURL(blob);
  188. var link = document.createElement('a');
  189. link.href = url;
  190. link.download = modelName + '.txt';
  191. link.style.display = 'none';
  192. document.body.appendChild(link);
  193. console.log('Attempting to download text file:', link.download);
  194. link.click();
  195. document.body.removeChild(link);
  196. } else {
  197. console.log('Description element not found.');
  198. }
  199. }
  200.  
  201. function convertHtmlToMarkdown(html) {
  202. // This is a very basic implementation and might not work for all HTML.
  203. // Consider using a library like Turndown for a more robust solution.
  204. var tempDiv = document.createElement('div');
  205. tempDiv.innerHTML = html;
  206. return tempDiv.innerText;
  207. }
  208.  
  209.  
  210. })();