How to track DOM change in chrome extension?

前端 未结 2 1599
别跟我提以往
别跟我提以往 2020-12-31 01:06

I am writing a chrome extension where I want to fetch all the images exist on a page but some of the images load after some time (may be through ajax) which I could not fetc

2条回答
  •  伪装坚强ぢ
    2020-12-31 02:07

    Updated for 2020:

    The recommended way nowadays is to use the Mutation Observer API.

     let observer = new MutationObserver(mutations => {
        for(let mutation of mutations) {
             for(let addedNode of mutation.addedNodes) {
                 if (addedNode.nodeName === "IMG") {
                     console.log("Inserted image", addedNode);
                  }
              }
         }
     });
     observer.observe(document, { childList: true, subtree: true });
    

提交回复
热议问题