-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.html
More file actions
312 lines (287 loc) · 9.38 KB
/
index.html
File metadata and controls
312 lines (287 loc) · 9.38 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
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shorten-URL</title>
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
background-image: linear-gradient(-20deg, #d0b782 20%, #a0cecf 80%)
}
input[type=text] {
border: none;
text-align: center;
outline: none;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
.card {
width: 800px;
background-color: #ebedee;
border-radius: 10px;
overflow: hidden;
border: 1px solid #9b9ea0;
}
h2 {
text-align: center;
background-color: #dbdfe0a8;
margin: 0;
padding: 10px;
border-bottom: 1px solid #9b9ea0;
font-size: 20px;
}
.form-group {
padding: 5px 20px 20px 20px;
box-sizing: border-box;
width: 100%;
}
.form-group>label {
width: 100px;
line-height: 40px;
font-size: 18px;
}
.form-group>input {
width: 100%;
line-height: 28px;
font-size: 18px;
padding: 8px;
border-radius: 5px;
box-sizing: border-box;
}
.form-group>#submit {
width: 75%;
line-height: 40px;
font-size: 18px;
background-color: #61bee9;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
margin: auto;
margin-top: 20px;
display: block;
}
/* 遮罩层 */
.dialog {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
opacity: 0;
justify-content: center;
/* align-items: center; */
z-index: -1;
transition: opacity .4s ease-out, all .2s;
transform: translateZ(0);
}
/* 弹窗 */
.dialog-content {
width: 450px;
margin-top: 0;
height: fit-content;
background-color: #fff;
border-radius: 10px;
overflow: hidden;
border: 1px solid #9b9ea0;
transition: margin-top .4s ease-out;
}
.dialog-content>.dialog-header {
width: 100%;
padding: 0 20px;
box-sizing: border-box;
line-height: 56px;
font-size: 25px;
border-bottom: 1px solid #9b9ea0;
display: flex;
justify-content: space-between;
}
.dialog-content>.dialog-header>.dialog-close {
cursor: pointer;
/* 不可选中 */
user-select: none;
}
.dialog-content>.dialog-body {
width: 100%;
padding: 0 20px;
box-sizing: border-box;
border-bottom: 1px solid #9b9ea0;
font-size: 19px;
}
.dialog-content>.dialog-footer {
width: 100%;
display: flex;
justify-content: center;
padding-bottom: 20px;
}
.dialog-content>.dialog-footer>button {
width: 90px;
line-height: 35px;
font-size: 18px;
background-color: #61bee9;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
margin: auto;
margin-top: 20px;
display: block;
}
/* 显示弹窗 */
.dialog-show {
opacity: 1;
z-index: 100;
}
.dialog-show>.dialog-content {
margin-top: 20px;
}
footer{
position: fixed;
bottom: 5px;
width: 100%;;
text-align: center;
font-size: 18px;
}
footer p{
color: #666;
margin: 0;
height: 30px;
line-height: 30px;
}
footer a{
color: #666;
text-decoration: none;
}
footer a:hover{
color: #61bee9;
text-decoration: underline;
}
</style>
</head>
<body>
<div class="dialog">
<div class="dialog-content">
<div class="dialog-header">
<span class="dialog-title">提示</span>
<span class="dialog-close">×</span>
</div>
<div class="dialog-body">
<!-- <p>短链接:<span id="shortUrl"></span></p> -->
</div>
<div class="dialog-footer">
<!-- 复制按钮 -->
<!-- <button class="copy" id="copy">复制</button> -->
<!-- 确定按钮 -->
<!-- <button class="conform" id="conform">确定</button> -->
</div>
</div>
</div>
<div class="card">
<h2>Shorten-URL</h2>
<div class="form-group">
<label for="url">长链接:</label>
<input type="text" id="url" name="url" placeholder="例:https://baidu.com" value="" />
<label for="shortStr">自定义路径:</label>
<input type="text" id="shortStr" name="shortStr" placeholder="例:bai/du 最前面不用加'/' 为空则自动生成一个路径" value="" />
<input type="button" id="submit" value="提交" />
</div>
</div>
<footer>
<p>author: <a href="https://wlnxing.com">wlnxing</a></p>
</footer>
<script>
var dialog = document.querySelector('.dialog');
var url = document.getElementById('url');
var shortStr = document.getElementById('shortStr');
var submit = document.getElementById('submit');
submit.onclick = function () {
if (url.value == '') {
alert('请输入长链接');
return false;
}
fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: url.value,
shortStr: shortStr.value
})
}).then(function (res) {
if (res.ok)
return res.json();
else
throw res;
}).then(function (data) {
// alert('提交成功,短链接为:' + data.data.shortUrl);
// 在弹窗上显示短链接
dialog.querySelector('.dialog-body').innerHTML =
`<p>短链接:<span id="shortUrl">${location.host}/${data.data.shortUrl}</span></p>`;
// 在弹窗上显示复制按钮
dialog.querySelector('.dialog-footer').innerHTML =
`<button class="copy" id="copy">复制</button>`;
// 复制按钮
var copy = document.getElementById('copy');
copy.onclick = function () {
// 将短链接复制到剪贴板
var shortUrl = document.querySelector('#shortUrl');
try {
let range = document.createRange();
range.selectNode(shortUrl);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
console.log('Copy success')
} catch (e) {
console.log('Copy error')
}
}
// 显示弹窗
dialog.classList.add('dialog-show');
}).catch(async function (err) {
console.log(err);
let data;
try {
data = await err.json();
} catch (e) {
alert('提交失败,请检查网络');
return false
}
console.log(data);
dialog.querySelector('.dialog-body').innerHTML = `<p>${data.msg}</p>`;
dialog.querySelector('.dialog-footer').innerHTML =
`<button class="conform" id="conform">确定</button>`;
var conform = document.getElementById('conform');
conform.onclick = function () {
dialog.classList.remove('dialog-show');
}
dialog.classList.add('dialog-show');
})
}
// 关闭按钮
document.querySelector('.dialog-close').onclick = function () {
dialog.classList.remove('dialog-show');
}
// 点击空白处关闭弹窗
dialog.onclick = function (e) {
// 阻止事件冒泡
e.stopPropagation();
if (e.target == dialog) {
dialog.classList.remove('dialog-show');
}
}
</script>
</body>
</html>