diff --git a/Source/content_script.js b/Source/content_script.js index 5a61aaa2..f18d3df1 100644 --- a/Source/content_script.js +++ b/Source/content_script.js @@ -1,5 +1,24 @@ walk(document.body); +if (window.MutationObserver) { + var observer = new MutationObserver(function (mutations) { + Array.prototype.forEach.call(mutations, function (m) { + if (m.type === 'childList') { + walk(m.target); + } else if (m.target.nodeType === 3) { + handleText(m.target); + } + }); + }); + + observer.observe(document.body, { + childList: true, + attributes: false, + characterData: true, + subtree: true + }); +} + function walk(node) { // I stole this function from here: @@ -29,14 +48,18 @@ function walk(node) function handleText(textNode) { - var v = textNode.nodeValue; + var oldValue = textNode.nodeValue; + v = oldValue; v = v.replace(/\bThe Cloud\b/g, "My Butt"); v = v.replace(/\bThe cloud\b/g, "My butt"); v = v.replace(/\bthe Cloud\b/g, "my Butt"); v = v.replace(/\bthe cloud\b/g, "my butt"); - textNode.nodeValue = v; + // avoid infinite series of DOM changes + if (v !== oldValue) { + textNode.nodeValue = v; + } }