Created
July 2, 2024 14:09
-
-
Save cemerson/66931c35a63778370edc6880d5d20419 to your computer and use it in GitHub Desktop.
javascript - obfuscate all html text
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 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