Last active
November 24, 2020 22:59
-
-
Save CvX/acad4654a1bb27e184741156c57b0ea0 to your computer and use it in GitHub Desktop.
This file contains 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
(function() { | |
let sanitizeTree = (tree) => { | |
let walk = document.createTreeWalker(tree); | |
let node; | |
while (node = walk.nextNode()) { | |
if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== '') { | |
if (node.parentNode && (node.parentNode.tagName || '').toLowerCase() === 'script') { | |
node.textContent = '// 🙂'; | |
} else { | |
node.textContent = '🙂'; | |
} | |
} | |
let tagName = (node.tagName || '').toLowerCase(); | |
if (!node.attributes) continue; | |
for (let attribute of node.attributes) { | |
let attributeName = (attribute.name || '').toLowerCase(); | |
if (attributeName === 'class') { | |
continue; | |
} else if (attributeName === 'id' && tagName === 'html') { | |
continue; | |
} else if (attributeName === 'src' && tagName === 'script') { | |
continue; | |
} else if (tagName === 'link' && node.attributes['rel'] && node.attributes['rel'].value.toLowerCase() === 'stylesheet') { | |
continue; | |
} else if (attributeName === 'style') { | |
let cleanValue = attribute.value.replace(/url\([^)]*\)/g, 'url(🙂)'); | |
node.setAttribute(attribute.name, cleanValue); | |
continue; | |
} else { | |
node.setAttribute(attribute.name, '🙂'); | |
} | |
} | |
} | |
return tree; | |
}; | |
let tree = sanitizeTree(document.cloneNode(true)); | |
let output = [...tree.childNodes].map((node) => node.outerHTML).join(''); | |
copy(output); | |
console.log(output); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment