:contains returns exactly what is expected. From specification :
Corresponding text can be displayed directly in the selected element in any of these descendants of the elements or combination .
In general, to replace, wrap, or highlight text conditions on a web page, you need to work at the TEXT_NODE level. Everything else runs the risk of sorting out: URL, id, event handler, etc.
You can use Tree Walker for this:
var txtWalker = document.createTreeWalker ( document.body, NodeFilter.SHOW_TEXT, { acceptNode: function (node) { //-- Skip whitespace-only nodes if (node.nodeValue.trim() ) return NodeFilter.FILTER_ACCEPT; return NodeFilter.FILTER_SKIP; } }, false ); var txtNode = null; while (txtNode = txtWalker.nextNode () ) { var oldTxt = txtNode.nodeValue; var newTxt = oldTxt.replace (/serscripts\.org/ig, "serscripts-mirror.org"); newTxt = newTxt.replace (/(:8080|%3A8080)/ig, ""); txtNode.nodeValue = newTxt; }
this will safely take care of everything on the page except for HTML attributes like href , src , etc.
If you want to change them, use the separate target .each () code, as you said, what you are doing, but do not change the text between <a></a> , as was already done with walker.
source share