Skip to content

Instantly share code, notes, and snippets.

@CvX
Last active November 24, 2020 22:59
Show Gist options
  • Save CvX/acad4654a1bb27e184741156c57b0ea0 to your computer and use it in GitHub Desktop.
Save CvX/acad4654a1bb27e184741156c57b0ea0 to your computer and use it in GitHub Desktop.
(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