-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
248 lines (209 loc) · 7.85 KB
/
Copy pathscript.js
File metadata and controls
248 lines (209 loc) · 7.85 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
// 测试的节点列表(固定)
const testDomains = [
"https://blockhaity.github.io",
"https://blockhaity.vercel.app",
"https://blockhaity.netlify.app",
"https://blockhaity.pages.dev",
"https://blog-vercel.blockhaity.dpdns.org",
"https://blog-edgeone.blockhaity.qzz.io"
];
// 存储测试结果
const testResults = [];
let fastestDomain = null;
let countdown = 3;
let countdownInterval;
let targetPath = "/"; // 默认路径为根目录
// 背景图片缓存状态
const cacheStatus = document.getElementById('cache-status');
// 缓存背景图片
async function cacheBackgroundImage() {
const imageUrl = 'https://api.blockhaity.qzz.io/images';
const cacheName = 'banner-cache-v1';
try {
// 检查浏览器是否支持Cache API
if (!('caches' in window)) {
cacheStatus.textContent = '缓存API不支持';
return;
}
// 打开缓存
const cache = await caches.open(cacheName);
// 检查图片是否已在缓存中
const cachedResponse = await cache.match(imageUrl);
if (cachedResponse) {
cacheStatus.textContent = '背景缓存: 已加载';
return;
}
// 如果不在缓存中,则获取并缓存
const response = await fetch(imageUrl);
if (response.ok) {
await cache.put(imageUrl, response.clone());
cacheStatus.textContent = '背景缓存: 已加载';
} else {
cacheStatus.textContent = '背景缓存: 加载失败';
}
} catch (error) {
console.error('缓存背景图片失败:', error);
cacheStatus.textContent = '背景缓存: 失败';
}
}
// 从URL参数获取目标路径
function getTargetPathFromUrl() {
const params = new URLSearchParams(window.location.search);
const pathParam = params.get('target');
// 确保路径以斜杠开头
if (pathParam) {
return pathParam.startsWith('/') ? pathParam : '/' + pathParam;
}
return "/"; // 未提供参数时默认使用根路径
}
// 初始化节点列表
function initDomainList() {
targetPath = getTargetPathFromUrl();
document.getElementById('path-display').textContent = targetPath;
const urlList = document.getElementById('url-list');
urlList.innerHTML = '';
testDomains.forEach((domain, index) => {
const urlItem = document.createElement('div');
urlItem.className = 'url-item loading';
urlItem.id = `domain-${index}`;
urlItem.innerHTML = `
<div class="url-icon">
<i class="fas fa-globe"></i>
</div>
<div class="url-details">
<div class="url-name">${domain}</div>
<div class="url-status">
<div class="ping-value">-</div>
<div class="status-text">测试中...</div>
</div>
</div>
`;
urlList.appendChild(urlItem);
});
}
// 测试单个节点的速度
function testDomainSpeed(domain, index) {
return new Promise((resolve) => {
const startTime = performance.now();
const timeout = 5000; // 5秒超时
const controller = new AbortController();
const timeoutId = setTimeout(() => {
controller.abort();
resolve({ domain, time: Infinity, status: 'timeout' });
}, timeout);
// 测试节点根路径
const testUrl = domain + '/';
fetch(testUrl, {
method: 'HEAD',
mode: 'no-cors',
signal: controller.signal
})
.then(() => {
clearTimeout(timeoutId);
const time = performance.now() - startTime;
resolve({ domain, time, status: 'success' });
})
.catch(() => {
clearTimeout(timeoutId);
resolve({ domain, time: Infinity, status: 'error' });
});
});
}
// 更新UI显示测试结果
function updateDomainResult(index, result) {
const domainItem = document.getElementById(`domain-${index}`);
if (!domainItem) return;
const pingValue = domainItem.querySelector('.ping-value');
const statusText = domainItem.querySelector('.status-text');
// 更新类名
domainItem.classList.remove('loading');
if (result.status === 'success') {
domainItem.classList.add('success');
statusText.textContent = '成功';
statusText.style.color = '#2ecc71';
pingValue.textContent = `${Math.round(result.time)}ms`;
pingValue.style.color = '#feb47b';
// 更新最快结果
if (!fastestDomain || result.time < fastestDomain.time) {
fastestDomain = result;
document.getElementById('fastest-ping').textContent = `${Math.round(result.time)}ms`;
}
} else {
domainItem.classList.add('error');
statusText.textContent = result.status === 'timeout' ? '超时' : '错误';
statusText.style.color = '#e74c3c';
pingValue.textContent = '失败';
pingValue.style.color = '#e74c3c';
}
// 更新已测试计数
const testedCount = document.querySelectorAll('.url-item:not(.loading)').length;
document.getElementById('tested-count').textContent = testedCount;
return testedCount;
}
// 显示结果并开始倒计时
function showResults() {
const resultContainer = document.getElementById('result-container');
const fullRedirectElement = document.getElementById('full-redirect-url');
const loadingOverlay = document.getElementById('loading-overlay');
// 隐藏加载层
loadingOverlay.style.display = 'none';
// 如果没有找到可用节点,使用第一个作为后备
if (!fastestDomain || fastestDomain.time === Infinity) {
fastestDomain = { domain: testDomains[0] };
}
// 显示完整重定向URL
const fullUrl = fastestDomain.domain + targetPath;
fullRedirectElement.textContent = fullUrl;
// 显示结果区域
resultContainer.style.display = 'block';
// 开始倒计时重定向
startCountdown();
}
// 开始倒计时重定向
function startCountdown() {
const countdownElement = document.getElementById('countdown');
const progressBar = document.getElementById('progress-bar');
let width = 0;
countdownInterval = setInterval(() => {
countdown--;
countdownElement.textContent = countdown;
// 更新进度条
width = 100 - (countdown / 3 * 100);
progressBar.style.width = `${width}%`;
if (countdown <= 0) {
clearInterval(countdownInterval);
// 重定向到完整URL(节点+目标路径)
window.location.href = fastestDomain.domain + targetPath;
}
}, 1000);
}
// 开始测试所有节点
async function startTesting() {
initDomainList();
// 同时测试所有节点
const testPromises = testDomains.map((domain, index) =>
testDomainSpeed(domain, index).then(result => {
const testedCount = updateDomainResult(index, result);
// 如果是最后一个完成的测试,显示结果
if (testedCount === testDomains.length) {
setTimeout(showResults, 400); // 延迟一点显示结果
}
return result;
})
);
// 等待所有测试完成
await Promise.all(testPromises);
}
// 页面加载完成后开始测试
document.addEventListener('DOMContentLoaded', () => {
// 缓存背景图片
cacheBackgroundImage();
// 添加立即跳转按钮事件
document.getElementById('redirect-btn').addEventListener('click', () => {
clearInterval(countdownInterval);
// 重定向到完整URL(节点+目标路径)
window.location.href = fastestDomain.domain + targetPath;
});
// 开始测试
startTesting();
});