Skip to content

Instantly share code, notes, and snippets.

@LeetCodes
Last active December 22, 2020 08:05
Show Gist options
  • Save LeetCodes/9d5934e721da96a016789551af2d5d98 to your computer and use it in GitHub Desktop.
Save LeetCodes/9d5934e721da96a016789551af2d5d98 to your computer and use it in GitHub Desktop.
saveResources
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