|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +<html> |
| 5 | + |
| 6 | + <head> |
| 7 | + |
| 8 | + <title> |
| 9 | + cors extension |
| 10 | + </title> |
| 11 | + |
| 12 | + <style> |
| 13 | + html |
| 14 | + {font-family:arial} |
| 15 | + |
| 16 | + #hdr |
| 17 | + {display:flex;justify-content:space-between} |
| 18 | + |
| 19 | + .item |
| 20 | + {display:flex;gap:30px} |
| 21 | + |
| 22 | + .item a |
| 23 | + {width:200px;text-align:right} |
| 24 | + |
| 25 | + </style> |
| 26 | + </head> |
| 27 | + |
| 28 | + |
| 29 | + <body> |
| 30 | + |
| 31 | + <div id=hdr> |
| 32 | + <div> |
| 33 | + <a href='https://ext-code.com/'> |
| 34 | + home |
| 35 | + </a> |
| 36 | + </div> |
| 37 | + |
| 38 | + <h3> |
| 39 | + cors extension |
| 40 | + </h3> |
| 41 | + |
| 42 | + <div> |
| 43 | + 17 Dec 2025 |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + |
| 47 | + |
| 48 | + <div class=desc> |
| 49 | + add cors headers to requests |
| 50 | + </div> |
| 51 | + |
| 52 | + |
| 53 | + <div> |
| 54 | + <div id=chrome style='text-decoration:underline'> |
| 55 | + download chrome |
| 56 | + </div> |
| 57 | + <div id=firefox style='text-decoration:underline'> |
| 58 | + download firefox |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + </body> |
| 65 | + |
| 66 | + |
| 67 | + <script> |
| 68 | + |
| 69 | + var $ = (root,css)=>(!css && (css=root,root=document),root.querySelector(css)); |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + window.onload = async function(){ |
| 74 | + |
| 75 | + await libs(); |
| 76 | + initdom(); |
| 77 | + |
| 78 | + }//onload |
| 79 | + |
| 80 | + |
| 81 | + //: |
| 82 | + |
| 83 | + |
| 84 | + async function libs(){ |
| 85 | + |
| 86 | + jszip = await import('https://cdn.jsdelivr.net/npm/jszip/+esm'); |
| 87 | + jszip = jszip.default; |
| 88 | + |
| 89 | + }//libs |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + //: |
| 94 | + |
| 95 | + |
| 96 | + var btn = {}; |
| 97 | + |
| 98 | + |
| 99 | + //: |
| 100 | + |
| 101 | + |
| 102 | + funciton initdom(){ |
| 103 | + |
| 104 | + $('#chrome').onclick = btn.chrome; |
| 105 | + $('#firefox').onclick = btn.firefox; |
| 106 | + |
| 107 | + }//initdom |
| 108 | + |
| 109 | + |
| 110 | + //: |
| 111 | + |
| 112 | + |
| 113 | + btn.chrome = function(){ |
| 114 | + |
| 115 | + var dir = { |
| 116 | + 'xml-http-request' :{directory:{ |
| 117 | + 'xhr-upload.html' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/xhr-upload.html'}}}, |
| 118 | + 'upload-server.js' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/upload-server.js'}}}, |
| 119 | + 'xhr-download.html' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/xhr-download.html'}}}, |
| 120 | + 'download-server.js' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/download-server.js'}}}, |
| 121 | + node_modules : {directory:{ |
| 122 | + 'a.txt' : {file:{contents:'a'}}, |
| 123 | + }} |
| 124 | + }} |
| 125 | + }; |
| 126 | + |
| 127 | + var blob = await create(dir,{download:false,df:true}); |
| 128 | + console.log('zip',blob.type,blob.size); |
| 129 | + }//chrome |
| 130 | + |
| 131 | + |
| 132 | + btn.firefox = function(){ |
| 133 | + |
| 134 | + var dir = { |
| 135 | + 'xml-http-request' :{directory:{ |
| 136 | + 'xhr-upload.html' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/xhr-upload.html'}}}, |
| 137 | + 'upload-server.js' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/upload-server.js'}}}, |
| 138 | + 'xhr-download.html' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/xhr-download.html'}}}, |
| 139 | + 'download-server.js' : {file:{github:{repo:'javascript-2020.github.io',path:'/blog/25-07-31/xml-http-request/ex/download-server.js'}}}, |
| 140 | + node_modules : {directory:{ |
| 141 | + 'a.txt' : {file:{contents:'a'}}, |
| 142 | + }} |
| 143 | + }} |
| 144 | + }; |
| 145 | + |
| 146 | + var blob = await create(dir,{download:false,df:true}); |
| 147 | + |
| 148 | + }//firefox |
| 149 | + |
| 150 | + |
| 151 | + //: |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + console.log('zip',blob.type,blob.size); |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + async function create(dir,{download=true,test,df=false}={}){ |
| 160 | + df && console.log('download',!!download); |
| 161 | + df && console.log('test',!!test); |
| 162 | + df && console.json(dir); |
| 163 | + df && console.log(); |
| 164 | + var zip; |
| 165 | + if(!test){ |
| 166 | + zip = new jszip(); |
| 167 | + } |
| 168 | + |
| 169 | + var resolve,promise=new Promise(res=>resolve=res); |
| 170 | + var ct=0,total=0; |
| 171 | + add(dir); |
| 172 | + await promise; |
| 173 | + |
| 174 | + var blob; |
| 175 | + if(zip){ |
| 176 | + blob = await zip.generateAsync({type:'blob'}); |
| 177 | + |
| 178 | + if(download){ |
| 179 | + var url = window.URL.createObjectURL(blob); |
| 180 | + var a = document.createElement('a'); |
| 181 | + a.download = 'https-file-server'; |
| 182 | + a.href = url; |
| 183 | + a.click(); |
| 184 | + } |
| 185 | + } |
| 186 | + return blob; |
| 187 | + |
| 188 | + |
| 189 | + async function add(dir,abs=''){ |
| 190 | + |
| 191 | + for(let key in dir){ |
| 192 | + |
| 193 | + ct++; |
| 194 | + var o = dir[key]; |
| 195 | + |
| 196 | + if(o.directory){ |
| 197 | + df && console.log('dir :',abs+key); |
| 198 | + if(zip){ |
| 199 | + zip.folder(abs+key); |
| 200 | + } |
| 201 | + add(o.directory,abs+key+'/'); |
| 202 | + complete(); |
| 203 | + } |
| 204 | + |
| 205 | + if(o.file?.contents){ |
| 206 | + df && console.log('file create :',abs+key); |
| 207 | + if(zip){ |
| 208 | + zip.file(abs+key,o.file.contents); |
| 209 | + } |
| 210 | + complete(); |
| 211 | + } |
| 212 | + |
| 213 | + if(o.file?.github){ |
| 214 | + var owner,repo,branch,path; |
| 215 | + if(typeof o.file.github=='string'){ |
| 216 | + path = o.file.github; |
| 217 | + }else{ |
| 218 | + ({owner,repo,branch,path} = o.file.github); |
| 219 | + } |
| 220 | + owner ||= 'javascript-2020'; |
| 221 | + repo ||= 'libs'; |
| 222 | + branch ||= 'main'; |
| 223 | + if(path.startsWith('/')){ |
| 224 | + path = path.slice(1); |
| 225 | + } |
| 226 | + df && console.log('file github :',abs+key); |
| 227 | + df && console.log(owner,repo,branch,path); |
| 228 | + if(zip){ |
| 229 | + fetch(`https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${path}`) |
| 230 | + .then(res=>res.text().then(txt=>{ |
| 231 | + |
| 232 | + zip.file(abs+key,txt); |
| 233 | + complete(); |
| 234 | + |
| 235 | + })); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + |
| 240 | + }//for |
| 241 | + |
| 242 | + |
| 243 | + function complete(){ |
| 244 | + |
| 245 | + total++; |
| 246 | + if(ct==total){ |
| 247 | + resolve(); |
| 248 | + } |
| 249 | + |
| 250 | + }//complete |
| 251 | + |
| 252 | + }//add |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + }//create |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + </script> |
| 263 | + |
| 264 | +</html> |
| 265 | + |
| 266 | + |
0 commit comments