|
1 | 1 |
|
2 | 2 | <style> |
| 3 | + #root |
| 4 | + {font-family:arial;--unknown:} |
| 5 | + .label |
| 6 | + {display:inline-block;width:150px;text-align:right;margin:5px 10px} |
| 7 | + #progress |
| 8 | + {border:1px solid gray;padding:2px;height:20px;margin:20px} |
| 9 | + #bar |
| 10 | + {background:lightgreen;width:0;height:100%} |
| 11 | + |
| 12 | + #bar.unknown |
| 13 | + {opacity:0.5;background-size:20px 20px;background-image:repeating-linear-gradient(-45deg,#f7b733 0,#f7b733 5px,yellow 0,yellow 50%); |
| 14 | + animation: stripes-scroll 1s linear infinite;} |
| 15 | + |
| 16 | + @keyframes stripes-scroll { |
| 17 | + from {background-position: 0 0;} |
| 18 | + to {background-position: 20px 0;} |
| 19 | + } |
| 20 | + |
3 | 21 | </style> |
4 | 22 |
|
5 | 23 | <div id=root> |
6 | 24 |
|
7 | 25 | <div> |
8 | | - encoding |
| 26 | + <span class=label> |
| 27 | + encoding |
| 28 | + </span> |
9 | 29 | <span id=encoding></span> |
10 | 30 | </div> |
11 | 31 | <div> |
12 | | - content-length |
| 32 | + <span class=label> |
| 33 | + content-length |
| 34 | + </span> |
13 | 35 | <span id=content-length></span> |
14 | 36 | </div> |
| 37 | + <div> |
| 38 | + <span class=label> |
| 39 | + content-type |
| 40 | + </span> |
| 41 | + <span id=content-type></span> |
| 42 | + </div> |
15 | 43 | <div id=progress> |
16 | 44 | <div id=bar></div> |
17 | 45 | </div> |
18 | | - |
| 46 | + <div id=result> |
| 47 | + <div> |
| 48 | + <div> |
| 49 | + <span class=label>name</span> |
| 50 | + <span id=name></span> |
| 51 | + </div> |
| 52 | + <div> |
| 53 | + <span class=label>size</span> |
| 54 | + <span id=size></span> |
| 55 | + </div> |
| 56 | + </div> |
19 | 57 | </div> |
20 | 58 |
|
21 | 59 |
|
22 | 60 | <script> |
23 | 61 |
|
24 | 62 | (async()=>{ |
25 | | -console.clear(); |
| 63 | + console.clear(); |
26 | 64 |
|
27 | | - |
28 | | - var url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip' |
| 65 | + var $ = (root,sel)=>(!sel && (sel=root,root=document),root.querySelector(sel)); |
29 | 66 |
|
30 | 67 |
|
31 | | - //var url = 'https://cdn.jsdelivr.net/gh/javascript-2020/libs/js/io/tiny-unzip/tiny-unzip.m.js'; |
| 68 | + //varr url = 'https://raw.githubusercontent.com/javascript-2020/external/main/ffmpeg/ffmpeg-wasm/ffmpeg-wasm.zip' |
| 69 | + var url = 'https://cdn.jsdelivr.net/gh/javascript-2020/libs/js/io/tiny-unzip/tiny-unzip.m.js'; |
| 70 | + |
| 71 | + var i = url.lastIndexOf('/')+1 |
| 72 | + var fn = url.slice(i); |
32 | 73 |
|
33 | 74 | var headers = {}; |
34 | 75 | var method = 'get'; |
|
38 | 79 |
|
39 | 80 | var encoding = res.headers.get('content-encoding')?.toLowerCase(); |
40 | 81 | if(encoding && encoding!=='identity'){ |
41 | | - console.log('unable to reliably computer content-length'); |
| 82 | + encoding = false; |
| 83 | + $('#bar').className = 'unknown'; |
| 84 | + $('#bar').style.width = '100%'; |
| 85 | + console.log('unable to reliably computer content-length'); |
| 86 | + }else{ |
| 87 | + encoding = true; |
42 | 88 | } |
| 89 | + |
| 90 | + var type = res.headers.get('content-type'); |
| 91 | + $('#content-type').textContent = type; |
| 92 | + |
43 | 93 | var len = res.headers.get('content-length'); |
44 | | - console.log(len); |
| 94 | + console.log(len); |
45 | 95 | var total = 0; |
46 | 96 | var reader = res.body.getReader(); |
47 | 97 | var chunks = []; |
48 | 98 | while(true){ |
49 | 99 |
|
50 | 100 | const {value,done} = await reader.read(); |
51 | 101 | if(done)break; |
52 | | - total += value.length; |
53 | | - console.log('chunk',datatype(value),value.byteLength,total,(total/len*100).toFixed(1)+' %'); |
| 102 | + total += value.length; |
54 | 103 | chunks.push(value); |
55 | | - |
| 104 | + |
| 105 | + if(encoding){ |
| 106 | + var w = (total/len*100).toFixed(1)+'%'; |
| 107 | + $('#bar').style.width = w; |
| 108 | + console.log('chunk',datatype(value),value.byteLength,total,w); |
| 109 | + } |
56 | 110 | }//while |
| 111 | + |
57 | 112 | var total = chunks.reduce((acc,chunk)=>acc+chunk.length,0); |
58 | 113 | var buf = new Uint8Array(total); |
59 | 114 | var offset = 0; |
|
63 | 118 | offset += chunk.length; |
64 | 119 |
|
65 | 120 | }//for |
| 121 | + |
66 | 122 | var blob = new Blob([buf],{type:'application/octet-stream'}); |
67 | | - console.log(blob); |
| 123 | + blob.name = fn; |
| 124 | + console.log(blob); |
68 | 125 |
|
| 126 | + $('#result #name').textContent = fn; |
| 127 | + $('#result #size').textContent = blob.size; |
69 | 128 |
|
| 129 | + $('#bar').classList.remove('unknown'); |
70 | 130 |
|
71 | 131 | function datatype(v){return Object.prototype.toString.call(v).slice(8,-1).toLowerCase()} |
72 | 132 |
|
|
0 commit comments