Something like this
function replaceKeywords(keyword, newWord) {
for (var i = 0; i < document.childNodes.length; i++) {
checkNode(document.childNodes[i]);
}
function checkNode(node) {
var nodeName = node.nodeName.toLowerCase();
if (nodeName === 'script' || nodeName === 'style' || ~(node.className || "").indexOf('ignore')) { return; }
if (node.nodeType === 3) {
var text = node.nodeValue;
var regEx = new RegExp(keyword, 'gi');
var newText = text.replace(regEx, newWord);
node.nodeValue = newText;
}
if (node.childNodes.length > 0) {
for (var j = 0; j < node.childNodes.length; j++) {
checkNode(node.childNodes[j]);
}
}
}
}
//Use like
replaceKeywords('keyword','cow');
Uses javascript to walk the entire DOM tree, looking specifically for textNodes (nodeType 3) and uses a regex to replace the keyword with the newWord (ignoring case), then recursively through each nodes children. Ignores <script>
, <style>
and elements with the class of ignore
.