File size: 27,173 Bytes
32d4199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de891cf
32d4199
 
 
79ec663
 
 
 
 
 
 
 
 
 
 
 
32d4199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de891cf
32d4199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// 全局变量
let isParsing = false;
let history = JSON.parse(localStorage.getItem('parseHistory') || '[]');

// 初始化主题
function initTheme() {
    const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");
    const savedTheme = localStorage.getItem('theme') || (prefersDarkScheme.matches ? 'dark' : 'light');
    document.documentElement.setAttribute('data-theme', savedTheme);
    const themeBtn = document.getElementById('theme-btn');
    themeBtn.innerHTML = savedTheme === 'dark' ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
}

// 初始化主题色
function initThemeColor() {
    const themeColors = [
        { primary: '#7c4dff', light: 'rgba(124, 77, 255, 0.2)', dark: '#5e35b1' },
        { primary: '#42a5f5', light: 'rgba(66, 165, 245, 0.2)', dark: '#1976d2' },
        { primary: '#ec407a', light: 'rgba(236, 64, 122, 0.2)', dark: '#d81b60' },
        { primary: '#66bb6a', light: 'rgba(102, 187, 106, 0.2)', dark: '#43a047' },
        { primary: '#ff9800', light: 'rgba(255, 152, 0, 0.2)', dark: '#f57c00' },
    ];
    const colorIndex = parseInt(localStorage.getItem('colorIndex') || '0');
    const color = themeColors[colorIndex];
    document.documentElement.style.setProperty('--primary-color', color.primary);
    document.documentElement.style.setProperty('--primary-light', color.light);
    document.documentElement.style.setProperty('--primary-dark', color.dark);
    document.getElementById('color-btn').onclick = function() {
        const nextIndex = (colorIndex + 1) % themeColors.length;
        localStorage.setItem('colorIndex', nextIndex);
        initThemeColor();
    };
}

// 加载樱花特效
function loadSakuraEffect() {
    const script = document.createElement('script');
    script.src = 'https://api.suyanw.cn/api/mouse/yinghua.js';
    document.body.appendChild(script);
}

// 鱼游动画(CDN引入)
function initFishAnimation() {
    console.log('鱼游动画已通过CDN加载');
}

// 调试信息显示
let debugTimer;
function showDebugInfo(message) {
    const debugEl = document.getElementById('debugInfo');
    debugEl.textContent = message;
    clearTimeout(debugTimer);
    debugTimer = setTimeout(() => debugEl.textContent = '', 5000);
}

// 实时时钟
function updateTime() {
    const now = new Date();
    document.getElementById('currentTime').textContent = 
        `${now.getHours().toString().padStart(2, '0')}:` +
        `${now.getMinutes().toString().padStart(2, '0')}:` +
        `${now.getSeconds().toString().padStart(2, '0')}`;
}

// 获取天气
async function getWeather() {
    try {
        const response = await fetch('https://api.kxzjoker.cn/api/Weather');
        const data = await response.json();

        if (data.code !== 200) throw new Error('天气获取失败');

        document.getElementById('weatherWidget').innerHTML = `
            <i class="fas fa-cloud-sun" style="background: linear-gradient(45deg, #ff9a00, #89CFF0);-webkit-background-clip: text;background-clip: text;color: transparent;"></i>
            ${data.data.tianqi.temperature}°C
        `;

        showDebugInfo(`👏欢迎来自${data.data.ipdata.info.split('-')[0]}的用户`);
    } catch (error) {
        document.getElementById('weatherWidget').innerHTML = `
            <i class="fas fa-cloud-sun" style="background: linear-gradient(45deg, #ff9a00, #89CFF0);-webkit-background-clip: text;background-clip: text;color: transparent;"></i>
            未知天气
        `;
        showDebugInfo('欢迎访问本服务');
    }
}

