Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
806 lines (664 loc) · 28.8 KB
/
Copy pathmain.js
File metadata and controls
806 lines (664 loc) · 28.8 KB
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
/**
*@Author: JH-Ahua
*@CreateTime: 2025/5/19 下午14:08
*@email: admin@bugpk.com
*@blog: www.jiuhunwl.cn
*@Api: api.bugpk.com
*@tip: 短视频去水印解析前端
*/
// 工具函数:验证URL
function isValidUrl(url) {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
// 安全转义HTML(用于文本内容)
function escapeHtml(unsafe) {
return unsafe?.toString()
?.replace(/&/g, "&")
?.replace(/</g, "<")
?.replace(/>/g, ">")
?.replace(/"/g, """)
?.replace(/'/g, "'") || '';
}
// 清理URL中的反引号(不转义其他字符)
function cleanUrl(url) {
return url?.replace(/`/g, '') || '';
}
// 下载单个图片
function downloadImage(url) {
const cleanUrl = cleanUrl(url);
downloadFile(cleanUrl);
}
// 通用下载函数
function downloadFile(url) {
const a = document.createElement('a');
a.href = url;
a.download = url.split('/').pop().split('?')[0];
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
// 格式化数字(添加千位分隔符)
function formatNumber(num) {
if (!num) return '0';
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// 格式化时间戳为日期字符串
function formatDate(timestamp) {
if (!timestamp) return '';
// 如果时间戳是秒级,转换为毫秒级
if (timestamp.toString().length <= 10) {
timestamp = timestamp * 1000;
}
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
// 显示提示消息
function showToast(message) {
let toast = document.getElementById('toast-message');
if (toast) document.body.removeChild(toast);
toast = document.createElement('div');
toast.id = 'toast-message';
toast.className = 'fixed top-4 right-4 bg-dark text-white px-6 py-3 rounded-lg shadow-lg z-50 transform transition-all duration-300 translate-y-[-20px] opacity-0 flex items-center';
toast.innerHTML = `<i class="fa fa-check-circle mr-2 text-green-400"></i><span>${message}</span>`;
document.body.appendChild(toast);
setTimeout(() => {
toast.classList.remove('translate-y-[-20px]', 'opacity-0');
}, 10);
setTimeout(() => {
toast.classList.add('translate-y-[-20px]', 'opacity-0');
setTimeout(() => document.body.removeChild(toast), 300);
}, 3000);
}
// 显示错误信息
function showError(message) {
hideAllContainers();
const errorContainer = document.getElementById('error-container');
const errorMessage = document.getElementById('error-message');
if (errorContainer && errorMessage) {
errorMessage.textContent = message;
errorContainer.classList.remove('hidden');
// 错误显示动画
errorContainer.style.cssText = 'opacity: 0; transform: translateY(10px); transition: opacity 0.3s ease, transform 0.3s ease';
setTimeout(() => {
errorContainer.style.opacity = '1';
errorContainer.style.transform = 'translateY(0)';
}, 10);
}
}
// 隐藏所有容器
function hideAllContainers() {
['result-container', 'loading-container', 'error-container', 'images-container'].forEach(id => {
document.getElementById(id)?.classList.add('hidden');
});
}
// 创建信息卡片
function createInfoCard(label, value) {
const card = document.createElement('div');
card.className = 'bg-gray-50 rounded-lg p-4';
const labelEl = document.createElement('div');
labelEl.className = 'text-sm text-gray-500 mb-1';
labelEl.textContent = label;
const valueEl = document.createElement('div');
valueEl.className = 'font-medium';
valueEl.textContent = escapeHtml(value);
card.append(labelEl, valueEl);
return card;
}
// 更新视频预览
function updateVideoPreview(previewVideo, videoPlaceholder, videoCover, videoData) {
// 重置资源
previewVideo.src = '';
videoCover.src = '';
// 处理封面
if (videoData.cover) {
videoCover.src = cleanUrl(videoData.cover);
videoCover.classList.remove('hidden');
videoPlaceholder.classList.add('hidden');
} else {
videoCover.classList.add('hidden');
videoPlaceholder.classList.remove('hidden');
}
// 处理视频
if (videoData.url) {
previewVideo.src = cleanUrl(videoData.url);
previewVideo.classList.remove('hidden');
videoPlaceholder.classList.add('hidden');
if (videoData.coverUrl) {
previewVideo.poster = cleanUrl(videoData.coverUrl);
}
} else {
previewVideo.classList.add('hidden');
if (videoData.coverUrl) {
videoCover.src = cleanUrl(videoData.coverUrl);
videoCover.classList.remove('hidden');
} else {
videoCover.classList.add('hidden');
}
}
}
// 创建音乐容器
function createMusicContainer(videoData) {
const music = videoData.music || {};
const musicContainer = document.createElement('div');
// 检查音乐数据是否存在且包含有效的URL
const hasValidMusic = music && music.url && isValidUrl(music.url);
if (!hasValidMusic) {
// 没有有效音乐链接,显示"视频原声"提示
musicContainer.className = 'bg-gray-50 rounded-xl p-6 mb-6 mt-6';
const flexContainer = document.createElement('div');
flexContainer.className = 'flex items-center';
const iconContainer = document.createElement('div');
iconContainer.className = 'w-12 h-12 bg-primary/10 rounded-full flex items-center justify-center mr-4';
iconContainer.innerHTML = '<i class="fa fa-music text-primary"></i>';
const textContainer = document.createElement('div');
const titleHeading = document.createElement('h4');
titleHeading.className = 'font-medium';
titleHeading.textContent = '视频原声';
textContainer.appendChild(titleHeading);
flexContainer.appendChild(iconContainer);
flexContainer.appendChild(textContainer);
musicContainer.appendChild(flexContainer);
return musicContainer;
}
// 有有效音乐链接,显示音乐播放模块
const musicTitle = escapeHtml(music.title || '未知音乐');
const musicAuthor = escapeHtml(music.author || '未知作者');
const musicAvatar = music.avatar ? cleanUrl(music.avatar) : '';
const musicUrl = cleanUrl(music.url);
musicContainer.className = 'bg-gray-50 rounded-xl p-6 mb-6 mt-6';
const flexContainer = document.createElement('div');
flexContainer.className = 'flex items-start';
// 添加音乐封面(如果有)
if (musicAvatar) {
const avatarImg = document.createElement('img');
avatarImg.src = musicAvatar;
avatarImg.className = 'w-20 h-20 rounded-lg mr-4';
avatarImg.onerror = "this.onerror=null;this.src='https://via.placeholder.com/80x80?text=封面'";
flexContainer.appendChild(avatarImg);
}
// 创建音乐信息容器
const infoContainer = document.createElement('div');
infoContainer.className = 'flex-1';
// 添加音乐标题
const titleHeading = document.createElement('h4');
titleHeading.className = 'font-medium';
titleHeading.textContent = musicTitle;
// 添加音乐作者
const authorPara = document.createElement('p');
authorPara.className = 'text-sm text-gray-500';
authorPara.textContent = musicAuthor;
// 添加标题和作者到信息容器
const metaContainer = document.createElement('div');
metaContainer.className = 'mb-4';
metaContainer.appendChild(titleHeading);
metaContainer.appendChild(authorPara);
// 创建音频控制和下载按钮的容器
const controlContainer = document.createElement('div');
controlContainer.className = 'flex items-center gap-4';
// 创建音频元素
const audioElement = document.createElement('audio');
audioElement.controls = true;
audioElement.className = 'flex-1';
const sourceElement = document.createElement('source');
sourceElement.src = musicUrl;
sourceElement.type = 'audio/mpeg';
audioElement.appendChild(sourceElement);
// 创建下载按钮
const downloadBtn = document.createElement('button');
downloadBtn.className = 'px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary-dark';
downloadBtn.innerHTML = '<i class="fa fa-download"></i>';
downloadBtn.onclick = () => downloadFile(musicUrl);
// 添加音频和下载按钮到控制容器
controlContainer.appendChild(audioElement);
controlContainer.appendChild(downloadBtn);
// 添加所有内容到音乐信息容器
infoContainer.appendChild(metaContainer);
infoContainer.appendChild(controlContainer);
// 添加音乐信息容器到主容器
flexContainer.appendChild(infoContainer);
musicContainer.appendChild(flexContainer);
return musicContainer;
}
// 创建作者信息容器
function createAuthorContainer(videoData) {
const authorContainer = document.createElement('div');
authorContainer.className = 'flex items-center mb-6';
if (videoData.avatar) {
const avatarImg = document.createElement('img');
avatarImg.src = cleanUrl(videoData.avatar);
avatarImg.className = 'w-12 h-12 rounded-full mr-4';
avatarImg.onerror = "this.onerror=null;this.src='https://via.placeholder.com/48x48?text=头像'";
authorContainer.appendChild(avatarImg);
}
const authorInfo = document.createElement('div');
const authorName = document.createElement('div');
authorName.className = 'font-medium';
authorName.textContent = videoData.author || '未知作者';
const likeCount = document.createElement('div');
likeCount.className = 'text-sm text-gray-500';
likeCount.textContent = videoData.like ? `点赞 ${formatNumber(videoData.like)}` : '';
authorInfo.appendChild(authorName);
authorInfo.appendChild(likeCount);
authorContainer.appendChild(authorInfo);
return authorContainer;
}
// 创建信息容器
function createInfoContainer(videoData) {
const infoContainer = document.createElement('div');
infoContainer.className = 'grid grid-cols-1 md:grid-cols-3 gap-4 mb-6';
// 视频标题
infoContainer.appendChild(createInfoCard('作品标题', videoData.title || '未知标题'));
// 发布时间
infoContainer.appendChild(createInfoCard('发布时间', formatDate(videoData.time) || '未知时间'));
// 作品类型
const typeText = videoData.images && videoData.images.length > 0 ? '图片集' : '视频';
infoContainer.appendChild(createInfoCard('作品类型', typeText));
return infoContainer;
}
// 创建视频播放器
function createVideoPlayer(container, videoData) {
// 创建视频预览容器
const previewContainer = document.createElement('div');
previewContainer.className = 'rounded-xl overflow-hidden mb-6 shadow-lg';
// 创建视频占位符
const videoPlaceholder = document.createElement('div');
videoPlaceholder.id = 'video-placeholder';
videoPlaceholder.className = 'w-full h-64 bg-gray-100 flex items-center justify-center';
videoPlaceholder.innerHTML = '<i class="fa fa-film text-5xl text-gray-300"></i>';
// 创建视频元素
const previewVideo = document.createElement('video');
previewVideo.id = 'preview-video';
previewVideo.className = 'w-full hidden';
previewVideo.setAttribute('controls', '');
// 创建封面图片
const videoCover = document.createElement('img');
videoCover.id = 'video-cover';
videoCover.className = 'w-full hidden';
videoCover.setAttribute('alt', '视频封面');
// 更新视频预览
updateVideoPreview(previewVideo, videoPlaceholder, videoCover, videoData);
// 添加到预览容器
previewContainer.append(videoPlaceholder, previewVideo, videoCover);
container.appendChild(previewContainer);
// 创建下载按钮
const downloadBtn = document.createElement('a');
downloadBtn.id = 'download-btn';
downloadBtn.href = cleanUrl(videoData.url);
downloadBtn.className = 'button-neomorphism px-6 py-3 text-white font-medium flex items-center justify-center mb-4';
downloadBtn.innerHTML = '<i class="fa fa-download mr-2"></i><span>下载无水印视频</span>';
container.appendChild(downloadBtn);
}
// 创建图片画廊
function createImageGallery(container, videoData) {
// 创建图片容器
const imagesContainer = document.createElement('div');
imagesContainer.id = 'images-container';
imagesContainer.className = 'swiper-container w-full mb-6';
const wrapper = document.createElement('div');
wrapper.className = 'swiper-wrapper';
// 添加所有图片
videoData.images.forEach((img, index) => {
const slide = document.createElement('div');
slide.className = 'swiper-slide relative group';
const imgElement = document.createElement('img');
imgElement.src = cleanUrl(img);
imgElement.className = 'w-full h-96 object-cover rounded-xl';
imgElement.loading = 'lazy';
imgElement.onerror = "this.onerror=null;this.src='https://via.placeholder.com/800x600?text=图片加载失败'";
const downloadBtn = document.createElement('button');
downloadBtn.className = 'absolute bottom-4 right-4 bg-white/80 hover:bg-white px-4 py-2 rounded-lg shadow-md transition-all opacity-0 group-hover:opacity-100';
downloadBtn.innerHTML = '<i class="fa fa-download mr-2"></i>下载';
downloadBtn.onclick = () => {
const cleanedUr = cleanUrl(img);
downloadFile(cleanedUr);
};
slide.appendChild(imgElement);
slide.appendChild(downloadBtn);
wrapper.appendChild(slide);
});
// 添加分页和导航
const pagination = document.createElement('div');
pagination.className = 'swiper-pagination mt-2';
const prevButton = document.createElement('div');
prevButton.className = 'swiper-button-prev';
const nextButton = document.createElement('div');
nextButton.className = 'swiper-button-next';
imagesContainer.appendChild(wrapper);
imagesContainer.appendChild(pagination);
imagesContainer.appendChild(prevButton);
imagesContainer.appendChild(nextButton);
container.appendChild(imagesContainer);
// 添加下载全部按钮
const downloadAllBtn = document.createElement('button');
downloadAllBtn.className = 'button-neomorphism px-6 py-3 mb-6 w-full';
downloadAllBtn.innerHTML = `<i class="fa fa-images mr-2"></i>下载全部图片(${videoData.images.length}张)`;
downloadAllBtn.onclick = () => {
videoData.images.forEach(img => {
const cleanedUrs = cleanUrl(img); // 修改变量名避免冲突
downloadFile(cleanedUrs);
});
};
container.appendChild(downloadAllBtn);
// 初始化Swiper
initSwiper();
}
// 初始化Swiper插件
function initSwiper() {
try {
if (typeof Swiper !== 'undefined') {
new Swiper('.swiper-container', {
loop: true,
pagination: { el: '.swiper-pagination', clickable: true },
navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' }
});
} else {
console.error('Swiper库未加载');
// 降级处理 - 为非Swiper环境添加基本样式
document.querySelectorAll('.swiper-slide').forEach(slide => {
slide.classList.add('mb-4');
});
showToast('幻灯片组件加载失败,将以普通图片显示');
}
} catch (error) {
console.error('初始化Swiper失败:', error);
// 降级处理
document.querySelectorAll('.swiper-slide').forEach(slide => {
slide.classList.add('mb-4');
});
showToast('幻灯片组件加载失败,将以普通图片显示');
}
}
// 复制URL并显示提示
function copyUrl(url, type) {
if (!url) {
showError(`没有可复制的${type}`);
return;
}
navigator.clipboard.writeText(url).then(() => {
showToast(`${type}已复制到剪贴板`);
// 获取按钮并添加成功状态
const btn = document.activeElement;
if (btn && btn.id.startsWith('copy-')) {
btn.classList.add('bg-green-500', 'text-white');
btn.innerHTML = `<i class="fa fa-check mr-2"></i><span>复制成功</span>`;
setTimeout(() => {
btn.classList.remove('bg-green-500', 'text-white');
btn.innerHTML = `<i class="fa fa-link mr-2"></i><span>复制${type}</span>`;
}, 2000);
}
}).catch(err => {
showError(`无法复制${type}`);
});
}
// 绑定复制按钮事件
function bindCopyButtonEvents() {
// 复制链接按钮
document.getElementById('copy-cover-btn')?.addEventListener('click', () => {
copyUrl(videosjson?.cover, '封面链接');
});
document.getElementById('copy-url-btn')?.addEventListener('click', () => {
copyUrl(videosjson?.url, '视频链接');
});
}
// 检查并加载Swiper库
function checkSwiper() {
if (typeof Swiper === 'undefined') {
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/swiper@9/swiper-bundle.min.js';
script.onload = () => {
console.log('Swiper库加载成功');
// 如果有图片集,重新初始化Swiper
const imagesContainer = document.getElementById('images-container');
if (imagesContainer && !imagesContainer.classList.contains('hidden')) {
initSwiper();
}
};
script.onerror = () => console.error('Swiper库加载失败');
document.head.appendChild(script);
}
}
// 为页面添加暗色模式支持
function setupDarkMode() {
const darkModeToggle = document.getElementById('dark-mode-toggle');
if (!darkModeToggle) return;
// 检查用户偏好
const isDarkMode = localStorage.getItem('darkMode') === 'true' ||
(localStorage.getItem('darkMode') === null &&
window.matchMedia('(prefers-color-scheme: dark)').matches);
// 应用初始模式
document.documentElement.classList.toggle('dark', isDarkMode);
darkModeToggle.checked = isDarkMode;
// 监听切换事件
darkModeToggle.addEventListener('change', () => {
const darkMode = darkModeToggle.checked;
document.documentElement.classList.toggle('dark', darkMode);
localStorage.setItem('darkMode', darkMode);
});
}
// 添加分享功能
function setupShare() {
const shareBtn = document.getElementById('share-btn');
if (!shareBtn) return;
shareBtn.addEventListener('click', async () => {
try {
if (navigator.share) {
await navigator.share({
title: '视频解析工具',
text: '我用这个工具解析了一个视频,你也可以试试',
url: window.location.href
});
} else {
// 回退方案:复制链接到剪贴板
navigator.clipboard.writeText(window.location.href).then(() => {
showToast('链接已复制到剪贴板');
}).catch(err => {
showError('无法复制链接');
});
}
} catch (error) {
showError('分享失败');
console.error('分享错误:', error);
}
});
}
// 存储视频数据
let videosjson = '';
document.addEventListener('DOMContentLoaded', function() {
setupFAQToggle();
// 导航栏滚动效果
const navbar = document.getElementById('navbar');
window.addEventListener('scroll', () => {
navbar.classList.toggle('py-2', window.scrollY > 50);
navbar.classList.toggle('py-4', window.scrollY <= 50);
navbar.classList.toggle('shadow-lg', window.scrollY > 50);
});
// 平台与接口的映射关系
const platformApiMap = {
'all': 'https://api.bugpk.com/api/short_videos',
'douyin': 'https://api.bugpk.com/api/douyin.php',
'kuaishou': 'https://api.bugpk.com/api/kuaishou',
'bilibili': 'https://api.bugpk.com/api/bilibili'
};
// 当前选中的平台
let currentPlatform = 'all';
// 平台选择按钮
document.querySelectorAll('.platform-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.platform-btn').forEach(b => {
b.classList.remove('active', 'bg-primary/10', 'text-primary');
b.classList.add('bg-gray-100', 'text-gray-600');
});
btn.classList.add('active', 'bg-primary/10', 'text-primary');
btn.classList.remove('bg-gray-100', 'text-gray-600');
currentPlatform = btn.getAttribute('data-platform');
});
});
// 初始激活"所有平台"按钮
document.querySelector('.platform-btn[data-platform="all"]')?.click();
// 清空输入框按钮
document.getElementById('clear-btn')?.addEventListener('click', () => {
const videoUrlInput = document.getElementById('video-url');
videoUrlInput.value = '';
videoUrlInput.parentElement.classList.remove('scale-[1.01]');
document.getElementById('clear-btn').classList.add('animate-spin');
setTimeout(() => document.getElementById('clear-btn').classList.remove('animate-spin'), 500);
});
// 解析按钮
document.getElementById('parse-btn')?.addEventListener('click', async () => {
const parseBtn = document.getElementById('parse-btn');
parseBtn.classList.add('scale-95');
setTimeout(() => parseBtn.classList.remove('scale-95'), 200);
const url = document.getElementById('video-url')?.value.trim() || '';
if (!url) {
showError('请输入视频链接');
return;
}
if (!isValidUrl(url)) {
showError('请输入有效的URL');
return;
}
hideAllContainers();
document.getElementById('loading-container')?.classList.remove('hidden');
try {
const apiUrl = platformApiMap[currentPlatform] || platformApiMap['all'];
const controller = new AbortController();
const signal = controller.signal;
const timeoutId = setTimeout(() => controller.abort(), 15000);
const encodedUrl = encodeURIComponent(url);
const response = await fetch(`${apiUrl}?url=${encodedUrl}`, { signal });
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP错误! 状态码: ${response.status}`);
}
const data = await response.json();
if (data.code === 200) {
videosjson = data.data;
showResult(videosjson);
// 重新绑定复制按钮事件
bindCopyButtonEvents();
} else {
showError(data.msg || '解析失败,请稍后再试');
}
} catch (error) {
console.error('解析请求出错:', error);
showError(error.name === 'AbortError' ? '请求超时,请重试' : '网络错误,请检查您的连接');
}
});
// 处理键盘事件
document.addEventListener('keydown', (event) => {
// Ctrl+V 快捷解析
if (event.ctrlKey && event.key === 'v') {
setTimeout(() => {
const urlInput = document.getElementById('video-url');
if (urlInput && urlInput.value.trim()) {
const parseBtn = document.getElementById('parse-btn');
if (parseBtn) parseBtn.click();
}
}, 100);
}
// ESC 关闭结果
if (event.key === 'Escape') {
hideAllContainers();
document.getElementById('video-url').value = '';
}
});
// 初始化暗色模式
setupDarkMode();
// 初始化分享功能
setupShare();
// 页面加载时检查Swiper
checkSwiper();
});
// 显示解析结果
function showResult(videoData) {
hideAllContainers();
const resultContainer = document.getElementById('result-container');
if (!resultContainer) return;
resultContainer.innerHTML = '';
resultContainer.classList.remove('hidden');
// 显示容器动画
resultContainer.style.cssText = 'opacity: 0; transform: translateY(20px); transition: opacity 0.5s ease, transform 0.5s ease';
setTimeout(() => {
resultContainer.style.opacity = '1';
resultContainer.style.transform = 'translateY(0)';
}, 10);
// 创建作者信息(图片和视频模式都需要)
const authorContainer = createAuthorContainer(videoData);
if (authorContainer) {
resultContainer.appendChild(authorContainer);
}
// 创建视频/图片内容
if (videoData.images && videoData.images.length > 0) {
// 图片集处理
createImageGallery(resultContainer, videoData);
// 复制按钮容器(仅图片模式)
const copyBtnContainer = document.createElement('div');
copyBtnContainer.className = 'flex flex-wrap gap-4 mb-6';
// 复制封面链接按钮
const copyCoverBtn = document.createElement('button');
copyCoverBtn.id = 'copy-cover-btn';
copyCoverBtn.className = 'bg-white border border-primary text-primary hover:bg-primary/5 transition-colors rounded-lg px-6 py-3 font-medium flex items-center justify-center flex-1';
copyCoverBtn.innerHTML = '<i class="fa fa-link mr-2"></i><span>复制封面链接</span>';
copyBtnContainer.appendChild(copyCoverBtn);
resultContainer.appendChild(copyBtnContainer);
} else {
// 视频处理
createVideoPlayer(resultContainer, videoData);
// 复制按钮容器(仅视频模式)
const copyBtnContainer = document.createElement('div');
copyBtnContainer.className = 'flex flex-wrap gap-4 mb-6';
// 复制封面链接按钮
const copyCoverBtn = document.createElement('button');
copyCoverBtn.id = 'copy-cover-btn';
copyCoverBtn.className = 'bg-white border border-primary text-primary hover:bg-primary/5 transition-colors rounded-lg px-6 py-3 font-medium flex items-center justify-center flex-1';
copyCoverBtn.innerHTML = '<i class="fa fa-link mr-2"></i><span>复制封面链接</span>';
// 复制视频链接按钮
const copyUrlBtn = document.createElement('button');
copyUrlBtn.id = 'copy-url-btn';
copyUrlBtn.className = 'bg-white border border-primary text-primary hover:bg-primary/5 transition-colors rounded-lg px-6 py-3 font-medium flex items-center justify-center flex-1';
copyUrlBtn.innerHTML = '<i class="fa fa-link mr-2"></i><span>复制视频链接</span>';
copyBtnContainer.append(copyCoverBtn, copyUrlBtn);
resultContainer.appendChild(copyBtnContainer);
}
// 创建视频信息(图片和视频模式都需要)
const infoContainer = createInfoContainer(videoData);
if (infoContainer) {
resultContainer.appendChild(infoContainer);
}
// 添加音乐容器(图片和视频模式都需要)
const musicContainer = createMusicContainer(videoData);
if (musicContainer && musicContainer.innerHTML.trim()) {
resultContainer.appendChild(musicContainer);
}
}
// 新增常见问题切换功能
function setupFAQToggle() {
document.querySelectorAll('.faq-btn').forEach(button => {
button.addEventListener('click', function() {
const content = this.nextElementSibling;
const icon = this.querySelector('i');
// 切换内容显示
content.classList.toggle('hidden');
content.classList.toggle('block');
// 旋转图标动画
icon.classList.toggle('rotate-180');
// 关闭其他展开的问题
document.querySelectorAll('.faq-btn').forEach(otherBtn => {
if (otherBtn !== button) {
otherBtn.nextElementSibling.classList.add('hidden');
otherBtn.querySelector('i').classList.remove('rotate-180');
}
});
});
});
}