-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchg1.js
41 lines (32 loc) · 1.09 KB
/
fetchg1.js
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
async function fetchG1(
path = "https://oglobo-scraper.s3-sa-east-1.amazonaws.com/g1_scrape.json",
loader_selector = ".progress-bar"
) {
//ex: data = await fetchG1('https://oglobo-scraper.s3-sa-east-1.amazonaws.com/g1_scrape.json','.progress-bar')
let loader = document.querySelector(loader_selector);
let req = await fetch(path);
let reader = req.body.getReader();
let total = Number(req.headers.get("content-length"));
let loaded = 0;
let chunks = []; // array of received binary chunks (comprises the body)
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
chunks.push(value);
loaded += value.length;
const progress = ((loaded / total) * 100).toFixed(2);
loader.style.width = `${progress}%`;
loader.innerText = `${progress}%`;
}
let chunksAll = new Uint8Array(loaded);
let position = 0;
for (let chunk of chunks) {
chunksAll.set(chunk, position);
position += chunk.length;
}
// Step 5: decode into a string
let result = new TextDecoder("utf-8").decode(chunksAll);
return JSON.parse(result);
}