// 获取随机文案
async function fetchRandomQuote() {
    try {
        const response = await fetch('https://api.suyanw.cn/api/weimei.php?type=json');
        const data = await response.json();
        const quoteEl = document.getElementById('random-quote');
        if (quoteEl) {
            quoteEl.textContent = data.text || "每一个视频背后,都有一个想被分享的故事";
        }
    } catch (error) {
        const quoteEl = document.getElementById('random-quote');
        if (quoteEl) {
            quoteEl.textContent = "每一个视频背后,都有一个想被分享的故事";
        }
        console.error('获取随机文案失败:', error);
    }
}

// 获取并更新背景图片
async function updateBackgroundImage() {
    try {
        const response = await fetch('https://api.suyanw.cn/api/comic3.php');
        if (!response.ok) throw new Error(`背景图片 API 请求失败: ${response.status}`);
        const data = await response.json();
        const imageUrl = data.url || data.image || data.data?.url; // 适配可能的字段
        if (imageUrl) {
            document.body.style.backgroundImage = `url(${imageUrl})`;
            showDebugInfo('背景图片已更新');
        } else {
            throw new Error('背景图片 URL 为空');
        }
    } catch (error) {
        console.error('获取背景图片失败:', error);
        showDebugInfo('背景图片加载失败,将使用默认背景');
        document.body.style.backgroundImage = `url('https://img.zlee.de/file/1747480653744_e852b46603ba8a533c63f5631308f760.jpg')`;
    }
}

// 获取在线访客人数(假设 API)
async function updateOnlineUsers() {
    try {
        const response = await fetch('https://api.example.com/online-users');
        const data = await response.json();
        const onlineUsers = data.count || 0;
        document.getElementById('online-users').textContent = onlineUsers;
    } catch (error) {
        console.error('获取在线人数失败:', error);
        document.getElementById('online-users').textContent = '未知';
    }
}

// URL提取
function extractURL(text) {
    try {
        const urlRegex = /(https?:\/\/.*)/g;
        const matches = text.match(urlRegex);
        return matches ? matches[0].trim() : null;
    } catch (e) {
        console.error('URL提取错误:', e);
        return null;
    }
}

// 监听剪贴板
function monitorClipboard() {
    const videoUrlInput = document.getElementById('urlInput');
    const pasteButton = document.getElementById('clipboard-btn');

    pasteButton.addEventListener('click', async function() {
        try {
            const permissionStatus = await navigator.permissions.query({ name: 'clipboard-read' });
            if (permissionStatus.state === 'denied') {
                throw new Error('无权限访问剪贴板,请手动粘贴');
            }

            const text = await navigator.clipboard.readText();
            if (text) {
                videoUrlInput.value = text;
                const pureUrl = extractURL(text);
                if (pureUrl) {
                    showDebugInfo(`已提取纯链接: ${pureUrl}`);
                    parseContent(pureUrl);
                } else {
                    showAlert('未检测到有效的视频链接,请确保链接包含 http 或 https,或者直接从平台复制分享链接!');
                }
                videoUrlInput.focus();
            } else {
                showAlert('剪贴板为空,请复制链接后重试');
            }
        } catch (error) {
            console.warn('无法访问剪贴板:', error);
            showAlert('无法访问剪贴板,请手动粘贴链接或检查浏览器权限');
            videoUrlInput.focus();
        }
    });

    videoUrlInput.addEventListener('focus', async function() {
        if (this.value === '') {
            try {
                const permissionStatus = await navigator.permissions.query({ name: 'clipboard-read' });
                if (permissionStatus.state === 'denied') {
                    showAlert('无权限访问剪贴板,请手动粘贴');
                    return;
                }

                const text = await navigator.clipboard.readText();
                if (text) {
                    this.value = text;
                    const pureUrl = extractURL(text);
                    if (pureUrl) {
                        showDebugInfo(`自动填充提取的纯链接: ${pureUrl}`);
                    } else {
                        showAlert('未检测到有效的视频链接,请确保链接包含 http 或 https,或者直接从平台复制分享链接!');
                    }
                }
            } catch (error) {
                console.warn('无法自动读取剪贴板:', error);
            }
        }
    });

    videoUrlInput.addEventListener('input', function() {
        const pureUrl = extractURL(this.value);
        if (pureUrl) {
            showDebugInfo(`已检测到有效链接: ${pureUrl}`);
        }
    });
}

