| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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.cleanFilesBtn = document.getElementById('cleanFiles');
- this.downloadImageBtn = document.getElementById('downloadImage');
- this.checkIncompleteBtn = document.getElementById('checkIncomplete');
- this.clearOutputBtn = document.getElementById('clearOutput');
- this.proxySelect = document.getElementById('proxy');
-
- this.initEvents();
- }
-
- initEvents() {
- // 读取URL按钮
- this.loadUrlsBtn.addEventListener('click', () => {
- this.loadTargetUrls();
- });
-
- // 下载URL按钮
- this.downloadUrlBtn.addEventListener('click', () => {
- this.downloadUrls()
- });
-
- // 下载图片按钮
- this.downloadImageBtn.addEventListener('click', () => {
- this.downloadImages()
- });
- // 检查未完成按钮
- this.checkIncompleteBtn.addEventListener('click', () => {
- this.checkIncomplete();
- });
- // 清理文件按钮
- this.cleanFilesBtn.addEventListener('click', () => {
- this.cleanFiles();
- });
-
- // 清除输出按钮
- 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 proxy = this.proxySelect.value;
-
- this.showOutput('正在抓取画廊链接...', 'info');
- const res = await fetch('/download_urls', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ proxy })
- });
- const data = await res.json();
- this.showOutput(data.message, data.success ? 'success' : 'error');
- }
- async downloadImages() {
- const proxy = this.proxySelect.value;
-
- this.showOutput('正在下载图片...', 'info');
- const res = await fetch('/download_images', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ proxy })
- });
- const data = await res.json();
- this.showOutput(data.message, data.success ? 'success' : 'error');
- }
- async checkIncomplete() {
- try {
- this.showOutput('正在检查未完成文件...', 'info');
-
- const response = await fetch('/check_incomplete', {
- method: 'POST'
- });
-
- const result = await response.json();
-
- if (result.success) {
- // 这里先显示后端返回的测试数据,等您完成后端逻辑后会返回实际数据
- let message = `检查完成!\n\n`;
- message += `返回数据: ${JSON.stringify(result.data, null, 2)}`;
- this.showOutput(message, 'success');
- } else {
- this.showOutput(`检查失败: ${result.message}`, 'error');
- }
- } catch (error) {
- this.showOutput(`检查未完成文件时出错: ${error.message}`, 'error');
- }
- }
- async cleanFiles() {
- try {
- this.showOutput('正在清理日志和JSON文件...', 'info');
-
- const response = await fetch('/clean_files', {
- method: 'POST'
- });
-
- const result = await response.json();
-
- if (result.success) {
- let message = `清理完成!成功删除 ${result.deleted_count} 个文件\n\n`;
- if (result.deleted_files && result.deleted_files.length > 0) {
- message += "已删除的文件:\n" + result.deleted_files.join('\n');
- }
- this.showOutput(message, 'success');
- } else {
- let message = `清理完成,但有 ${result.error_count} 个文件删除失败\n\n`;
- if (result.deleted_files && result.deleted_files.length > 0) {
- message += "已删除的文件:\n" + result.deleted_files.join('\n') + '\n\n';
- }
- if (result.error_files && result.error_files.length > 0) {
- message += "删除失败的文件:\n" + result.error_files.join('\n');
- }
- this.showOutput(message, 'error');
- }
- } catch (error) {
- this.showOutput(`清理文件时出错: ${error.message}`, '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();
- });
|