Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:16:22+00:00 2026-06-01T23:16:22+00:00

Update: With guidance from am no I am, I’ve solved this; working code at

  • 0

Update: With guidance from “am no I am”, I’ve solved this; working code at the end.

I’m working on my first Chrome extension at the moment, which is intended to perform text replacement on viewed webpages. I’ve been going all over trying to get my head around DOM manipulation, and I’ve eventually come up with the following. In theory this should go through the whole DOM table, find text nodes, clone them, replace the text (where applicable), and replace the original node with the clone.

Here’s my manifest.json…

{
    "name": "My app",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Do thing.",
    "permissions": [
        "tabs", "http://*/"
    ],
    "content_scripts": [
        {
            "matches": ["http://*/*"],
            "js": ["myapp.js"],
            "run_at": "document_end"
        }
    ]
}

And the myapp.js;

function myapp() {
    var nodes = document.getElementsByTagName("*");
        for(var i = 0; i < nodes.length; i++) {
             if(nodes[i].type == "text") {
                  var parent = nodes[i].parentnode;
                  var newnode = nodes[i].cloneNode(true);
                  newnode.data.replace("test text","replacement test text");
                  parent.replaceChild(newnode, nodes[i]);
             };
        };
};
myapp();

I have the extension imported and activated. Chrome’s javascript console has shown errors with it previously, which implies it’s running, but has none now, which implies no basic typo-style errors. I assume either I’m wrong about how to alter or swap the text nodes, or have a basic misunderstanding of how to set up the manifest file or the js file, but I’ve hit a wall trying to figure out which.

Any help would be gratefully received!

Update: Here’s the working version.

function myapp() {
    var nodes = document.getElementsByTagName("*");
    for(var i = 0; i < nodes.length; i++) {
         var subNodes = nodes[i].childNodes;
         for (var j = 0; j < subNodes.length; j++) {
            var node = subNodes[j];
            if (node.nodeType === 3) {
                if (node.data) {
                    node.data = node.data.replace(/test text/g,"replacement test text");
                }
            }
        }
    }
};
myapp();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:16:24+00:00Added an answer on June 1, 2026 at 11:16 pm

    There may also be issues with the iteration since you’re modifying the DOM, and nodes is a “live” NodeList that will update with DOM changes.

    Not sure if this is an issue since you seem to be doing a swap at the same index, but I’m not sure. You could try converting the NodeList to an Array, or try iterating in reverse.


    But since you’re only dealing with text nodes, it would seem that you don’t need the .replaceChild at all. You could just update the .data of the text node directly.


    One more thing is that you’re checking the .type property, when it should probalby be the .nodeType property…

    if(nodes[i].nodeType == 3) {
    

    Or the nodeName…

    if(nodes[i].nodeName == '#text') {
    

    Oh, one more thing. You can’t get text nodes using getElementsByTagName('*'), because it only fetches elements.

    If you decide to do it that way, you’ll need to add code to deal with the child text nodes of the elements.

    function myapp() {
        var nodes = document.getElementsByTagName("*");
        for(var i = 0; i < nodes.length; i++) {
             var el = nodes[i];
             for (var j = 0; j < el.childNodes; j++) {
                 var node = el.childNodes[j];
                 if (node.nodeType === 3)
                     node.data = node.data.replace("test text","replacement test text");
             }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: This does work, I was being stupid :( i have the following extension
Update: Based on the answers I initially went the route of using IsInstanceOf() which
Update: This is, as I was told, no principle Python related problem, but seems
Update Putting this at the top because it is crazy :) So some users
UPDATE 2 : For posterity, this is how I've settled on doing it (thanks
I'm looking for some guidance on the overall architecture of this little system I'm
This particular assignment has to do with removing substrings from strings; I am trying
I want to read and save some data from this rss feed to in
I need some guidance/advices from someone who has any ideea about custom jquery event.
I am attempting to update the div of my dialog with new content from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.