Skip to content

Instantly share code, notes, and snippets.

@ELLIOTTCABLE
Created September 6, 2011 04:23
Show Gist options
  • Select an option

  • Save ELLIOTTCABLE/1196576 to your computer and use it in GitHub Desktop.

Select an option

Save ELLIOTTCABLE/1196576 to your computer and use it in GitHub Desktop.
<script type='text/javascript'>var onAllText, replaceText, slice = Array.prototype.slice;
onAllText = function(_){ return function(parentNode){
slice.apply(parentNode.childNodes).forEach(function(childNode){
switch(childNode.nodeType) { case Node.ELEMENT_NODE: onAllText(_)(childNode); break;
case Node.TEXT_NODE: _ (childNode); break;
default: console.log(new(Error)("What the fuck is this shit?"),
childNode)} }) }}
replaceText = function(textNode, query, replacement){ var index; var before, after;
replacement = replacement.cloneNode(true);
if ( (index = textNode.nodeValue.indexOf(query)) !== -1 ) {
before = document.createTextNode( textNode.nodeValue.substr(0, index) )
after = document.createTextNode( textNode.nodeValue.substr(index + query.length) )
textNode.parentNode.insertBefore(replacement, textNode)
textNode.parentNode.insertBefore(before, replacement)
textNode.parentNode.replaceChild(after, textNode) }}
window.onBodyLoad = function(){ var pawsNode;
// All of this, just to avoid innerHTML. >,>
pawsNode = document.createElement('span'); pawsNode.classList.add('vanity')
pawsNode.appendChild(document.createTextNode('Paws'))
slice.apply(document.getElementsByTagName('p')).forEach( onAllText(function(textNode){
replaceText(textNode, 'Paws', pawsNode) }))
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment