Skip to content

Instantly share code, notes, and snippets.

@unel
Created September 4, 2020 13:19
Show Gist options
  • Save unel/dd6d8cb4c7cb460182da7dfef8c99284 to your computer and use it in GitHub Desktop.
Save unel/dd6d8cb4c7cb460182da7dfef8c99284 to your computer and use it in GitHub Desktop.
function cleanup(node) {
const removeMe =
node instanceof Image
|| node instanceof Text
|| node instanceof Comment
|| node.tagName === 'svg'
|| node.tagName === 'iframe';
const ignoreChilds =
node.tagName === 'svg'
|| node.tagName === 'iframe';
if (node.textContent && !node.childNodes?.length) {
node.textContent = '';
}
else if (node.childNodes?.length && !ignoreChilds) {
for (const child of node.childNodes) {
cleanup(child);
}
}
else if (removeMe) {
action = 'remove';
node.remove();
}
}
cleanup(document.body);
console.log('done');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment