Skip to content

Instantly share code, notes, and snippets.

@pellepim
Last active February 11, 2017 20:37
Show Gist options
  • Save pellepim/f0939a037f502a24767d to your computer and use it in GitHub Desktop.
Save pellepim/f0939a037f502a24767d to your computer and use it in GitHub Desktop.
(function () {
var words = ['President Donald J. Trump', 'President Donald Trump', 'President Trump', 'Donald John Trump',
'Donald J. Trump', 'Donald Trump', 'Mr. Trump', 'Trump'];
var replace_with = ['The Kwisatz Haderach', 'The God Emperor', 'The Tango Faced Twatnacker',
'The Peerless Abortion of Good Taste', 'Der Furher',
'The Knight of the Abyss', 'Myrna Minkoff\'s Horny Uncle',
'The Glorious Leader and Saviour of Western Civilisation',
'His Abhorrent Saltiness'];
var replacepatterns = [];
for (var i = 0; i < words.length; i++) {
replacepatterns.push(new RegExp(words[i], 'ig'))
}
function replaceText(node) {
var current = node.nodeValue;
for (var i = 0; i < replacepatterns.length; i++) {
var replacer = replace_with[Math.floor(Math.random() * replace_with.length)];
current = current.replace(replacepatterns[i], replacer);
}
node.nodeValue = current;
}
function traverse(node) {
var children = node.childNodes;
var childLen = children.length;
for (var i = 0; i < childLen; i++) {
var child = children.item(i);
if (child.nodeType == 3)//or if(child instanceof Text)
replaceText(child);
else
traverse(child);
}
}
traverse(document.body);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment