-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathredirect.html
74 lines (68 loc) · 2.24 KB
/
redirect.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Redirector</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 20%;
}
a {
display: inline-block;
margin-top: 20px;
padding: 10px 20px;
background-color: #007bff;
color: white;
text-decoration: none;
border-radius: 5px;
}
a:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>跳转中,请稍候...</h1>
<p>如果未自动跳转,请手动点击下面的链接:</p>
<a id="redirect-link" href="#">手动跳转</a>
<script>
// 获取 URL 参数
function getQueryParam(param) {
const params = new URLSearchParams(window.location.search);
return params.get(param);
}
// 编解码逻辑
function decodeUrl(url) {
try {
return decodeURIComponent(url);
} catch (e) {
return url; // 如果解码失败,返回原始 URL
}
}
function encodeUrl(url) {
try {
return encodeURIComponent(url);
} catch (e) {
return url; // 如果编码失败,返回原始 URL
}
}
// 自动编解码处理
const rawUrl = getQueryParam('url'); // 原始输入 URL
const targetUrl = decodeUrl(rawUrl); // 尝试解码用户输入
// 校验 URL 是否有效
if (targetUrl && (targetUrl.startsWith('http') || targetUrl.includes(':'))) {
// 自动跳转
window.location.href = targetUrl;
// 更新手动跳转链接
document.getElementById('redirect-link').href = targetUrl;
document.getElementById('redirect-link').textContent = `跳转到:${encodeUrl(targetUrl)}`;
} else {
// 如果 URL 无效,提示用户
document.body.innerHTML = `<h1>无效的链接!</h1><p>请检查 URL 是否正确。</p>`;
}
</script>
</body>
</html>