class DownloadTool { constructor() { this.form = document.getElementById('downloadForm'); this.output = document.getElementById('output'); this.loadUrlsBtn = document.getElementById('loadUrls'); this.urlListTextarea = document.getElementById('urlList'); this.downloadUrlBtn = document.getElementById('downloadUrl'); this.downloadImageBtn = document.getElementById('downloadImage'); this.clearOutputBtn = document.getElementById('clearOutput'); this.initEvents(); } initEvents() { // 读取URL按钮 this.loadUrlsBtn.addEventListener('click', () => { this.loadTargetUrls(); }); // 下载URL按钮 this.downloadUrlBtn.addEventListener('click', () => { this.downloadUrls() }); // 下载图片按钮 this.downloadImageBtn.addEventListener('click', () => { this.downloadImages() }); // 清除输出按钮 this.clearOutputBtn.addEventListener('click', () => { this.clearOutput(); }); } async loadTargetUrls() { try { this.showOutput('正在读取 targets.txt...', ''); const response = await fetch('/load_urls', { method: 'POST' }); const result = await response.json(); if (result.success) { // 在URL列表文本框中显示读取的URL this.urlListTextarea.value = result.urls.join('\n'); this.showOutput(`成功读取 ${result.urls.length} 个URL`, 'success'); } else { this.showOutput(`读取失败: ${result.message}`, 'error'); } } catch (error) { this.showOutput(`读取URL时出错: ${error.message}`, 'error'); } } async clearOutput() { try { const response = await fetch('/clear', { method: 'POST' }); const result = await response.json(); if (result.success) { this.showOutput('', 'success'); this.urlListTextarea.value = ''; // 同时清空URL列表 } } catch (error) { this.showOutput(`清除失败: ${error.message}`, 'error'); } } async downloadUrls() { const ip = document.getElementById('ip').value; const port = document.getElementById('port').value; this.showOutput('正在抓取画廊链接...', 'info'); const res = await fetch('/download_urls', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ip, port }) }); const data = await res.json(); this.showOutput(data.message, data.success ? 'success' : 'error'); } async downloadImages() { const ip = document.getElementById('ip').value; const port = document.getElementById('port').value; this.showOutput('正在下载图片...', 'info'); const res = await fetch('/download_images', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ip, port }) }); const data = await res.json(); this.showOutput(data.message, data.success ? 'success' : 'error'); } showOutput(message, type = '') { this.output.textContent = message; this.output.className = 'output-area'; if (type) { this.output.classList.add(type); } // 自动滚动到底部 this.output.scrollTop = this.output.scrollHeight; } setLoading(loading) { const buttons = this.form.querySelectorAll('button'); buttons.forEach(button => { button.disabled = loading; }); if (loading) { document.body.classList.add('loading'); } else { document.body.classList.remove('loading'); } } } // 初始化应用 document.addEventListener('DOMContentLoaded', () => { new DownloadTool(); });