forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
79 lines (66 loc) · 2.3 KB
/
fetch.js
File metadata and controls
79 lines (66 loc) · 2.3 KB
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
const pr = console.log;
// let url = 'https://ipv4-c062-lax009-ix.1.oca.nflxvideo.net/speedtest/range/0-26214400?c=us&n=26421&v=109&e=1662347532&t=Z8Ooso0dW0HLf3O1CYaDqCRxpDgLIWFmu3yPJQ';
// let url =
// ' https://speedtest.oacys.com.prod.hosts.ooklaserver.net:8080/download?nocache=8a09403e-b991-4716-a234-eb10ccfea7ae&size=100000&guid=a78f57b0-2d31-11ed-be0d-85d48b1baa5e';
let rand_str = '&rnd=' + String(Math.random()).slice(2);
url =
'https://images.unsplash.com/photo-1514065549995-7706f6017a27?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=349&q=80 ';
const MaxIter = 1;
let i = 0,
intervalId = null;
function download(url) {
if (i >= MaxIter) {
clearInterval(intervalId);
return;
}
i++;
const t0 = new Date();
fetch(url, { mode: 'no-cors' })
.then((response) => response.blob())
// .then((blob) => streamBlob0(blob, { t0: t0, i: i }));
.then((blob) => downloadImage(blob, { t0: t0, i: i }));
}
const img = document.querySelector('img');
function downloadImage(url, options) {
fetch(url, options)
.then((response) => response.blob())
.then((blob) => window.URL.createObjectURL(blob))
.then((url) => (img.src = url))
.catch(console.error);
}
// let tortoise = './tortoise.png';
let tortoise =
'https://mdn.github.io/dom-examples/streams/grayscale-png/tortoise.png';
downloadImage(url + rand_str);
function streamBlob0(blob, params) {
let reader = blob.stream().getReader(),
size = 0;
reader.read().then(function processData({ done, value }) {
if (done) {
pr(params.i, 'stream completed');
const t1 = new Date();
pr({ t0: params.t0, dt: t1 - params.t0, size: size });
return;
}
pr('not done yet');
size += value.length;
return reader.read().then(processData);
});
}
// intervalId = setInterval(() => download(url), 1000);
// fetch or embed text ====================
let S = null;
function getJson(href, root) {
fetch(href)
.then((response) => response.text())
.then((text) => {
root.textContent = text;
pr(text);
// const obj = eval(text);
S = JSON.parse(text);
pr(S);
});
}
getJson('./data.json', document.querySelector('#text'));
let el = document.querySelector('#embed');
pr('try to get from embed: ', document.querySelector('#embed'));