最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using mutationObserver doesn't seem to keep track of a changing table in the DOM - Stack Overflow

programmeradmin0浏览0评论

I am trying to write a simple chrome extension that changes the values of a table in the DOM.The important thing here is that the webpage is using ajax mands to build and change the contents of this table.(First, there is nothing in the page.Then you click on a link, and with an ajax request(no page reloads) a table will be created and after that by clicking other links, this table will be modified).

Here's the code I am using to listen to the table's changes :

var target = document.querySelector('#my-table');

var observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
        alert('The table has appeared');
    });
});

var config = {
    attributes: true,
    childList: true,
    characterData: true,
    subtree: true
};

observer.observe(target, config);

observer.disconnect();

The problem is that nothing will happens when I press a link in the page that makes the table change.And when I was inspecting chrome's console to debug my work, I saw that it gave me this error as soon as the page loads :

Uncaught NotFoundError: Failed to execute 'observe' on 'MutationObserver': The provided node was null

I am really lost and don't know what to do.Does anyone have any suggestions? ps:Here's my extension's manifest file:

"manifest_version": 2,

  "name": "MyExtension",
  "description": "Testing the Content Script api",
  "version": "1.0",

  "content_scripts": [
    {
      "matches": ["www.myWebsite"],
      "js": ["script.js"]
    }
  ],

  "browser_action": {
    "default_icon": "icon.png"
  }
}

I am trying to write a simple chrome extension that changes the values of a table in the DOM.The important thing here is that the webpage is using ajax mands to build and change the contents of this table.(First, there is nothing in the page.Then you click on a link, and with an ajax request(no page reloads) a table will be created and after that by clicking other links, this table will be modified).

Here's the code I am using to listen to the table's changes :

var target = document.querySelector('#my-table');

var observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
        alert('The table has appeared');
    });
});

var config = {
    attributes: true,
    childList: true,
    characterData: true,
    subtree: true
};

observer.observe(target, config);

observer.disconnect();

The problem is that nothing will happens when I press a link in the page that makes the table change.And when I was inspecting chrome's console to debug my work, I saw that it gave me this error as soon as the page loads :

Uncaught NotFoundError: Failed to execute 'observe' on 'MutationObserver': The provided node was null

I am really lost and don't know what to do.Does anyone have any suggestions? ps:Here's my extension's manifest file:

"manifest_version": 2,

  "name": "MyExtension",
  "description": "Testing the Content Script api",
  "version": "1.0",

  "content_scripts": [
    {
      "matches": ["www.myWebsite."],
      "js": ["script.js"]
    }
  ],

  "browser_action": {
    "default_icon": "icon.png"
  }
}
Share Improve this question edited Nov 25, 2014 at 22:00 Scimonster 33.4k10 gold badges79 silver badges91 bronze badges asked Nov 25, 2014 at 21:30 roostaamirroostaamir 1,9686 gold badges26 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You need to re-evaluate the selection after the mutation, inspecting each added element in turn.

  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      mutation.addedNodes.forEach(function(node) {
        if (node.id == 'my-table') { // or node.getElementsByClassName(..).length or whatever
          console.log('table has appeared.');
        });
      });
    });
  });
  observer.observe(document, {
    childList: true,
    subtree: true,
    attributes: false,
    characterData: false,
  });

Less efficiently, but shorter code, you can re-select the entire document after each mutation:

  var observer = new MutationObserver(function() {
    if (document.getElementById('my-table')) {
      console.log('table has appeared.');
    }
  });
  observer.observe(document, {
    childList: true,
    subtree: true,
    attributes: false,
    characterData: false,
  });

If you are using a simple ID selector, it might be efficient since selecting by id is highly optimized.

发布评论

评论列表(0)

  1. 暂无评论