How to inject mutationobserver to puppeteer

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 02:53:05

问题


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


回答1:


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...
   }
}) 


来源:https://stackoverflow.com/questions/47903954/how-to-inject-mutationobserver-to-puppeteer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!