-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
84 lines (74 loc) · 3.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
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node JS Workshop using API Twitter</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<style>
img {
object-fit: cover;
max-height: 100%;
width: 100%;
min-height: 200px;
}
.image-wrapper {
overflow: hidden;
height: 200px;
}
</style>
<script>
const socket = new WebSocket(`ws://${window.location.hostname}:${window.location.port}`);
let container;
document.addEventListener("DOMContentLoaded", function(event) {
container = document.querySelector("#insertImages");
});
socket.addEventListener("message", async function (event) {
let data = []
try {
data = await event.data.text()
console.log("message from server: ", data)
await loadImage(data)
// socket.send(data)
} catch (err) {
//
}
})
socket.addEventListener('open', function (event) {
console.log("connected", event)
});
function loadImage(url) {
let div = document.createElement('div');
let img = document.createElement('img');
img.src = url
div.classList.add('col')
div.classList.add('image-wrapper')
div.appendChild(img)
if (container.hasChildNodes()) {
container.insertBefore(div, container.firstChild)
} else {
container.appendChild(div)
}
}
function sendKeyword() {
let keywords = document.getElementById('keywords').value
socket.send(keywords)
}
</script>
</head>
<body>
<div class="container my-2">
<h1 class="fs-2 text-center">See twitter images posted in real time.</h1>
</div>
<div class="container my-4">
<div class="input-group">
<input id="keywords" type="text" class="form-control" placeholder="Keyword1, Keyword2, ...">
<button type="button" class="btn btn-dark" onclick="sendKeyword()">Get images</button>
</div>
</div>
<div class="container">
<div id="insertImages" class=" row row-cols-3 gy-3"></div>
</div>
</body>
</html>