Skip to content

Instantly share code, notes, and snippets.

@voltrevo
Created July 4, 2017 02:34
Show Gist options
  • Save voltrevo/9a5fa8018e77d31c14088efbf74d07eb to your computer and use it in GitHub Desktop.
Save voltrevo/9a5fa8018e77d31c14088efbf74d07eb to your computer and use it in GitHub Desktop.
(async function() {
let headHTML = document.head.innerHTML;
await Promise.all(
Array.from(document.head.querySelectorAll('link[rel=stylesheet]'))
.map(el => fetch(el.href)
.then(res => res.text())
.then((cssTxt) => {
headHTML += `<style>\n${cssTxt}\n</style>`;
})
)
);
const innerHTML = `<!DOCTYPE html>
<html>
<head>
${headHTML}
</head>
<body>
${document.body.innerHTML}
</body>
</html>
`;
const pageName = escape(
document.title ||
(() => {
const parts = window.location.href
.replace(window.location.search, '')
.split('/')
.filter(part => part.trim() !== '')
;
return parts[parts.length - 1] || 'page';
})()
) + '.html';
const link = document.createElement('a');
link.setAttribute('download', pageName);
link.setAttribute('href', 'data:text/html;base64,' + btoa(innerHTML));
link.textContent = pageName;
document.body.appendChild(link);
link.click();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment