0

I'm working on a Chrome extension, and it's a simple find-and-replace that grabs text using a tree walker. It works on most sites, but not Facebook or Google. Here's my code:

 walk(document.body);

function walk(rootNode)
{
    // Find all the text nodes in rootNode
    var walker = document.createTreeWalker(
                                       rootNode,
                                       NodeFilter.SHOW_TEXT,
                                       null,
                                       false
                                       ),
    node;

    // Modify each text node's value
    while (node = walker.nextNode()) {
        handleText(node);
    }
}

function handleText(textNode) {
    textNode.nodeValue = replaceText(textNode.nodeValue);
}

And then I have another function called replaceText() that runs fine. I suspect the issue is with how document.createTreeWalker() works on Facebook and Google due to their sites' structures. Does anyone know what the issue might be?

Kyle Somers
  • 614
  • 1
  • 8
  • 21
  • Console got anything to say? I don't think there's anything much specific about those site's structures - the DOM works the same way for all of us. (They could perhaps overwrite your starting point, document.body [possible under certain conditions], but I rather doubt that, I think that would have too many side effects.) I'd rather expect other measures at work, such as Content Security Policy (although I am not sure how much extensions are "above that.") – CBroe Sep 23 '16 at 18:04
  • 2
    There's no mystery. Both sites add content dynamically. [Is there a JavaScript/jQuery DOM change listener?](https://stackoverflow.com/a/39508954) – wOxxOm Sep 23 '16 at 18:18
  • That definitely helps, thanks. – Kyle Somers Sep 23 '16 at 19:07

0 Answers0