script.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. class DownloadTool {
  2. constructor() {
  3. this.form = document.getElementById('downloadForm');
  4. this.output = document.getElementById('output');
  5. this.loadUrlsBtn = document.getElementById('loadUrls');
  6. this.urlListTextarea = document.getElementById('urlList');
  7. this.downloadUrlBtn = document.getElementById('downloadUrl');
  8. this.cleanFilesBtn = document.getElementById('cleanFiles');
  9. this.downloadImageBtn = document.getElementById('downloadImage');
  10. this.checkIncompleteBtn = document.getElementById('checkIncomplete');
  11. this.clearOutputBtn = document.getElementById('clearOutput');
  12. this.proxySelect = document.getElementById('proxy');
  13. this.initEvents();
  14. }
  15. initEvents() {
  16. // 读取URL按钮
  17. this.loadUrlsBtn.addEventListener('click', () => {
  18. this.loadTargetUrls();
  19. });
  20. // 下载URL按钮
  21. this.downloadUrlBtn.addEventListener('click', () => {
  22. this.downloadUrls()
  23. });
  24. // 下载图片按钮
  25. this.downloadImageBtn.addEventListener('click', () => {
  26. this.downloadImages()
  27. });
  28. // 检查未完成按钮
  29. this.checkIncompleteBtn.addEventListener('click', () => {
  30. this.checkIncomplete();
  31. });
  32. // 清理文件按钮
  33. this.cleanFilesBtn.addEventListener('click', () => {
  34. this.cleanFiles();
  35. });
  36. // 清除输出按钮
  37. this.clearOutputBtn.addEventListener('click', () => {
  38. this.clearOutput();
  39. });
  40. }
  41. async loadTargetUrls() {
  42. try {
  43. this.showOutput('正在读取 targets.txt...', '');
  44. const response = await fetch('/load_urls', {
  45. method: 'POST'
  46. });
  47. const result = await response.json();
  48. if (result.success) {
  49. // 在URL列表文本框中显示读取的URL
  50. this.urlListTextarea.value = result.urls.join('\n');
  51. this.showOutput(`成功读取 ${result.urls.length} 个URL`, 'success');
  52. } else {
  53. this.showOutput(`读取失败: ${result.message}`, 'error');
  54. }
  55. } catch (error) {
  56. this.showOutput(`读取URL时出错: ${error.message}`, 'error');
  57. }
  58. }
  59. async clearOutput() {
  60. try {
  61. const response = await fetch('/clear', {
  62. method: 'POST'
  63. });
  64. const result = await response.json();
  65. if (result.success) {
  66. this.showOutput('', 'success');
  67. this.urlListTextarea.value = ''; // 同时清空URL列表
  68. }
  69. } catch (error) {
  70. this.showOutput(`清除失败: ${error.message}`, 'error');
  71. }
  72. }
  73. async downloadUrls() {
  74. const proxy = this.proxySelect.value;
  75. this.showOutput('正在抓取画廊链接...', 'info');
  76. const res = await fetch('/download_urls', {
  77. method: 'POST',
  78. headers: { 'Content-Type': 'application/json' },
  79. body: JSON.stringify({ proxy })
  80. });
  81. const data = await res.json();
  82. this.showOutput(data.message, data.success ? 'success' : 'error');
  83. }
  84. async downloadImages() {
  85. const proxy = this.proxySelect.value;
  86. this.showOutput('正在下载图片...', 'info');
  87. const res = await fetch('/download_images', {
  88. method: 'POST',
  89. headers: { 'Content-Type': 'application/json' },
  90. body: JSON.stringify({ proxy })
  91. });
  92. const data = await res.json();
  93. this.showOutput(data.message, data.success ? 'success' : 'error');
  94. }
  95. async checkIncomplete() {
  96. try {
  97. this.showOutput('正在检查未完成文件...', 'info');
  98. const response = await fetch('/check_incomplete', {
  99. method: 'POST'
  100. });
  101. const result = await response.json();
  102. if (result.success) {
  103. // 这里先显示后端返回的测试数据,等您完成后端逻辑后会返回实际数据
  104. let message = `检查完成!\n\n`;
  105. message += `返回数据: ${JSON.stringify(result.data, null, 2)}`;
  106. this.showOutput(message, 'success');
  107. } else {
  108. this.showOutput(`检查失败: ${result.message}`, 'error');
  109. }
  110. } catch (error) {
  111. this.showOutput(`检查未完成文件时出错: ${error.message}`, 'error');
  112. }
  113. }
  114. async cleanFiles() {
  115. try {
  116. this.showOutput('正在清理日志和JSON文件...', 'info');
  117. const response = await fetch('/clean_files', {
  118. method: 'POST'
  119. });
  120. const result = await response.json();
  121. if (result.success) {
  122. let message = `清理完成!成功删除 ${result.deleted_count} 个文件\n\n`;
  123. if (result.deleted_files && result.deleted_files.length > 0) {
  124. message += "已删除的文件:\n" + result.deleted_files.join('\n');
  125. }
  126. this.showOutput(message, 'success');
  127. } else {
  128. let message = `清理完成,但有 ${result.error_count} 个文件删除失败\n\n`;
  129. if (result.deleted_files && result.deleted_files.length > 0) {
  130. message += "已删除的文件:\n" + result.deleted_files.join('\n') + '\n\n';
  131. }
  132. if (result.error_files && result.error_files.length > 0) {
  133. message += "删除失败的文件:\n" + result.error_files.join('\n');
  134. }
  135. this.showOutput(message, 'error');
  136. }
  137. } catch (error) {
  138. this.showOutput(`清理文件时出错: ${error.message}`, 'error');
  139. }
  140. }
  141. showOutput(message, type = '') {
  142. this.output.textContent = message;
  143. this.output.className = 'output-area';
  144. if (type) {
  145. this.output.classList.add(type);
  146. }
  147. // 自动滚动到底部
  148. this.output.scrollTop = this.output.scrollHeight;
  149. }
  150. setLoading(loading) {
  151. const buttons = this.form.querySelectorAll('button');
  152. buttons.forEach(button => {
  153. button.disabled = loading;
  154. });
  155. if (loading) {
  156. document.body.classList.add('loading');
  157. } else {
  158. document.body.classList.remove('loading');
  159. }
  160. }
  161. }
  162. // 初始化应用
  163. document.addEventListener('DOMContentLoaded', () => {
  164. new DownloadTool();
  165. });