-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
76 lines (69 loc) · 2.15 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ciaran - Food.</title>
<link rel="icon" href="./favicon.ico" type="image/x-icon">
<script>
const url = 'https://blog.ciaranfood.com';
const limit = 20;
const fetchBlogs = async (start = 0, responses = []) => {
const promises = await Promise.all(
new Array(limit)
.fill(undefined)
.map((_, index) =>
new Promise((res, error) => {
fetch(`${url}/${start + index}`)
.then(response => {
res({ response, index });
})
})
)
);
console.log(promises)
if (promises[limit - 1].response.ok) {
return fetchBlogs(start + limit, [...responses, ...promises]);
} else {
return [...responses, ...promises];
}
}
const createLink = async (response, index) => {
const text = await response.text();
const parsed = (new window.DOMParser()).parseFromString(text, "text/html");
const li = document.createElement('li');
const a = document.createElement('a');
a.innerText = parsed.title;
a.href = `${url}/${index}`;
li.appendChild(a);
document.getElementById('articles').append(li);
}
fetchBlogs()
.then(responses =>
responses
.filter(({ response }) => response.ok)
.forEach(({ response, index }) => createLink(response, index))
).finally(() => {
document.getElementById('loading-indicator').remove();
});
</script>
<style>
* {
font-family: 'Courier New', Courier, monospace;
}
main {
display: flex;
align-items: center;
flex-direction: column;
}
</style>
</head>
<body>
<main>
<h1>Some thoughts on things I've eaten</h1>
<p id="loading-indicator">Loading...</p>
<ul id="articles"></ul>
</main>
</body>
</html>