0

I used the following TreeWalker as a template from this post https://stackoverflow.com/a/37178130/7102491 and modified it to skip the element 'script', to prevent certain pages like a google search from breaking to no avail. Does anyone know how I can change the code to prevent breaking certain pages? Thanks.

var replaceArry = [
    [/b/gi,    'better'],
    [/Terms of service/gi,          'Términos y condiciones'],
    [/Privacy policy/gi,            'Privacidad'],
    // etc.
];
var numTerms    = replaceArry.length;
var txtWalker   = document.createTreeWalker (
    document.body,
    NodeFilter.SHOW_TEXT,
    {   acceptNode: function (node) {
            //-- Skip whitespace-only nodes
            if (node.nodeValue.trim() && node.parentNode.nodeName != 'SCRIPT')
                return NodeFilter.FILTER_ACCEPT;

            return NodeFilter.FILTER_SKIP;
        }
    },
    false
);
var txtNode     = null;

while (txtNode  = txtWalker.nextNode () ) {
    var oldTxt  = txtNode.nodeValue;

    for (var J  = 0;  J < numTerms;  J++) {
        oldTxt  = oldTxt.replace (replaceArry[J][0], replaceArry[J][1]);
    }
    txtNode.nodeValue = oldTxt;
}
Shah
  • 1
  • 1
  • 1
  • 4
  • Use TreeWalker API: [Replace many text terms, using Tampermonkey, without affecting URLs and not looking for classes or ids](//stackoverflow.com/a/24419809) – wOxxOm Jul 16 '17 at 09:45
  • @wOxxOm hey thanks for the response. Unfortunately I don't think it quite works. I used that exact code and modified it to replace only a character, and it still breaks a google search. – Shah Jul 16 '17 at 10:31
  • 1
    I guess you need to skip some elements like maybe `script`. – wOxxOm Jul 16 '17 at 10:44
  • @wOxxOm thanks again. do you think you could point me to some documentation to do that? not really too familiar with tree walker if i'm being honest. i appreciate the help – Shah Jul 16 '17 at 11:40
  • TreeWalker documentation is just one page which you can easily google up yourself, but it seems not relevant to the problem, so you can skip the element in your code just the same by simply checking `node.localName`. – wOxxOm Jul 16 '17 at 11:48
  • @wOxxOm hey, i edited my original post. Still having problems so if you could help out it'd be really appreciated. Thanks again – Shah Jul 17 '17 at 02:30
  • Please [edit] the question to be on-topic: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jul 18 '17 at 02:15
  • Define "break". Also provide exact information as to URL(s) which break so we can duplicate the problem. Without a [mcve] which duplicates the problem, we're just guessing as to what the issue is. Note: you should also skip text nodes that are children of ` – Makyen Jul 18 '17 at 02:19

0 Answers0