-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
104 lines (93 loc) · 3.33 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Your Page Title</title>
<style>
article {
margin: 20px;
border-bottom: 1px dotted gray;
}
</style>
<script>
let count = 0;
const lineregexp = /([^,]*),([^,]*),(.*)/g;
function processLine(line) {
if (line.startsWith("Druid")) return; // Ignore header
count++;
if (count > 1000) return;
let array = line.split(",", 3);
const result = Array.from(line.matchAll(lineregexp))[0];
const druid = result[1];
const file = result[2];
let description = result[3];
const article = document.createElement("article");
const caption = document.createElement("p");
description = description.replace(/^"(.*)"$/, "$1");
caption.textContent = `${druid} ${description}`;
const container = document.getElementById("container");
const img = document.createElement("img");
img.loading = "lazy";
img.src = `https://stacks.stanford.edu/image/iiif/${druid}/${file.replace(".jp2", "")}/full/!800,800/0/default.jpg`;
article.appendChild(img);
article.appendChild(caption);
container.appendChild(article);
}
// Function to fetch and stream the text file
function fetchAndStreamTextFile(url) {
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(
`Failed to fetch the file: ${response.statusText}`,
);
}
// Create a reader for the response body
return response.body.getReader();
})
.then((reader) => {
const decoder = new TextDecoder("utf-8");
let remainder = "";
function readChunk() {
reader
.read()
.then(({ value: chunk, done }) => {
if (done) {
// If there is a remainder after all chunks are read, process it
if (remainder) {
processLine(remainder);
}
return; // Exit the function when reading is done
}
// Decode the current chunk
const text =
remainder + decoder.decode(chunk, { stream: true });
// Split the text into lines
const lines = text.split(/\r?\n/);
// Save the last partial line to process with the next chunk
remainder = lines.pop();
// Process each complete line
lines.forEach((line) => processLine(line));
// Read the next chunk
readChunk();
})
.catch((err) => {
console.error("Error reading chunk:", err);
});
}
// Start reading chunks
readChunk();
})
.catch((err) => {
console.error("Error fetching and streaming file:", err);
});
}
fetchAndStreamTextFile("image_search_output_b.csv");
</script>
</head>
<body>
<h1>Images</h1>
<section id="container"></section>
</body>
</html>