Last active
December 22, 2020 08:05
-
-
Save LeetCodes/9d5934e721da96a016789551af2d5d98 to your computer and use it in GitHub Desktop.
saveResources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const penURL=document.head.querySelector('link[rel="canonical"]').href.replace(/\?.*/gm, ''); | |
const URLS = window.performance | |
.getEntriesByType("resource") | |
.map((_) => _.name) | |
.filter((_) => !/static\.codepen\.io/.test(_)); | |
((modules) => { | |
return Promise.all( | |
modules.map(function (m) { | |
return import( | |
"https://dev.jspm.io/" + (m.name ? m.name : m) | |
).then(({ default: module }) => [m.varName ? m.varName : m, module]); | |
}) | |
).then((modules) => { | |
let mods = {}; | |
modules.forEach((mod) => (mods[mod[0]] = mod[1])); | |
return mods; | |
}); | |
})([ | |
{ name: "jszip", varName: "JSZip" }, | |
{ name: "file-saver", varName: "saveAs" } | |
]).then((_) => { | |
Object.assign(this, _); | |
const zip = new JSZip(); | |
const filename = "sauce.zip"; | |
['script.js','style.css', 'index.html'].map(_=>{ | |
const url=`${penURL}.${_.replace(/.*\./, '')}` | |
const blobPromise = fetch(url).then((r) => { | |
if (r.status === 200) return r.blob(); | |
return Promise.reject(new Error(r.statusText)); | |
}); | |
zip.file(_, blobPromise); | |
}) | |
URLS.forEach((url) => { | |
const blobPromise = fetch(url).then((r) => { | |
if (r.status === 200) return r.blob(); | |
return Promise.reject(new Error(r.statusText)); | |
}); | |
const name = url.match(/.*\/(.*\.(.*))$/); | |
zip.folder(name[2]).file(name[1], blobPromise); | |
}); | |
zip.generateAsync({ type: "blob" }) | |
.then((blob) => saveAs(blob, filename)) | |
.catch((e) => console.log(e)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment