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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
<!DOCTYPE html>
<html lang="en">
<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, user-scalable=0" />
<title>网页留言板</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 5px;
box-sizing: border-box;
}
textarea {
background-color: #333;
border-radius: 10px;
width: 80%;
height: 80%;
display: block;
margin: 0 auto;
color: #fff;
padding: 20px;
font-size: 16px;
box-sizing: border-box;
}
textarea::-webkit-input-placeholder {
color: #eee;
}
.action {
width: 80%;
margin: 10px auto;
text-align: right;
}
.btn {
padding: 0 10px;
height: 38px;
line-height: 38px;
min-width: 100px;
text-align: center;
border: none;
border-radius: 4px;
user-select: none;
-webkit-user-select:none;
-moz-user-select:none;
-o-user-select:none;
}
.btn-save {
background-color: #409eff;
color: #fff;
}
.btn-del {
background-color: #f56c6c;
color: #fff;
}
.btn-copy {
background-color: #409eff;
color: #fff;
height: 20px;
line-height: 20px;
width: 50px;
font-size: 12px;
margin-left: 5px;
}
.copy-box {
display: flex;
margin-top: 5px;
}
.copy-input {
flex: 1;
border: none;
padding: 0 10px;
border: 1px solid #ddd;
}
.operation-btn {
display: flex;
align-items: center;
margin-top: 20px;
justify-content: space-between;
}
.time-select {
flex: 1;
height: 38px;
margin-right: 5px;
max-width: 220px;
}
.message-box {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%);
background-color: #fff;
padding: 5px;
max-width: 250px;
border-radius: 4px;
z-index: 9999;
display: none;
}
@media (max-width: 900px) {
textarea {
width: 95%;
}
.action {
width: 95%;
}
}
</style>
</head>
<body>
<textarea id="text-box" class="" placeholder="请留言"> </textarea>
<div class="action copy-box">
<input class="copy-input" readonly value="" />
<button class="btn btn-copy">分享留言</button>
</div>
<div class="action operation-btn">
<select class="time-select">
<option value="1">5分钟销毁</option>
<option value="2">15分钟销毁</option>
<option value="3">1小时销毁</option>
<option value="4">24小时销毁</option>
<option value="5">7天后销毁</option>
<option value="6">30天后销毁</option>
<option value="7">一年后销毁</option>
</select>
<div onselectstart="return false;">
<button class="btn btn-del">清空/删除</button>
<button class="btn btn-save">留言</button>
</div>
</div>
<!-- 错误信息 -->
<div class="message-box"></div>
<script src="/clipboard.min.js"></script>
<script>
const baseUrl = window.location.origin
let btnSave = document.querySelector('.btn-save')
let btnDel = document.querySelector('.btn-del')
let textBox = document.querySelector('#text-box')
let timeSelect = document.querySelector('.time-select')
let copyInput = document.querySelector('.copy-input')
let btnCopy = document.querySelector('.btn-copy')
let messageBox = document.querySelector('.message-box')
let timerid = null
let needCopy = false
function showMessage(text, time = 1500) {
if(timerid){
clearTimeout(timerid)
timerid = null
}
messageBox.innerHTML = text
messageBox.style.display = 'block'
timerid = setTimeout(() => {
messageBox.style.display = 'none'
}, time)
}
function getValue(reload=false) {
let key = this.getQueryByKey(window.location.href, 'k')
if (!key) {
key = getQueryByKey(window.location.href, 'key')
}
if(!key){
textBox.value = ""
return
}
fetch(baseUrl + '/get?k=' + encodeURIComponent(key), {
method: 'GET',
}).then(function (res) {
return res.text()
}).then((val) => {
// 赋值
if(!val){
if(!reload){
getValue(true)
}else{
textBox.value = val
// history.replaceState('', '', baseUrl)
}
return
}
textBox.value = val
// 赋值内容
let shareUrl = baseUrl + '/?k=' + encodeURIComponent(key)
let is_block_key = getQueryByKey(window.location.href, 'block')
if (!is_block_key){
is_block_key = getQueryByKey(window.location.href, 'b')
if (is_block_key){
shareUrl = shareUrl + "&b=" + is_block_key
}
}
copyInput.value = shareUrl
// 读取消息有效时间
fetch(baseUrl + '/get?k=time_index_' + encodeURIComponent(key), {
method: 'GET',
}).then(function (res) {
return res.text()
}).then((val) => {
if(val){
timeSelect.selectedIndex = val
}
})
})
}
function getQueryByKey(url, name) {
if(!url){
return ""
}
var paths=url.split('?')
if(paths.length < 2){
return ""
}
var str = paths[1]
var keys = str.split('&')
var obj = {}
keys.forEach((item, idx, data) => {
var arr = item.split('=')
obj[arr[0]] = decodeURI(arr[1])
})
return obj[name]
}
function getTime() {
var index = timeSelect.selectedIndex
let time = timeSelect.options[index].value
switch (time) {
case '1':
return 5 * 60
case '2':
return 15 * 60
case '3':
return 1 * 60 * 60
case '4':
return 24 * 60 * 60
case '5':
return 7 * 24 * 60 * 60
case '6':
return 30 * 24 * 60 * 60
case '7':
return 12 * 30 * 24 * 60 * 60
default:
return 60 * 60
}
}
function getRandom(m) {
// 随机数字
// m = m > 16 ? 16 : m;
// var num = Math.random().toString();
// return num.substring(num.length - m);
// 随机数字+小写字母
// let letter = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var data = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
]
m = m > 16 ? 16 : m
var str = ''
let endnum = data.length - 1
let startnum = 0
for (var i = 0; i < m; i++) {
var ram = Math.floor(
Math.random() * (endnum - startnum + 1 + startnum)
)
str += data[ram]
}
return str.toUpperCase()
}
btnSave.addEventListener('click', function () {
let text = textBox.value
let is_block_key = getQueryByKey(window.location.href, 'block')
if (!is_block_key) {
is_block_key = getQueryByKey(window.location.href, 'b')
}
let key;
if (is_block_key == 'true'){
key = getQueryByKey(window.location.href, 'key')
if (!key){
key = getQueryByKey(window.location.href, 'k')
}
}
if (!key){
key = getRandom(8)
}
let shareUrl = baseUrl + '/?k=' + encodeURIComponent(key)
if (is_block_key){
shareUrl = shareUrl + '&b=' + encodeURIComponent(is_block_key)
}
copyInput.value = shareUrl
history.replaceState('', '', shareUrl)
fetch(baseUrl + '/set', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
't': getTime(),
'k': key,
'v': text
})
}).then(function () {
needCopy = true
showMessage('留言成功,请分享链接')
})
fetch(baseUrl + '/set', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
't': getTime(),
'k': 'time_index_'+key,
'v': ''+timeSelect.selectedIndex
})})
});
// var saveClipboard = new ClipboardJS('.btn-save', {
// text: function() {
// return shareUrl;
// }
// });
btnDel.addEventListener('click', function () {
textBox.value = ''
showMessage('清空成功,长按可删除')
})
var copyClipboard = new ClipboardJS('.btn-copy', {
text: function() {
return copyInput.value;
}
});
copyClipboard.on('success', function(e) {
showMessage('复制成功,请分享链接')
});
timeSelect.addEventListener('change', function () {
localStorage.setItem('timeselection', timeSelect.selectedIndex)
})
var lastTouchEnd=0;
var btntouchtime=0;
function touchstart(){
if(event.touches.length>1){
event.preventDefault();
}
}
function touchend(){
var now=(new Date()).getTime();
if(now-lastTouchEnd<=300){
event.preventDefault();
}
lastTouchEnd=now;
}
function btntouchstart(){
var now=(new Date()).getTime();
if(event.touches.length>1){
event.preventDefault();
}
if(now-btntouchtime<=300){
event.preventDefault();
}
btntouchtime = now
}
//function btntouchend(e){
// var now=(new Date()).getTime();
// if(now-btntouchtime<=300){
// event.preventDefault();
// }
//}
var timer=null;
var touchtime=null;
var touchx=0; var touchy=0;
var touchstartHander=function(event){
// event.preventDefault();
var now=(new Date()).getTime();
touchtime = now
timer=setTimeout(LongPress,500);
touchx=0; touchy=0;
}
var touchmoveHander=function(event){
if(touchx!=0 && touchy!=0){
event.preventDefault();
clearTimeout(timer);
timer=null;
}
touchx = event.changedTouches[0].pageX;
touchy = event.changedTouches[0].pageY;
}
var touchendHander=function(event){
var now=(new Date()).getTime();
clearTimeout(timer);
timer=null;
if(now-touchtime>=500){
event.preventDefault();
event.target.disabled = true;
setTimeout(function(){
event.target.disabled = false;
},100);
}
return false;
}
function LongPress(){
let key = getQueryByKey(copyInput.value, 'k')
if (!key) {
key = getQueryByKey(copyInput.value, 'key')
}
var supportsVibrate = "vibrate" in window.navigator;
if (supportsVibrate) {
window.navigator.vibrate(100);
}
fetch(baseUrl + '/del?k=' + encodeURIComponent(key), {
method: 'GET',
}).then(function () {
copyInput.value = ''
textBox.value = ''
showMessage('删除成功')
})
}
window.onload = function () {
getValue()
timeSelect.selectedIndex = localStorage.getItem('timeselection') || 6
// 其他功能
document.addEventListener('touchstart',touchstart);
document.addEventListener('touchend',touchend,false);
btnCopy.addEventListener('touchstart',btntouchstart);
// btnCopy.addEventListener('touchend',btntouchend,false);
btnDel.addEventListener('touchstart',btntouchstart);
// btnDel.addEventListener('touchend',btntouchend,false);
btnSave.addEventListener('touchstart',btntouchstart);
// btnSave.addEventListener('touchend',btntouchend,false);
// 长按删除
btnDel.addEventListener("touchstart",touchstartHander,false);
btnDel.addEventListener("touchmove",touchmoveHander,false);
btnDel.addEventListener("touchend",touchendHander,false);
btnDel.addEventListener("mousedown",touchstartHander,false);
btnDel.addEventListener("mousemove",touchmoveHander,false);
btnDel.addEventListener("mouseup",touchendHander,false);
document.addEventListener('gesturestart', function (event) {
event.preventDefault();
});
}
function copyText() {
if(needCopy){
needCopy = false
if (navigator.clipboard) {
navigator.clipboard.writeText(copyInput.value)
} else {
// 选中
copyInput.select()
// 复制
document.execCommand('copy', true)
}
}
}
// document.body.addEventListener('mouseup',copyText, true);
</script>
</body>
</html>
|