Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created July 2, 2024 14:09
Show Gist options
  • Save cemerson/66931c35a63778370edc6880d5d20419 to your computer and use it in GitHub Desktop.
Save cemerson/66931c35a63778370edc6880d5d20419 to your computer and use it in GitHub Desktop.
javascript - obfuscate all html text
function getRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function replaceTextNodesWithRandomString(node) {
// If the node is a text node, replace its content
if (node.nodeType === Node.TEXT_NODE) {
const textLength = node.nodeValue.length;
node.nodeValue = getRandomString(textLength);
} else {
// If the node is not a text node, recursively replace text nodes in its children
node.childNodes.forEach(child => replaceTextNodesWithRandomString(child));
}
}
//// Start the replacement from the body element
// replaceTextNodesWithRandomString(document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment