Skip to content

Instantly share code, notes, and snippets.

const replaceOnDocument = (pattern, string, { target = document.body } = {}) => {
[target, ...target.querySelectorAll("*:not(script):not(noscript):not(style)")]
.forEach(({ childNodes: [...nodes] }) => nodes
.filter(({ nodeType }) => nodeType === document.TEXT_NODE).forEach((textNode) => (textNode.textContent = textNode.textContent.replace(patter, string)))
);
replaceOnDocument("search","replace");
// retrives all childNodes of body