// 解析内容
async function parseContent(urlToParse) {
    if (isParsing) return;
    isParsing = true;

    const parseBtn = document.querySelector('.parse-btn');
    parseBtn.disabled = true;
    parseBtn.style.opacity = '0.6';

    showDebugInfo('开始解析流程...');

    try {
        const input = document.getElementById('urlInput');
        if (!input) throw new Error('找不到输入框元素');

        const url = urlToParse || extractURL(input.value);
        showDebugInfo(`提取到URL: ${url || '无'}`);

        if (!url) {
            showAlert('🚨 请输入有效的链接哦~ (´•̥ ̯ •̥`)');
            return;
        }

        toggleLoading(true);

        showDebugInfo(`正在解析中: ${url}`);
        const apiUrl = `https://api.kxzjoker.cn/api/jiexi_video_2?url=${encodeURIComponent(url)}`;
        const response = await fetch(apiUrl);
        showDebugInfo(`收到响应状态: ${response.status}`);

        if (!response.ok) {
            throw new Error(`API请求失败: ${response.status} ${response.statusText}`);
        }

        const data = await response.json();
        showDebugInfo(`API响应数据: ${JSON.stringify(data).slice(0, 100)}...`);

        if (!data.success) {
            throw new Error(`解析失败: ${data.message || '未知错误'}`);
        }

        const { video_title, video_url, download_url, image_url } = data.data;
        showDebugInfo(`解析成功 - 时间: ${data.time}, 备注: ${data.tips}`);

        renderContent(data.data);
        saveToHistory({
            url: url,
            title: video_title || '',
            cover: image_url || ''
        });
        showDebugInfo('内容渲染完成');

        const shareContainer = document.getElementById('share-container');
        shareContainer.style.display = 'block';
    } catch (error) {
        console.error('解析流程错误:', error);
        showDebugInfo(`错误: ${error.message}`);
        showAlert(`❌ 解析失败: ${error.message},请检查链接或稍后重试`);
    } finally {
        toggleLoading(false);
        parseBtn.disabled = false;
        parseBtn.style.opacity = '1';
        isParsing = false;
    }
}

