-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
209 lines (198 loc) · 7.7 KB
/
index.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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>二维码工具</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #f8f9fa;
}
.navbar {
background-color: #007bff;
margin-bottom: 2rem;
}
.navbar-brand, .nav-link {
color: white !important;
}
.btn-primary {
background-color: #007bff;
border-color: #007bff;
}
.btn-primary:hover {
background-color: #0056b3;
border-color: #0056b3;
}
#qrInput {
width: 100%;
height: 200px;
border: 2px dashed #007bff;
display: flex;
justify-content: center;
align-items: center;
cursor: text;
padding: 10px;
}
#qrInputText {
width: 100%;
height: 100%;
border: none;
outline: none;
resize: none;
font-size: 16px;
}
#qrResult {
margin-top: 20px;
}
.main-container {
/* 主要内容container的特定样式 */
max-width: 800px; /* 例如,限制主要内容的宽度 */
margin: 0 auto; /* 居中显示 */
}
.custom-alert {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
padding: 15px 20px;
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
border-radius: 4px;
z-index: 1000;
display: none;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jsQR.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg">
<div class="container nav-container"> <!-- 修改这里 -->
<a class="navbar-brand" href="#">二维码工具</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav"> <!-- 修改这里 -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">主页</a>
</li>
<li class="nav-item">
<a class="nav-link" href="changelog.html">更新日志</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">联系我们</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container main-container"> <!-- 修改这里 -->
<div class="row">
<div class="col-md-12">
<div id="qrInput">
<textarea id="qrInputText" placeholder="请在此处粘贴二维码图片"></textarea>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-md-12">
<div id="qrResult" class="alert alert-info" role="alert">
二维码内容为
</div>
</div>
</div>
<div class="row mt-2">
<div class="col-md-12">
<button id="openUrlBtn" class="btn btn-primary me-2">在新页面打开链接</button>
<button id="copyResultBtn" class="btn btn-secondary">复制结果</button>
</div>
</div>
</div>
<div id="customAlert" class="custom-alert"></div>
<script>
const qrInputText = document.getElementById('qrInputText');
const qrResult = document.getElementById('qrResult');
const openUrlBtn = document.getElementById('openUrlBtn');
const copyResultBtn = document.getElementById('copyResultBtn');
let currentUrl = null;
qrInputText.addEventListener('paste', handlePaste);
function handlePaste(e) {
e.preventDefault();
const items = e.clipboardData.items;
for (let item of items) {
if (item.type.indexOf('image') !== -1) {
const blob = item.getAsFile();
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const code = jsQR(imageData.data, imageData.width, imageData.height);
if (code) {
qrResult.textContent = `${code.data}`;
qrResult.className = 'alert alert-success';
if (isValidURL(code.data)) {
currentUrl = code.data;
} else {
currentUrl = null;
}
} else {
qrResult.textContent = '未能识别二维码,请确保粘贴的是二维码图片。';
qrResult.className = 'alert alert-danger';
currentUrl = null;
}
};
img.src = event.target.result;
};
reader.readAsDataURL(blob);
break;
}
}
}
openUrlBtn.onclick = function() {
if (currentUrl) {
window.open(currentUrl, '_blank');
} else {
showCustomAlert('当前内容不是有效的URL或未识别到二维码。');
}
};
copyResultBtn.onclick = function() {
const resultText = qrResult.textContent;
if (resultText && resultText !== '请粘贴二维码图片') {
navigator.clipboard.writeText(resultText).then(() => {
showCustomAlert('结果已复制到剪贴板');
}).catch(err => {
console.error('复制失败:', err);
showCustomAlert('复制失败,请手动复制');
});
} else {
showCustomAlert('没有可复制的结果');
}
};
function showCustomAlert(message) {
const alertElement = document.getElementById('customAlert');
alertElement.textContent = message;
alertElement.style.display = 'block';
setTimeout(() => {
alertElement.style.display = 'none';
}, 1000);
}
function isValidURL(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>