script.js 6.8 KB

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