// 渲染内容
function renderContent(data) {
    const contentBox = document.getElementById('contentBox');
    if (!contentBox) throw new Error('找不到内容容器');

    contentBox.innerHTML = '';

    try {
        if (data.images) {
            const galleryHTML = data.images.map((img, index) => `
                <div class="gallery-item" onclick="showFullImage('${img}')">
                    <img src="${img}" 
                         alt="图集 ${index + 1}"
                         loading="lazy"
                         style="width: 100%; 
                                aspect-ratio: 1/1; 
                                object-fit: cover;
                                cursor: pointer;">
                    <div class="image-index">${index + 1}</div>
                </div>
            `).join('');

            contentBox.innerHTML = `
                <div class="media-card">
                    <h2 style="color: var(--primary-color);">
                        <i class="fas fa-images"></i>
                        ${data.title || '未命名图集'}
                    </h2>
                    <div style="display: grid; 
                              gap: 10px; 
                              grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));">
                        ${galleryHTML}
                    </div>
                    <div style="text-align: center; margin-top: 15px;">
                        <a href="#" class="download-all-btn btn">
                            <i class="fas fa-file-archive"></i>
                            打包下载
                        </a>
                    </div>
                </div>
            `;

            contentBox.style.display = 'block';
            setTimeout(() => contentBox.scrollIntoView({ behavior: 'smooth' }), 300);

            const downloadBtn = contentBox.querySelector('.download-all-btn');
            downloadBtn.addEventListener('click', async (e) => {
                e.preventDefault();
                try {
                    downloadBtn.innerHTML = `<i class="fas fa-spinner fa-spin"></i> 准备中...`;
                    downloadBtn.disabled = true;

                    const zip = new JSZip();
                    const imgFolder = zip.folder('images');

                    const downloadPromises = data.images.map(async (imgUrl, index) => {
                        try {
                            const startTime = performance.now();
                            const response = await fetch(imgUrl);
                            if (!response.ok) throw new Error(`图片${index + 1}下载失败: ${response.status}`);
                            const blob = await response.blob();
                            const endTime = performance.now();
                            const duration = (endTime - startTime) / 1000;
                            const speed = (blob.size / 1024) / duration;
                            document.getElementById('progress-info').textContent = `下载速度: ${speed.toFixed(2)} KB/s`;
                            imgFolder.file(`image_${index + 1}.${getFileExtension(imgUrl)}`, blob);
                        } catch (error) {
                            console.warn(`图片${index + 1}下载失败:`, error);
                            return null;
                        }
                    });

                    let completed = 0;
                    downloadPromises.forEach(promise => {
                        promise.finally(() => {
                            completed++;
                            const progress = Math.round((completed / data.images.length) * 100);
                            document.getElementById('progress-bar').style.width = `${progress}%`;
                            document.getElementById('progress-info').textContent = `下载进度: ${progress}%`;
                        });
                    });

                    await Promise.all(downloadPromises);

                    const zipBlob = await zip.generateAsync({ type: 'blob', compression: 'DEFLATE', compressionOptions: { level: 6 } });
                    const cleanTitle = (data.title || '未命名图集').replace(/[<>:"/\\|?*]/g, '');
                    saveAs(zipBlob, `${cleanTitle}.zip`);
                    showAlert('🎉 图集下载完成!');
                } catch (error) {
                    console.error('打包下载失败:', error);
                    showAlert(`❌ 打包下载失败: ${error.message},请重试`);
                } finally {
                    downloadBtn.innerHTML = `<i class="fas fa-file-archive"></i> 打包下载`;
                    downloadBtn.disabled = false;
                }
            });

        } else if (data.video_url) {
            const { video_title, video_url, download_url, image_url } = data;
            contentBox.innerHTML = `
                <div class="media-card">
                    <h2 style="color: var(--primary-color);">
                        <i class="fas fa-video"></i>
                        ${video_title || '未命名视频'}
                    </h2>
                    ${image_url ? `<img src="${image_url}" alt="视频封面" style="width: 100%; border-radius: 15px; margin-bottom: 15px;">` : ''}
                    <div style="position: relative; padding-top: 56.25%;">
                        <video controls 
                               style="position: absolute;
                                      top: 0;
                                      left: 0;
                                      width: 100%;
                                      height: 100%;">
                            <source src="${video_url}" type="video/mp4">
                            您的浏览器不支持视频播放
                        </video>
                    </div>
                    <div style="text-align: center; margin-top: 15px;">
                        <a href="${download_url}" class="download-btn btn" download>
                            <i class="fas fa-download"></i>
                            保存视频
                        </a>
                    </div>
                </div>
            `;

            contentBox.style.display = 'block';
            setTimeout(() => contentBox.scrollIntoView({ behavior: 'smooth' }), 300);

            const downloadBtn = contentBox.querySelector('.download-btn');
            downloadBtn.addEventListener('click', async (e) => {
                e.preventDefault();
                try {
                    downloadBtn.innerHTML = `<i class="fas fa-spinner fa-spin"></i> 下载中...`;
                    downloadBtn.disabled = true;

                    const startTime = performance.now();
                    const response = await fetch(download_url);
                    if (!response.ok) throw new Error(`视频下载失败: ${response.status} ${response.statusText}`);
                    const blob = await response.blob();
                    const endTime = performance.now();
                    const duration = (endTime - startTime) / 1000;
                    const speed = (blob.size / 1024) / duration;
                    document.getElementById('progress-info').textContent = `下载速度: ${speed.toFixed(2)} KB/s`;
                    document.getElementById('progress-bar').style.width = '100%';

                    const fileName = `${video_title || '未命名视频'}.mp4`.replace(/[<>:"/\\|?*]/g, '');
                    saveAs(blob, fileName);
                    showAlert('🎉 视频下载完成!');
                } catch (error) {
                    console.error('下载失败:', error);
                    showAlert(`❌ 视频下载失败: ${error.message},请检查链接或稍后重试`);
                } finally {
                    downloadBtn.innerHTML = `<i class="fas fa-download"></i> 保存视频`;
                    downloadBtn.disabled = false;
                }
            });
        }
    } catch (e) {
        console.error('渲染错误:', e);
        showAlert('内容渲染失败,请检查数据格式');
    }
}

// 显示完整图片
function showFullImage(url) {
    const modal = document.getElementById('image-modal');
    const modalImage = document.getElementById('modal-image');
    modalImage.src = url;
    modal.style.display = 'flex';
}

// 工具函数
function toggleLoading(show) {
    document.getElementById('loading').style.display = show ? 'block' : 'none';
}

function showAlert(message) {
    const alert = document.createElement('div');
    alert.className = 'alert';
    alert.innerHTML = message;
    document.body.appendChild(alert);
    setTimeout(() => alert.remove(), 3000);
}

function getFileExtension(url) {
    try {
        const ext = url.split('.').pop().split(/[#?]/)[0].toLowerCase();
        return ['jpg', 'jpeg', 'png', 'webp', 'gif'].includes(ext) ? ext : 'jpg';
    } catch (e) {
        return 'jpg';
    }
}

function loadHistory() {
    const historyList = document.getElementById('history-list');
    if (history.length === 0) {
        historyList.innerHTML = '<li>暂无解析历史</li>';
        return;
    }
    historyList.innerHTML = '';
    history.forEach((item, index) => {
        const li = document.createElement('li');
        li.className = 'history-item';
        li.innerHTML = `
            <img class="history-thumb" src="${item.cover || 'https://via.placeholder.com/60'}" alt="缩略图">
            <div class="history-info">
                <div class="history-title">${item.title || '未知标题'}</div>
                <div class="history-url">${item.url}</div>
            </div>
            <div class="history-actions">
                <button class="history-btn history-reparse" data-url="${item.url}"><i class="fas fa-redo"></i> 重新解析</button>
                <button class="history-btn history-delete" data-index="${index}"><i class="fas fa-trash"></i> 删除</button>
            </div>
        `;
        historyList.appendChild(li);
    });
    document.querySelectorAll('.history-reparse').forEach(btn => {
        btn.addEventListener('click', function() {
            document.getElementById('urlInput').value = this.getAttribute('data-url');
            parseContent(this.getAttribute('data-url'));
        });
    });
    document.querySelectorAll('.history-delete').forEach(btn => {
        btn.addEventListener('click', function() {
            const index = parseInt(this.getAttribute('data-index'));
            history.splice(index, 1);
            localStorage.setItem('parseHistory', JSON.stringify(history));
            loadHistory();
        });
    });
}

function saveToHistory(data) {
    if (!data || !data.url) return;
    const exists = history.findIndex(item => item.url === data.url);
    if (exists > -1) history.splice(exists, 1);
    history.unshift({ url: data.url, title: data.title || '', cover: data.cover || '', timestamp: new Date().getTime() });
    if (history.length > 10) history = history.slice(0, 10);
    localStorage.setItem('parseHistory', JSON.stringify(history));
    loadHistory();
}

// 分享功能
function copyLink() {
    const url = window.location.href;
    navigator.clipboard.writeText(url).then(() => {
        showAlert('🎉 链接已复制到剪贴板!');
    }).catch(() => {
        showAlert('❌ 复制失败,请手动复制!');
    });
}

function shareToWeChat() {
    const url = window.location.href;
    const shareText = `我发现了一个超好用的短视频无水印解析工具,快来试试吧!${url}`;
    navigator.clipboard.writeText(shareText).then(() => {
        showAlert('🎉 分享内容已复制,请粘贴到微信!');
    });
}

function shareToQQ() {
    const url = window.location.href;
    const shareText = `我发现了一个超好用的短视频无水印解析工具,快来试试吧!${url}`;
    navigator.clipboard.writeText(shareText).then(() => {
        showAlert('🎉 分享内容已复制,请粘贴到QQ!');
    });
}

document.querySelectorAll('.platform-btn').forEach(btn => {
    const platform = btn.getAttribute('data-platform');
    const platformLinks = {
        '抖音': 'https://www.douyin.com/',
        '快手': 'https://www.kuaishou.com/',
        '火山视频': 'https://www.huoshan.com/',
        '西瓜视频': 'https://www.ixigua.com/',
        '皮皮虾': 'https://www.pipix.com/',
        '秒拍': 'https://www.miaopai.com/',
        '头条视频': 'https://www.toutiao.com/',
        '腾讯微视': 'https://weishi.qq.com/',
        '美图秀秀': 'https://www.meitu.com/',
        '美拍': 'https://www.meipai.com/',
        '微博': 'https://weibo.com/',
        '小红书': 'https://www.xiaohongshu.com/',
        '网易云': 'https://music.163.com/'
    };
    const link = platformLinks[platform] || '#';
    btn.addEventListener('click', () => window.open(link, '_blank'));
    btn.querySelector('img').addEventListener('click', e => { e.stopPropagation(); window.open(link, '_blank'); });
    btn.querySelector('span').addEventListener('click', e => { e.stopPropagation(); window.open(link, '_blank'); });
});

document.addEventListener('DOMContentLoaded', function() {
    initTheme();
    initThemeColor();
    loadSakuraEffect();
    initFishAnimation();
    fetchRandomQuote();
    getWeather();
    loadHistory();
    monitorClipboard();

    // 初始化背景图片并每10秒更新
    updateBackgroundImage();
    setInterval(updateBackgroundImage, 10000);

    // 初始化在线访客人数并每30秒更新
    updateOnlineUsers();
    setInterval(updateOnlineUsers, 30000);

    document.getElementById('theme-btn').addEventListener('click', function() {
        const currentTheme = document.documentElement.getAttribute('data-theme');
        const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
        document.documentElement.setAttribute('data-theme', newTheme);
        localStorage.setItem('theme', newTheme);
        this.innerHTML = newTheme === 'dark' ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
    });

    document.getElementById('parse-btn').addEventListener('click', function() {
        const url = document.getElementById('urlInput').value.trim();
        parseContent(extractURL(url));
    });

    document.getElementById('urlInput').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            const url = document.getElementById('urlInput').value.trim();
            parseContent(extractURL(url));
        }
    });

    const feedbackModal = document.getElementById('feedback-modal');
    const feedbackBtn = document.getElementById('feedback-btn');
    const footerFeedback = document.getElementById('footer-feedback');

    if (feedbackBtn) {
        feedbackBtn.addEventListener('click', () => {
            feedbackModal.style.display = 'block';
        });
    }

    if (footerFeedback) {
        footerFeedback.addEventListener('click', () => {
            feedbackModal.style.display = 'block';
        });
    }

    document.getElementById('cancel-feedback').addEventListener('click', () => {
        feedbackModal.style.display = 'none';
    });

    document.getElementById('feedback-form').addEventListener('submit', function(e) {
        e.preventDefault();
        const feedback = document.getElementById('feedback-text').value.trim();
        if (!feedback) {
            alert('请输入反馈内容');
            return;
        }
        console.log('用户反馈:', feedback);
        alert('感谢您的反馈!我们会尽快处理。');
        document.getElementById('feedback-text').value = '';
        feedbackModal.style.display = 'none';
    });

    document.getElementById('close-modal').addEventListener('click', () => {
        document.getElementById('image-modal').style.display = 'none';
        feedbackModal.style.display = 'none';
    });

    if (!window.saveAs) {
        console.warn('FileSaver.js 未加载!下载功能将不可用');
        showDebugInfo('警告:文件保存功能需要 FileSaver.js 支持');
    }
    if (!window.JSZip) {
        console.warn('JSZip 未加载!打包下载功能不可用');
        showDebugInfo('警告:打包下载需要 JSZip 库支持');
    }

    setInterval(updateTime, 1000);
    updateTime();
});