Skip to content

Instantly share code, notes, and snippets.

@vnys
Created January 11, 2016 13:05
Show Gist options
  • Save vnys/d3f223726566b3efd925 to your computer and use it in GitHub Desktop.
Save vnys/d3f223726566b3efd925 to your computer and use it in GitHub Desktop.
Reverse text on webpage
(function(body) {
var text = [],
treewalker = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false);
while(treewalker.nextNode()) {
if (treewalker.currentNode.nodeValue.charAt(0) !== '\n') {
text.push(treewalker.currentNode);
}
};
text.forEach( function(txt) {
txt.nodeValue = txt.nodeValue.split(' ').map(function(word) {
var firstLetterUppercase = word.substr(0,1) === word.substr(0,1).toUpperCase();
var segments = word.match(/([\W]+)?([\wÆØÅæøå]+)?([\W]+)?/);
if (segments[2]) {
segments[2] = segments[2].toLowerCase().split("").reverse().join("");
if (firstLetterUppercase) {
segments[2] = segments[2].charAt(0).toUpperCase() + segments[2].slice(1);
}
}
return (segments[1] || '') + (segments[2] || '') + (segments[3] || '');
}).join(' ');
})
}(document.body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment