Created
January 11, 2016 13:05
-
-
Save vnys/d3f223726566b3efd925 to your computer and use it in GitHub Desktop.
Reverse text on webpage
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(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