【问题标题】:How to inject mutationobserver to puppeteer如何将mutationobserver注入puppeteer
【发布时间】:2018-01-08 15:21:11
【问题描述】:

我想在 headless chrome 中跟踪更改的 DOM,例如 mutationobserver。 所以我学习了 puppeteer 库,但不知道如何使用。

可以在 puppeteer 中追踪 DOM 变化吗?谢谢

【问题讨论】:

标签: mutation-observers puppeteer


【解决方案1】:

好吧,您可以将自定义代码注入浏览器。

一种方式:

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

在您的节点脚本中:

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

【讨论】:

猜你喜欢
  • 2018-04-09
  • 2020-10-03
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2019-09-25
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多