script.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.downloadImageBtn = document.getElementById('downloadImage');
  9. this.clearOutputBtn = document.getElementById('clearOutput');
  10. this.initEvents();
  11. }
  12. initEvents() {
  13. // 读取URL按钮
  14. this.loadUrlsBtn.addEventListener('click', () => {
  15. this.loadTargetUrls();
  16. });
  17. // 下载URL按钮
  18. this.downloadUrlBtn.addEventListener('click', () => {
  19. this.downloadUrls()
  20. });
  21. // 下载图片按钮
  22. this.downloadImageBtn.addEventListener('click', () => {
  23. this.downloadImages()
  24. });
  25. // 清除输出按钮
  26. this.clearOutputBtn.addEventListener('click', () => {
  27. this.clearOutput();
  28. });
  29. }
  30. async loadTargetUrls() {
  31. try {
  32. this.showOutput('正在读取 targets.txt...', '');
  33. const response = await fetch('/load_urls', {
  34. method: 'POST'
  35. });
  36. const result = await response.json();
  37. if (result.success) {
  38. // 在URL列表文本框中显示读取的URL
  39. this.urlListTextarea.value = result.urls.join('\n');
  40. this.showOutput(`成功读取 ${result.urls.length} 个URL`, 'success');
  41. } else {
  42. this.showOutput(`读取失败: ${result.message}`, 'error');
  43. }
  44. } catch (error) {
  45. this.showOutput(`读取URL时出错: ${error.message}`, 'error');
  46. }
  47. }
  48. async clearOutput() {
  49. try {
  50. const response = await fetch('/clear', {
  51. method: 'POST'
  52. });
  53. const result = await response.json();
  54. if (result.success) {
  55. this.showOutput('', 'success');
  56. this.urlListTextarea.value = ''; // 同时清空URL列表
  57. }
  58. } catch (error) {
  59. this.showOutput(`清除失败: ${error.message}`, 'error');
  60. }
  61. }
  62. async downloadUrls() {
  63. const ip = document.getElementById('ip').value;
  64. const port = document.getElementById('port').value;
  65. this.showOutput('正在抓取画廊链接...', 'info');
  66. const res = await fetch('/download_urls', {
  67. method: 'POST',
  68. headers: { 'Content-Type': 'application/json' },
  69. body: JSON.stringify({ ip, port })
  70. });
  71. const data = await res.json();
  72. this.showOutput(data.message, data.success ? 'success' : 'error');
  73. }
  74. async downloadImages() {
  75. const ip = document.getElementById('ip').value;
  76. const port = document.getElementById('port').value;
  77. this.showOutput('正在下载图片...', 'info');
  78. const res = await fetch('/download_images', {
  79. method: 'POST',
  80. headers: { 'Content-Type': 'application/json' },
  81. body: JSON.stringify({ ip, port })
  82. });
  83. const data = await res.json();
  84. this.showOutput(data.message, data.success ? 'success' : 'error');
  85. }
  86. showOutput(message, type = '') {
  87. this.output.textContent = message;
  88. this.output.className = 'output-area';
  89. if (type) {
  90. this.output.classList.add(type);
  91. }
  92. // 自动滚动到底部
  93. this.output.scrollTop = this.output.scrollHeight;
  94. }
  95. setLoading(loading) {
  96. const buttons = this.form.querySelectorAll('button');
  97. buttons.forEach(button => {
  98. button.disabled = loading;
  99. });
  100. if (loading) {
  101. document.body.classList.add('loading');
  102. } else {
  103. document.body.classList.remove('loading');
  104. }
  105. }
  106. }
  107. // 初始化应用
  108. document.addEventListener('DOMContentLoaded', () => {
  109. new DownloadTool();
  110. });