From 2b10bd5e31b81b29b4bbd27f14630a2d4cadbbe8 Mon Sep 17 00:00:00 2001 From: Mike White Date: Thu, 24 Apr 2014 12:40:12 -0400 Subject: [PATCH] handle post-load DOM changes too --- Source/content_script.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) 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; + } }