-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
113 lines (94 loc) · 2.01 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
<small>
<span id="last"></span>
<span id="next"></span>
</small>
<script type="module">
const res = await fetch('https://geolocation.microlink.io/')
const json = await res.json()
const {
city: {
name: city
},
country: {
name: country,
flag
}
} = json
await fetch('/visit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
city,
country,
flag
})
})
const source = new EventSource('/visit')
const $last = document.getElementById('last')
const $next = document.getElementById('next')
let ready = true
source.addEventListener('update', (event) => {
if (ready === false) return
const { city, country, flag } = JSON.parse(event.data)
const message = `Last visit from ${city}, ${country} ${flag}`
const isLastEmpty = $last.innerHTML === ''
if (isLastEmpty) {
$last.innerHTML = message
return
}
$next.innerHTML = message
$next.style.animation = 'fadeInUp 1s ease-in-out forwards'
$last.style.animation = 'fadeOutUp 1s ease-in-out forwards'
ready = false
$next.onanimationend = () => {
$next.style.animation = ''
$last.style.animation = ''
$last.innerHTML = message
$next.innerHTML = ''
ready = true
}
})
window.addEventListener('unload', (event) => {
source.close()
})
</script>
<style>
body {
background: #000;
display: grid;
place-content: center;
min-height: 100vh;
}
small {
font-size: 12px;
color: #f6f6f6;
text-align: center;
font-family: Menlo, monospace;
}
small span {
display: block;
height: 20px;
}
@keyframes fadeOutUp {
from {
opacity: 1;
transform: translateY(0);
}
to {
opacity: 0;
transform: translateY(-20px)
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(0px);
}
to {
opacity: 1;
transform: translateY(-20px)
}
}
</style>