Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // create a TreeWalker of all text nodes
  2. var allTextNodes = document.createTreeWalker(document.body,NodeFilter.SHOW_TEXT),
  3. // some temp references for performance
  4. tmptxt,
  5. tmpnode,
  6. // compile the RE and cache the replace string, for performance
  7. cakeRE = /cake/g
  8. replaceValue = "pie";
  9.  
  10. // iterate through all text nodes
  11. while (allTextNodes.nextNode()) {
  12. tmpnode = allTextNodes.currentNode;
  13. tmptxt = tmpnode.nodeValue;
  14. tmpnode.nodeValue = tmptxt.replace(cakeRE,replaceValue);
  15. }
  16.  
  17. // the innerHTML property of any DOM node is a string
  18. document.body.innerHTML = document.body.innerHTML.replace(/cake/g,'pie')
  19.  
  20. var search_re = new RegExp("(?:>[^<]*)(" + stringToReplace + ")(?:[^>]*<)", "gi");
  21.  
  22. // replace some chunk of stuff, the first section of your page works nicely
  23. // if you happen to have that organization
  24. //
  25. setTimeout(function() { /* replace the rest */ }, 10);
  26.  
  27. var tmp = element.innerHTML.replace(search_re, whatever);
  28. /* more replace calls, maybe this is in a for loop, i don't know what you're doing */
  29. element.innerHTML = tmp;
  30.  
  31. $('body').html($('body').html().replace('pie','cake'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement