I want to write a script that will cause a page to "decay", changing characters at random. Let's say I've got some HTML that looks like this:
<div class="eebee">
Lorem Ipsum <a href="http://example.com">Anchor here</a>
</div>
And I want to replace every instance of "e" with "∑" so it'll be
<div class="eebee">
Lor∑m Ipsum <a href="http://example.com">Anchor h∑r∑</a>
</div>
but, obviously, I don't want it to be
<div class="∑∑b∑∑">
Lor∑m Ipsum <a hr∑f="http://∑xample.com">Anchor h∑r∑</a>
</div>
How to accomplish this? A DOM parser? Or just some regex, searching for content betw∑∑n ">" and "<"?
EDIT: As per Oriol's solution below, to put it into a function that accepts any find-and-replace strings:
function decay(find_string, replace_string) {
var treeWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
while(treeWalker.nextNode()) {
var node = treeWalker.currentNode;
re = new RegExp(find_string, "g");
node.nodeValue = node.nodeValue.replace(re, replace_string);
}
}