-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
223 lines (194 loc) · 7.8 KB
/
script.js
File metadata and controls
223 lines (194 loc) · 7.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
// 全局变量
const PASSWORD = 'gouzai';
const API_URL = 'https://api.guijianpan.com/waterRemoveDetail/xxmQsyByAk?ak=cb0c4fca8fb24938b9fdc74fa8b6b2d0&link=';
let currentVideoData = null;
// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
// 密码输入框回车事件
document.getElementById('passwordInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
verifyPassword();
}
});
// 链接输入框回车事件
document.getElementById('linkInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
processVideo();
}
});
});
// 密码验证函数
function verifyPassword() {
const passwordInput = document.getElementById('passwordInput');
const passwordError = document.getElementById('passwordError');
const password = passwordInput.value.trim();
if (password === PASSWORD) {
// 密码正确,显示主界面
document.getElementById('passwordSection').classList.add('hidden');
document.getElementById('mainSection').classList.remove('hidden');
passwordError.textContent = '';
} else {
// 密码错误
passwordError.textContent = '密码错误,请重新输入';
passwordInput.value = '';
passwordInput.focus();
}
}
// 从文本中提取HTTPS链接
function extractHttpsLink(text) {
const httpsRegex = /https:\/\/[^\s\u4e00-\u9fff]+/g;
const matches = text.match(httpsRegex);
return matches ? matches[0] : null;
}
// 处理视频解析和下载
async function processVideo() {
const linkInput = document.getElementById('linkInput');
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const error = document.getElementById('error');
const downloadBtn = document.getElementById('downloadBtn');
const inputText = linkInput.value.trim();
if (!inputText) {
showError('请输入包含视频链接的文本');
return;
}
// 提取HTTPS链接
const videoLink = extractHttpsLink(inputText);
if (!videoLink) {
showError('未找到有效的HTTPS链接,请检查输入内容');
return;
}
// 显示加载状态
loading.classList.remove('hidden');
result.classList.add('hidden');
error.classList.add('hidden');
downloadBtn.disabled = true;
downloadBtn.textContent = '解析中...';
try {
// 调用API
const apiUrl = API_URL + encodeURIComponent(videoLink);
const response = await fetch(apiUrl);
const data = await response.json();
if (data.code === '10000' && data.content && data.content.success) {
// 解析成功
currentVideoData = data.content;
displayVideoInfo(data.content);
} else {
throw new Error(data.msg || '解析失败,请检查链接是否正确');
}
} catch (err) {
console.error('API调用失败:', err);
showError('解析失败: ' + err.message);
} finally {
// 恢复按钮状态
loading.classList.add('hidden');
downloadBtn.disabled = false;
downloadBtn.textContent = '解析下载';
}
}
// 显示视频信息
function displayVideoInfo(videoData) {
const result = document.getElementById('result');
const videoTitle = document.getElementById('videoTitle');
const videoCover = document.getElementById('videoCover');
const downloadVideoBtn = document.getElementById('downloadVideoBtn');
const downloadCoverBtn = document.getElementById('downloadCoverBtn');
// 设置标题
videoTitle.textContent = videoData.title || '未知标题';
// 设置封面
if (videoData.cover) {
videoCover.src = videoData.cover;
videoCover.style.display = 'block';
} else {
videoCover.style.display = 'none';
}
// 绑定下载事件
downloadVideoBtn.onclick = () => downloadFile(videoData.url, getFileName(videoData.title, 'mp4'));
downloadCoverBtn.onclick = () => downloadFile(videoData.cover, getFileName(videoData.title, 'jpg'));
// 显示结果
result.classList.remove('hidden');
}
// 生成文件名
function getFileName(title, extension) {
// 清理标题,移除特殊字符
const cleanTitle = title ? title.replace(/[^\w\u4e00-\u9fff\s-]/g, '').trim() : '视频';
const timestamp = new Date().getTime();
return `${cleanTitle}_${timestamp}.${extension}`;
}
// 下载文件
async function downloadFile(url, filename) {
if (!url) {
showError('下载链接无效');
return;
}
try {
// 显示下载提示
showError('正在准备下载...', false);
// 直接在新窗口打开链接,这是最可靠的方法
// 用户可以在新窗口中右键保存或使用浏览器的下载功能
const newWindow = window.open(url, '_blank');
if (newWindow) {
showError('已在新窗口打开视频,请右键选择"另存为"进行下载', false);
// 3秒后清除提示
setTimeout(() => {
document.getElementById('error').classList.add('hidden');
}, 3000);
} else {
// 如果弹窗被阻止,提供复制链接的选项
showCopyLinkOption(url, filename);
}
} catch (err) {
console.error('下载失败:', err);
showCopyLinkOption(url, filename);
}
}
// 显示复制链接选项
function showCopyLinkOption(url, filename) {
const errorElement = document.getElementById('error');
errorElement.innerHTML = `
<div style="text-align: left; padding: 10px; background: #f8f9fa; border-radius: 8px; margin-top: 10px;">
<p style="margin: 0 0 10px 0; font-weight: bold;">无法直接下载,请手动操作:</p>
<p style="margin: 0 0 8px 0;">1. 复制下面的链接</p>
<p style="margin: 0 0 8px 0;">2. 在新标签页中打开</p>
<p style="margin: 0 0 10px 0;">3. 右键选择"另存为"</p>
<div style="display: flex; gap: 10px; align-items: center;">
<input type="text" value="${url}" readonly style="flex: 1; padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 12px;">
<button onclick="copyToClipboard('${url}')" style="padding: 8px 12px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">复制链接</button>
</div>
</div>
`;
errorElement.style.color = '#333';
errorElement.classList.remove('hidden');
}
// 复制到剪贴板
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
showError('链接已复制到剪贴板!', false);
setTimeout(() => {
document.getElementById('error').classList.add('hidden');
}, 2000);
}).catch(() => {
// 如果复制失败,选中文本让用户手动复制
const input = document.querySelector('input[readonly]');
if (input) {
input.select();
input.setSelectionRange(0, 99999);
showError('请手动复制选中的链接', false);
}
});
}
// 显示错误信息
function showError(message, isError = true) {
const errorElement = document.getElementById('error');
errorElement.textContent = message;
errorElement.style.color = isError ? '#e74c3c' : '#27ae60';
errorElement.classList.remove('hidden');
}
// 重置表单
function resetForm() {
document.getElementById('linkInput').value = '';
document.getElementById('result').classList.add('hidden');
document.getElementById('error').classList.add('hidden');
currentVideoData = null;
}