Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

mutation observers - How to inject mutationobserver to puppeteer

I want trace changed DOM like mutationobserver in headless chrome. So I learning puppeteer library, but don’t know how to use do that.

It’s possible to trace DOM change in puppeteer?? thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well,you can inject custom code to the browser.

One way:

await page.evaluate(() => {
   const observer = new MutationObserver(
   function() {
     // communicate with node through console.log method
     console.log('__mutation')
    }
   )
   const config = {
     attributes: true,
     childList: true,
     characterData: true,
     subtree: true
   }
   observer.observe(target, config)
})

In your node script:

page.on('console', async (msg) => {
   if (msg.text === '__mutation') {
     // do something...
   }
}) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...