ImageDownloaderLib(Priv)

Version privada.

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/528315/1544571/ImageDownloaderLib%28Priv%29.js

  1. /*
  2. * Dependencies:
  3. *
  4. * GM_info(optional)
  5. * Docs: https://violentmonkey.github.io/api/gm/#gm_info
  6. *
  7. * GM_xmlhttpRequest(optional)
  8. * Docs: https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
  9. *
  10. * JSZIP
  11. * Github: https://github.com/Stuk/jszip
  12. * CDN: https://unpkg.com/jszip@3.7.1/dist/jszip.min.js
  13. *
  14. * FileSaver
  15. * Github: https://github.com/eligrey/FileSaver.js
  16. * CDN: https://unpkg.com/file-saver@2.0.5/dist/FileSaver.min.js
  17. */
  18.  
  19. ;const ImageDownloader = (({ JSZip, saveAs }) => {
  20. let maxNum = 0;
  21. let promiseCount = 0;
  22. let fulfillCount = 0;
  23. let isErrorOccurred = false;
  24.  
  25. // elements
  26. let startNumInputElement = null;
  27. let endNumInputElement = null;
  28. let downloadButtonElement = null;
  29. let panelElement = null;
  30.  
  31. // svg icons
  32. const externalLinkSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentcolor" width="16" height="16"><path fill-rule="evenodd" d="M10.604 1h4.146a.25.25 0 01.25.25v4.146a.25.25 0 01-.427.177L13.03 4.03 9.28 7.78a.75.75 0 01-1.06-1.06l3.75-3.75-1.543-1.543A.25.25 0 0110.604 1zM3.75 2A1.75 1.75 0 002 3.75v8.5c0 .966.784 1.75 1.75 1.75h8.5A1.75 1.75 0 0014 12.25v-3.5a.75.75 0 00-1.5 0v3.5a.25.25 0 01-.25.25h-8.5a.25.25 0 01-.25-.25v-8.5a.25.25 0 01.25-.25h3.5a.75.75 0 000-1.5h-3.5z"></path></svg>`;
  33. const reloadSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentcolor" width="16" height="16"><path fill-rule="evenodd" d="M8 2.5a5.487 5.487 0 00-4.131 1.869l1.204 1.204A.25.25 0 014.896 6H1.25A.25.25 0 011 5.75V2.104a.25.25 0 01.427-.177l1.38 1.38A7.001 7.001 0 0114.95 7.16a.75.75 0 11-1.49.178A5.501 5.501 0 008 2.5zM1.705 8.005a.75.75 0 01.834.656 5.501 5.501 0 009.592 2.97l-1.204-1.204a.25.25 0 01.177-.427h3.646a.25.25 0 01.25.25v3.646a.25.25 0 01-.427.177l-1.38-1.38A7.001 7.001 0 011.05 8.84a.75.75 0 01.656-.834z"></path></svg>`;
  34.  
  35. // initialization
  36. function init({
  37. maxImageAmount,
  38. getImagePromises,
  39. title = `paquete_${Date.now()}`,
  40. imageSuffix = 'webp',
  41. zipOptions = {},
  42. positionOptions = {}
  43. }) {
  44. // assign value
  45. maxNum = maxImageAmount;
  46.  
  47. // setup UI
  48. setupUI(positionOptions);
  49.  
  50. // add click event listener to download button
  51. downloadButtonElement.onclick = function () {
  52. if (!isOKToDownload()) return;
  53.  
  54. this.disabled = true;
  55. this.textContent = "Procesando";
  56. this.style.backgroundColor = '#aaa';
  57. this.style.cursor = 'not-allowed';
  58. download(getImagePromises, title, imageSuffix, zipOptions);
  59. }
  60. }
  61.  
  62. // setup UI
  63. function setupUI(positionOptions) {
  64. // common input element style
  65. const inputElementStyle = `
  66. box-sizing: content-box;
  67. padding: 1px 2px;
  68. width: 40%;
  69. height: 26px;
  70.  
  71. border: 1px solid #aaa;
  72. border-radius: 4px;
  73.  
  74. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  75. text-align: center;
  76. `;
  77.  
  78. // create start number input element
  79. startNumInputElement = document.createElement('input');
  80. startNumInputElement.id = 'ImageDownloader-StartNumInput';
  81. startNumInputElement.style = inputElementStyle;
  82. startNumInputElement.type = 'text';
  83. startNumInputElement.value = 1;
  84.  
  85. // create end number input element
  86. endNumInputElement = document.createElement('input');
  87. endNumInputElement.id = 'ImageDownloader-EndNumInput';
  88. endNumInputElement.style = inputElementStyle;
  89. endNumInputElement.type = 'text';
  90. endNumInputElement.value = maxNum;
  91.  
  92. // prevent keyboard input from being blocked
  93. startNumInputElement.onkeydown = (e) => e.stopPropagation();
  94. endNumInputElement.onkeydown = (e) => e.stopPropagation();
  95.  
  96. // create 'to' span element
  97. const toSpanElement = document.createElement('span');
  98. toSpanElement.id = 'ImageDownloader-ToSpan';
  99. toSpanElement.textContent = 'a';
  100. toSpanElement.style = `
  101. margin: 0 6px;
  102. color: black;
  103. line-height: 1;
  104. word-break: keep-all;
  105. user-select: none;
  106. `;
  107.  
  108. // create download button element
  109. downloadButtonElement = document.createElement('button');
  110. downloadButtonElement.id = 'ImageDownloader-DownloadButton';
  111. downloadButtonElement.textContent = 'Descargar';
  112. downloadButtonElement.style = `
  113. margin-top: 8px;
  114. width: 128px;
  115. height: 48px;
  116.  
  117. display: flex;
  118. justify-content: center;
  119. align-items: center;
  120.  
  121. font-size: 14px;
  122. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  123. color: #fff;
  124. line-height: 1.2;
  125.  
  126. background-color: #0984e3;
  127. border: none;
  128. border-radius: 4px;
  129. cursor: pointer;
  130. `;
  131.  
  132. // create range input container element
  133. const rangeInputContainerElement = document.createElement('div');
  134. rangeInputContainerElement.id = 'ImageDownloader-RangeInputContainer';
  135. rangeInputContainerElement.style = `
  136. display: flex;
  137. justify-content: center;
  138. align-items: baseline;
  139. `;
  140.  
  141. // create panel element
  142. panelElement = document.createElement('div');
  143. panelElement.id = 'ImageDownloader-Panel';
  144. panelElement.style = `
  145. position: fixed;
  146. top: 72px;
  147. left: 72px;
  148. z-index: 999999999;
  149.  
  150. box-sizing: border-box;
  151. padding: 8px;
  152. width: 146px;
  153. height: 106px;
  154.  
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: center;
  158. align-items: baseline;
  159.  
  160. font-size: 14px;
  161. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  162. letter-spacing: normal;
  163.  
  164. background-color: #f1f1f1;
  165. border: 1px solid #aaa;
  166. border-radius: 4px;
  167. `;
  168.  
  169. // modify panel position according to 'positionOptions'
  170. for (const [key, value] of Object.entries(positionOptions)) {
  171. if (key === 'top' || key === 'bottom' || key === 'left' || key === 'right') {
  172. panelElement.style[key] = value;
  173. }
  174. }
  175.  
  176. // assemble and then insert into document
  177. rangeInputContainerElement.appendChild(startNumInputElement);
  178. rangeInputContainerElement.appendChild(toSpanElement);
  179. rangeInputContainerElement.appendChild(endNumInputElement);
  180. panelElement.appendChild(rangeInputContainerElement);
  181. panelElement.appendChild(downloadButtonElement);
  182. document.body.appendChild(panelElement);
  183. }
  184.  
  185. // check validity of page nums from input
  186. function isOKToDownload() {
  187. const startNum = Number(startNumInputElement.value);
  188. const endNum = Number(endNumInputElement.value);
  189.  
  190. if (Number.isNaN(startNum) || Number.isNaN(endNum)) { alert("请正确输入数值\nIntroduzca el número de página correctamente."); return false; }
  191. if (!Number.isInteger(startNum) || !Number.isInteger(endNum)) { alert("请正确输入数值\nIntroduzca el número de página correctamente."); return false; }
  192. if (startNum < 1 || endNum < 1) { alert("页码的值不能小于1\nEl número de página no debe ser inferior a 1."); return false; }
  193. if (startNum > maxNum || endNum > maxNum) { alert(`页码的值不能大于${maxNum}\nEl número de página no debe ser superior a ${maxNum}.`); return false; }
  194. if (startNum > endNum) { alert("起始页码的值不能大于终止页码的值\nEl número de inicio no debe ser mayor que el número de final."); return false; }
  195.  
  196. return true;
  197. }
  198.  
  199. // start downloading
  200. async function download(getImagePromises, title, imageSuffix, zipOptions) {
  201. const startNum = Number(startNumInputElement.value);
  202. const endNum = Number(endNumInputElement.value);
  203. promiseCount = endNum - startNum + 1;
  204.  
  205. // start downloading images, max amount of concurrent requests is limited to 4
  206. let images = [];
  207. for (let num = startNum; num <= endNum; num += 4) {
  208. const from = num;
  209. const to = Math.min(num + 3, endNum);
  210. try {
  211. const result = await Promise.all(getImagePromises(from, to));
  212. images = images.concat(result);
  213. } catch (error) {
  214. return; // cancel downloading
  215. }
  216. }
  217.  
  218. // configure file structure of zip archive
  219. JSZip.defaults.date = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
  220. const zip = new JSZip();
  221. const zipTitle = title // remove some characters
  222. .replaceAll(/\/|\\|\:|\*|\?|\"|\<|\>|\|/g, '')
  223. .replaceAll(/\-\s|\-\-+/g, '')
  224. .replaceAll(/ZonaTMO/g, '')
  225. .replaceAll(/\s+/g, '_')
  226. .replace(/_+$/g, '')
  227. .replace(/^_+/g, '');
  228. const folder = zip.folder(zipTitle);
  229. for (const [index, image] of images.entries()) {
  230. const filename = `${String(index + 1).padStart(images.length >= 100 ? String(images.length).length : 2, '0')}.${imageSuffix}`;
  231. folder.file(filename, image, zipOptions);
  232. }
  233.  
  234. // start zipping & show progress
  235. const zipProgressHandler = (metadata) => { downloadButtonElement.innerHTML = `Empaquetando<br>(${metadata.percent.toFixed()}%)`; }
  236. const content = await zip.generateAsync({ type: "blob" }, zipProgressHandler);
  237.  
  238. // open 'Save As' window to save
  239. saveAs(content, `${zipTitle}.zip`);
  240.  
  241. // all completed
  242. downloadButtonElement.textContent = "Completado";
  243. }
  244.  
  245. // handle promise fulfilled
  246. function fulfillHandler(res) {
  247. if (!isErrorOccurred) {
  248. fulfillCount++;
  249. downloadButtonElement.innerHTML = `Procesando<br>(${fulfillCount}/${promiseCount})`;
  250. }
  251.  
  252. return res;
  253. }
  254.  
  255. // handle promise rejected
  256. function rejectHandler(err) {
  257. isErrorOccurred = true;
  258. console.error(err);
  259.  
  260. downloadButtonElement.textContent = 'Se ha producido un error.';
  261. downloadButtonElement.style.backgroundColor = 'red';
  262.  
  263. return Promise.reject(err);
  264. }
  265.  
  266. return { init, fulfillHandler, rejectHandler };
  267. })(window);

QingJ © 2025

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