Last active
February 17, 2025 08:49
-
-
Save taowen/2abd3ef2e2dacbce0050ee8616dfb5af 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
javascript:(function(){ | |
const getComputedStyleString = (element) => { | |
const computed = window.getComputedStyle(element); | |
let styles = ''; | |
for (const prop of computed) { | |
if (prop.includes('"')) { | |
continue; | |
} | |
const value = computed.getPropertyValue(prop); | |
if (value) { | |
styles += `${prop}:${value.replaceAll('"', "'")};`; | |
} | |
} | |
return styles; | |
}; | |
const dumpNode = (node) => { | |
if (node.nodeType === Node.TEXT_NODE) { | |
return node.textContent.trim() ? node.textContent : ''; | |
} | |
if (node.nodeType === Node.ELEMENT_NODE) { | |
const tagName = node.tagName.toLowerCase(); | |
if (tagName === 'script') { return ''; } | |
if (tagName === 'br') { return '<br/>'; } | |
const styles = getComputedStyleString(node); | |
const rect = node.getBoundingClientRect(); | |
let result = `<${tagName} style="${styles}" data-rect="[${rect.left},${rect.top},${rect.width},${rect.height}]"`; | |
for (const attr of node.attributes) { | |
if (attr.name !== 'style') { | |
if (tagName === 'img' && attr.name === 'src' && !attr.value.match(/^(https?:\/\/|data:|blob:)/)) { | |
const absoluteUrl = new URL(attr.value, window.location.href).href; | |
result += ` ${attr.name}="${absoluteUrl}"`; | |
} else { | |
result += ` ${attr.name}="${attr.value}"`; | |
} | |
} | |
} | |
result += '>'; | |
for (const child of node.childNodes) { | |
result += dumpNode(child); | |
} | |
result += `</${tagName}>`; | |
return result; | |
} | |
return ''; | |
}; | |
const htmlContent = dumpNode(document.body); | |
const blob = new Blob([`<html><head><meta charset="UTF-8"></head>${htmlContent}</html>`], { type: "text/html" }); | |
const url = URL.createObjectURL(blob); | |
const link = document.createElement('a'); | |
link.href = url; | |
link.download = "page-dump.html"; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
URL.revokeObjectURL(url); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment