script.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.showOutput('下载URL被点击', 'success');
  20. });
  21. // 下载图片按钮
  22. this.downloadImageBtn.addEventListener('click', () => {
  23. this.showOutput('下载IMG被点击', 'success');
  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. showOutput(message, type = '') {
  63. this.output.textContent = message;
  64. this.output.className = 'output-area';
  65. if (type) {
  66. this.output.classList.add(type);
  67. }
  68. // 自动滚动到底部
  69. this.output.scrollTop = this.output.scrollHeight;
  70. }
  71. setLoading(loading) {
  72. const buttons = this.form.querySelectorAll('button');
  73. buttons.forEach(button => {
  74. button.disabled = loading;
  75. });
  76. if (loading) {
  77. document.body.classList.add('loading');
  78. } else {
  79. document.body.classList.remove('loading');
  80. }
  81. }
  82. }
  83. // 初始化应用
  84. document.addEventListener('DOMContentLoaded', () => {
  85. new DownloadTool();
  86. });