0

I want to detect the DOM changes using MutationObserver on web page https://www.baidu.com/ in Tampermonkey script in Chrome.

When I refer to MutationObserver in the Tampermonkey script, it is always null. It seems the original js script in the web page sets MutationObserver to null.

Is there a way to recover MutationObserver so I can use the feature normally?

Dillion Wang
  • 362
  • 3
  • 18
  • 2
    Sounds like a problem with a particular site that overrides window.MutationObserver or you have an extension that does the same in [page context](/a/9517879). – wOxxOm May 08 '20 at 09:17
  • 2
    If it's `null`, it appears that something is directly setting it to that value. – VLAZ May 08 '20 at 09:17
  • 2
    `MutationObserver`has been supported in Chrome for years. If you're getting `null` for `MutationObserver`, something has either overwritten it or shadowed it. If you're getting `null` for `window.MutationObserver`, something has overwritten it or shadowed `window`. In either case, it's specific to the page/environment where you're doing this, it's not a general thing using Chrome. – T.J. Crowder May 08 '20 at 09:19
  • @wOxxOm @VLAZ You are right! When I run a Tempermonkey script on a website, the `MutationObserver` is always `null`. It must be something wrong with the Tempermonkey extension. – Dillion Wang May 08 '20 at 09:21
  • @T.J.Crowder Yes, `MutationObserver` works normally on other websites. Seems like the specific web page itself (https://www.baidu.com) sets the `MutationObserver` to `null`. Is there a way to recover the `MutationObserver`? – Dillion Wang May 08 '20 at 09:49
  • 2
    Restore it via iframe: [example](https://stackoverflow.com/a/7089553). – wOxxOm May 08 '20 at 09:58
  • @wOxxOm Thanks! fixed using this method. – Dillion Wang May 08 '20 at 10:17

0 Answers0