LibImgDown

WEBのダウンロードライブラリ

目前为 2025-03-06 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/528949/1548317/LibImgDown.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. let createFolder = false;
  25. let folderName = "images";
  26. let zipFileName = "download.zip";
  27.  
  28. // elements
  29. let startNumInputElement = null;
  30. let endNumInputElement = null;
  31. let downloadButtonElement = null;
  32. let panelElement = null;
  33. let folderRadioYes = null;
  34. let folderRadioNo = null;
  35. let folderNameInput = null;
  36. let zipFileNameInput = null;
  37.  
  38. // svg icons
  39. 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>`;
  40. 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>`;
  41.  
  42. // initialization
  43. function init({
  44. maxImageAmount,
  45. getImagePromises,
  46. title = `package_${Date.now()}`,
  47. imageSuffix = 'jpg',
  48. zipOptions = {},
  49. positionOptions = {}
  50. }) {
  51. // assign value
  52. maxNum = maxImageAmount;
  53.  
  54. // setup UI
  55. setupUI(positionOptions, title);
  56.  
  57. // setup update notification
  58. setupUpdateNotification();
  59.  
  60. // add click event listener to download button
  61. downloadButtonElement.onclick = function () {
  62. if (!isOKToDownload()) return;
  63.  
  64. this.disabled = true;
  65. this.textContent = "Processing";
  66. this.style.backgroundColor = '#aaa';
  67. this.style.cursor = 'not-allowed';
  68. download(getImagePromises, title, imageSuffix, zipOptions);
  69. };
  70. }
  71.  
  72.  
  73. // setup UI
  74. function setupUI(positionOptions, title) {
  75. // common input element style
  76. const inputElementStyle = `
  77. box-sizing: content-box;
  78. padding: 0px 0px;
  79. width: 40%;
  80. height: 26px;
  81. border: 1px solid #aaa;
  82. border-radius: 4px;
  83. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  84. text-align: center;
  85. `;
  86.  
  87. // create start number input element
  88. startNumInputElement = document.createElement('input');
  89. startNumInputElement.id = 'ImageDownloader-StartNumInput';
  90. startNumInputElement.style = inputElementStyle;
  91. startNumInputElement.type = 'text';
  92. startNumInputElement.value = 1;
  93.  
  94. // create end number input element
  95. endNumInputElement = document.createElement('input');
  96. endNumInputElement.id = 'ImageDownloader-EndNumInput';
  97. endNumInputElement.style = inputElementStyle;
  98. endNumInputElement.type = 'text';
  99. endNumInputElement.value = maxNum;
  100.  
  101. // prevent keyboard input from being blocked
  102. startNumInputElement.onkeydown = (e) => e.stopPropagation();
  103. endNumInputElement.onkeydown = (e) => e.stopPropagation();
  104.  
  105. // create 'to' span element
  106. const toSpanElement = document.createElement('span');
  107. toSpanElement.id = 'ImageDownloader-ToSpan';
  108. toSpanElement.textContent = 'to';
  109. toSpanElement.style = `
  110. margin: 0 6px;
  111. color: black;
  112. line-height: 1;
  113. word-break: keep-all;
  114. user-select: none;
  115. `;
  116.  
  117. // create download button element
  118. downloadButtonElement = document.createElement('button');
  119. downloadButtonElement.id = 'ImageDownloader-DownloadButton';
  120. downloadButtonElement.textContent = 'Download';
  121. downloadButtonElement.style = `
  122. margin-top: 8px;
  123. margin-left: auto; // 追加
  124. width: 128px;
  125. height: 48px;
  126. display: block;
  127. justify-content: center;
  128. align-items: center;
  129. font-size: 14px;
  130. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  131. color: #fff;
  132. line-height: 1.2;
  133. background-color: #0984e3;
  134. border: none;
  135. border-radius: 4px;
  136. cursor: pointer;
  137. `;
  138.  
  139. // create range input container element
  140. const rangeInputContainerElement = document.createElement('div');
  141. rangeInputContainerElement.id = 'ImageDownloader-RangeInputContainer';
  142. rangeInputContainerElement.style = `
  143. display: flex;
  144. justify-content: center;
  145. align-items: baseline;
  146. `;
  147.  
  148. // create panel element
  149. panelElement = document.createElement('div');
  150. panelElement.id = 'ImageDownloader-Panel';
  151. panelElement.style = `
  152. position: fixed;
  153. top: 5px;
  154. left: 5px;
  155. z-index: 999999999;
  156. box-sizing: border-box;
  157. padding: 0px;
  158. width: 220px;
  159. height: auto;
  160. display: flex;
  161. flex-direction: column;
  162. justify-content: center;
  163. align-items: baseline;
  164. font-size: 14px;
  165. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  166. letter-spacing: normal;
  167. background-color: #f1f1f1;
  168. border: 1px solid #aaa;
  169. border-radius: 4px;
  170. `;
  171.  
  172. // modify panel position according to 'positionOptions'
  173. for (const [key, value] of Object.entries(positionOptions)) {
  174. if (key === 'top' || key === 'bottom' || key === 'left' || key === 'right') {
  175. panelElement.style[key] = value;
  176. }
  177. }
  178.  
  179. // create folder radio buttons
  180. folderRadioYes = document.createElement('input');
  181. folderRadioYes.type = 'radio';
  182. folderRadioYes.name = 'createFolder';
  183. folderRadioYes.value = 'yes';
  184. folderRadioYes.id = 'createFolderYes';
  185.  
  186. folderRadioNo = document.createElement('input');
  187. folderRadioNo.type = 'radio';
  188. folderRadioNo.name = 'createFolder';
  189. folderRadioNo.value = 'no';
  190. folderRadioNo.id = 'createFolderNo';
  191. folderRadioNo.checked = true;
  192.  
  193. // フォルダ名入力欄の作成
  194. folderNameInput = document.createElement('input');
  195. folderNameInput.type = 'text';
  196. folderNameInput.id = 'folderNameInput';
  197. folderNameInput.value = title; // titleを初期値として使用
  198. folderNameInput.disabled = true;
  199. folderNameInput.style = `
  200. box-sizing: content-box;
  201. padding: 0px 0px;
  202. width: 95%;
  203. height: 26px;
  204. border: 1px solid #aaa;
  205. border-radius: 1px;
  206. font-size: 12px;
  207. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  208. text-align: left;
  209. `;
  210.  
  211. // ZIPファイル名入力欄の作成
  212. zipFileNameInput = document.createElement('input');
  213. zipFileNameInput.type = 'text';
  214. zipFileNameInput.id = 'zipFileNameInput';
  215. zipFileNameInput.value = `${title}.zip`; // titleを使用してZIPファイル名を設定
  216. zipFileNameInput.style = `
  217. box-sizing: content-box;
  218. padding: 0px 0px;
  219. width: 95%;
  220. height: 26px;
  221. border: 1px solid #aaa;
  222. border-radius: 1px;
  223. font-size: 12px;
  224. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  225. text-align: left;
  226. `;
  227.  
  228. // add event listeners for radio buttons
  229. folderRadioYes.addEventListener('change', () => {
  230. createFolder = true;
  231. folderNameInput.disabled = false;
  232. });
  233.  
  234. folderRadioNo.addEventListener('change', () => {
  235. createFolder = false;
  236. folderNameInput.disabled = true;
  237. });
  238.  
  239. // assemble and then insert into document
  240. rangeInputContainerElement.appendChild(startNumInputElement);
  241. rangeInputContainerElement.appendChild(toSpanElement);
  242. rangeInputContainerElement.appendChild(endNumInputElement);
  243. panelElement.appendChild(rangeInputContainerElement);
  244. panelElement.appendChild(document.createElement('br'));
  245. panelElement.appendChild(folderRadioYes);
  246. panelElement.appendChild(document.createTextNode('フォルダを作成する'));
  247. panelElement.appendChild(folderRadioNo);
  248. panelElement.appendChild(document.createTextNode('フォルダを作成しない'));
  249. panelElement.appendChild(document.createElement('br'));
  250. panelElement.appendChild(document.createTextNode('フォルダ名: '));
  251. panelElement.appendChild(folderNameInput);
  252. panelElement.appendChild(document.createElement('br'));
  253. panelElement.appendChild(document.createTextNode('ZIPファイル名: '));
  254. panelElement.appendChild(zipFileNameInput);
  255. panelElement.appendChild(document.createElement('br'));
  256. panelElement.appendChild(downloadButtonElement);
  257. document.body.appendChild(panelElement);
  258. }
  259.  
  260. // setup update notification
  261. async function setupUpdateNotification() {
  262. if (typeof GM_info === 'undefined' || typeof GM_xmlhttpRequest === 'undefined') return;
  263.  
  264. // get local version
  265. const localVersion = Number(GM_info.script.version);
  266.  
  267. // get latest version
  268. const scriptID = (GM_info.script.homepageURL || GM_info.script.homepage).match(/scripts\/(?<id>\d+)-/)?.groups?.id;
  269. const scriptURL = `https://update.gf.qytechs.cn/scripts/${scriptID}/raw.js`;
  270. const latestVersionString = await new Promise(resolve => {
  271. GM_xmlhttpRequest({
  272. method: 'GET',
  273. url: scriptURL,
  274. responseType: 'text',
  275. onload: res => resolve(res.response.match(/@version\s+(?<version>[0-9\.]+)/)?.groups?.version)
  276. });
  277. });
  278. const latestVersion = Number(latestVersionString);
  279.  
  280. if (Number.isNaN(localVersion) || Number.isNaN(latestVersion)) return;
  281. if (latestVersion <= localVersion) return;
  282.  
  283. // show update notification
  284. const updateLinkElement = document.createElement('a');
  285. updateLinkElement.id = 'ImageDownloader-UpdateLink';
  286. updateLinkElement.href = scriptURL.replace('raw.js', 'raw.user.js');
  287. updateLinkElement.innerHTML = `Update to V${latestVersionString}${externalLinkSVG}`;
  288. updateLinkElement.style = `
  289. position: absolute;
  290. bottom: -38px;
  291. left: -1px;
  292.  
  293. display: flex;
  294. justify-content: space-around;
  295. align-items: center;
  296.  
  297. box-sizing: border-box;
  298. padding: 8px;
  299. width: 146px;
  300. height: 32px;
  301.  
  302. font-size: 14px;
  303. font-family: 'Consolas', 'Monaco', 'Microsoft YaHei';
  304. text-decoration: none;
  305. color: white;
  306.  
  307. background-color: #32CD32;
  308. border-radius: 4px;
  309. `;
  310. updateLinkElement.onclick = () => setTimeout(() => {
  311. updateLinkElement.removeAttribute('href');
  312. updateLinkElement.innerHTML = `Please Reload${reloadSVG}`;
  313. updateLinkElement.style.cursor = 'default';
  314. }, 1000);
  315.  
  316. panelElement.appendChild(updateLinkElement);
  317. }
  318.  
  319. // check validity of page nums from input
  320. function isOKToDownload() {
  321. const startNum = Number(startNumInputElement.value);
  322. const endNum = Number(endNumInputElement.value);
  323.  
  324. if (Number.isNaN(startNum) || Number.isNaN(endNum)) { alert("请正确输入数值\nPlease enter page number correctly."); return false; }
  325. if (!Number.isInteger(startNum) || !Number.isInteger(endNum)) { alert("请正确输入数值\nPlease enter page number correctly."); return false; }
  326. if (startNum < 1 || endNum < 1) { alert("页码的值不能小于1\nPage number should not smaller than 1."); return false; }
  327. if (startNum > maxNum || endNum > maxNum) { alert(`页码的值不能大于${maxNum}\nPage number should not bigger than ${maxNum}.`); return false; }
  328. if (startNum > endNum) { alert("起始页码的值不能大于终止页码的值\nNumber of start should not bigger than number of end."); return false; }
  329.  
  330. return true;
  331. }
  332.  
  333. // start downloading
  334. async function download(getImagePromises, title, imageSuffix, zipOptions) {
  335. const startNum = Number(startNumInputElement.value);
  336. const endNum = Number(endNumInputElement.value);
  337. promiseCount = endNum - startNum + 1;
  338. // start downloading images, max amount of concurrent requests is limited to 4
  339. let images = [];
  340. for (let num = startNum; num <= endNum; num += 4) {
  341. const from = num;
  342. const to = Math.min(num + 3, endNum);
  343. try {
  344. const result = await Promise.all(getImagePromises(from, to));
  345. images = images.concat(result);
  346. } catch (error) {
  347. return; // cancel downloading
  348. }
  349. }
  350.  
  351. // configure file structure of zip archive
  352. JSZip.defaults.date = new Date(Date.now() - (new Date()).getTimezoneOffset() * 60000);
  353. const zip = new JSZip();
  354. folderName = folderNameInput.value;
  355. zipFileName = zipFileNameInput.value;
  356.  
  357. folderName = folderName.trim()
  358. folderName = folderName.replace(/[A-Za-z0-9]/g, function(s) {
  359. return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
  360. });
  361. folderName = folderName.replace(/ /g, ' ');
  362. folderName = folderName.replace(/[!?][!?]/g, '⁉');
  363. folderName = folderName.replace(/[!#$%&()+*]/g, function(s) {
  364. return '!#$%&()+*'['!#$%&()+*'.indexOf(s)];
  365. });
  366. folderName = folderName.replace(/[\\/:*?"<>|]/g, '-');
  367.  
  368. zipFileName = zipFileName.trim()
  369. zipFileName = zipFileName.replace(/[A-Za-z0-9]/g, function(s) {
  370. return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
  371. });
  372. zipFileName = zipFileName.replace(/ /g, ' ');
  373. zipFileName = zipFileName.replace(/[!?][!?]/g, '⁉');
  374. zipFileName = zipFileName.replace(/[!#$%&()+*]/g, function(s) {
  375. return '!#$%&()+*'['!#$%&()+*'.indexOf(s)];
  376. });
  377. zipFileName = zipFileName.replace(/[\\/:*?"<>|]/g, '-');
  378.  
  379. if (createFolder) {
  380. const folder = zip.folder(folderName);
  381. for (const [index, image] of images.entries()) {
  382. const filename = `${String(index + 1).padStart(images.length >= 100 ? String(images.length).length : 2, '0')}.${imageSuffix}`;
  383. folder.file(filename, image, zipOptions);
  384. }
  385. } else {
  386. for (const [index, image] of images.entries()) {
  387. const filename = `${String(index + 1).padStart(images.length >= 100 ? String(images.length).length : 2, '0')}.${imageSuffix}`;
  388. zip.file(filename, image, zipOptions);
  389. }
  390. }
  391.  
  392. // start zipping & show progress
  393. const zipProgressHandler = (metadata) => { downloadButtonElement.innerHTML = `Zipping(${metadata.percent.toFixed()}%)`; };
  394. const content = await zip.generateAsync({ type: "blob" }, zipProgressHandler);
  395. // open 'Save As' window to save
  396. saveAs(content, zipFileName);
  397. // 全て完了
  398. downloadButtonElement.textContent = "Completed";
  399.  
  400. // ボタンを再度押せるようにする
  401. downloadButtonElement.disabled = false;
  402. downloadButtonElement.style.backgroundColor = '#0984e3';
  403. downloadButtonElement.style.cursor = 'pointer';
  404. }
  405.  
  406. // handle promise fulfilled
  407. function fulfillHandler(res) {
  408. if (!isErrorOccurred) {
  409. fulfillCount++;
  410. downloadButtonElement.innerHTML = `Processing(${fulfillCount}/${promiseCount})`;
  411. }
  412. return res;
  413. }
  414.  
  415. // handle promise rejected
  416. function rejectHandler(err) {
  417. isErrorOccurred = true;
  418. console.error(err);
  419. downloadButtonElement.textContent = 'Error Occurred';
  420. downloadButtonElement.style.backgroundColor = 'red';
  421. return Promise.reject(err);
  422. }
  423.  
  424. return { init, fulfillHandler, rejectHandler };
  425. })(window);

QingJ © 2025

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