How to get all active Mutation Observers on page?

Viewed 298

I checked API but couldn't find a way how to get list of active mutation observers on a page using javascript. Is it possible?

1 Answers

What if you extend the MutationObserver like this:

class TrackedMutationObserver extends MutationObserver {

    static instances = []

    constructor(...args) {
        super(...args);
    }

    observe(...args) {
        super.observe(...args);
        this.constructor.instances.push(this)
    }

    disconnect() {
        super.disconnect();
        this.constructor.instances = this.constructor.instances.splice(this.constructor.instances.findIndex(instance => instance === this), 1)
    }

    static getActive() {
        return this.instances
    }
}

and instead of using MutationObserver you instantiate TrackedMutationObserver

TS Playground example

Related