-
-
Save nicolasmendonca/0a262edfc7cbb25051ce75b993831e23 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 walkDOMTree( | |
root, | |
whatToShow = NodeFilter.SHOW_ALL, | |
{ inspect, collect, callback } = {} | |
) { | |
const walker = document.createTreeWalker(root, whatToShow, { | |
acceptNode(node) { | |
if (inspect && !inspect(node)) { | |
return NodeFilter.FILTER_REJECT; | |
} | |
if (collect && !collect(node)) { | |
return NodeFilter.FILTER_SKIP; | |
} | |
return NodeFilter.FILTER_ACCEPT; | |
}, | |
}); | |
const nodes = []; | |
let n; | |
while ((n = walker.nextNode())) { | |
callback?.(n); | |
nodes.push(n); | |
} | |
return nodes; | |
} | |
const PARENT_TAGS_TO_EXCLUDE = ["STYLE", "SCRIPT", "TITLE"]; | |
function getAllTextNodes(el) { | |
return walkDOMTree(el, NodeFilter.SHOW_TEXT, { | |
inspect: (textNode) => | |
!PARENT_TAGS_TO_EXCLUDE.includes(textNode.parentElement?.nodeName), | |
}); | |
} | |
function generateRandomString(length) { | |
var result = ""; | |
var characters = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
var charactersLength = characters.length; | |
for (var i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
const textNodes = getAllTextNodes(document.body); | |
textNodes.forEach((node) => { | |
const textNodeLength = node.textContent.length; | |
node.textContent = generateRandomString(textNodeLength * 3); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment