Skip to solution
mediumFrontend

How would you use MutationObserver to detect when a third-party script injects new nodes into the DOM?

440 views
01

Understand the problem

Question presented to candidate: "A third-party analytics script you don't control keeps injecting an ad banner div somewhere inside your page's content area, and you need to detect and remove it the moment it appears. How would you reliably catch that injection?"

What a strong answer should cover:

  • 📌 Interview term: MutationObserver — a real, built-in browser API that lets code .observe(targetNode, options) and receive a real callback whenever the DOM tree under that target genuinely changes — child nodes added/removed, attributes changed, or text content changed, depending on the configured options.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly via jsdom (which, unlike ResizeObserver/IntersectionObserver, genuinely DOES implement MutationObserver): observing a target with { childList: true, subtree: true } genuinely detected a real, dynamically-injected <script> node appended anywhere under the target, reporting it in a real mutations array with type: "childList" and the injected node present in addedNodes.
  • 📌 Interview term: the real, asynchronous, batched callback timing — a precise answer names that MutationObserver callbacks genuinely fire ASYNCHRONOUSLY, batched as a real MICROTASK — verified directly, a mutation made synchronously did not appear in the log until AFTER the current synchronous script finished and a microtask was allowed to flush — several rapid mutations made in the same tick genuinely arrive together in ONE callback invocation, not one callback per mutation.
  • 📌 Interview term: { childList, subtree, attributes } options — a precise answer names that MutationObserver genuinely observes NOTHING by default — at least one of childList/attributes/characterData must be explicitly set to true, and subtree: true is genuinely required to catch a mutation happening on a DESCENDANT of the target, not just the target itself.
  • A precise answer names the real, practical response pattern for the prompt's own scenario: inside the callback, check each mutation's addedNodes for a match (by tag name, class, or a known selector) and call .remove() on it immediately — plus the real caveat that .disconnect() should eventually be called if the watch is no longer needed, to avoid an unnecessary real, ongoing observation cost.

Clarifying questions expected:

  • None — this is a definitional/practical question; directly demonstrating the real, verified detection of an injected node is the strong signal.

Code / implementation expected: Yes — a real, jsdom-verified MutationObserver detecting a dynamically injected node and an attribute change, plus proof of .disconnect() stopping further detection.

observerstrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every claim below was actually run against a real jsdom document — jsdom, unlike ResizeOb

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, direct proof via jsdom: MutationObserver detects a dynamically injected node and an attribute change, fires asynchronously as a batched microtask, and disconnect() stops further detection
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 92 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track