Created
July 4, 2017 02:34
-
-
Save voltrevo/9a5fa8018e77d31c14088efbf74d07eb to your computer and use it in GitHub Desktop.
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
(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