Class MutationObserver
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The MutationObserver interface provides the ability to watch for changes being made to the DOM tree. It is designed as a replacement for the older Mutation Events feature, which was part of the DOM3 Events specification.
[Value("MutationObserver")]
public class MutationObserver
- Inheritance
-
MutationObserver
- Inherited Members
Remarks
-'PerformanceObserver'
-'ResizeObserver'
-'IntersectionObserver'
-A brief overview
-A more in-depth discussion
-A screencast by Chromium developer Rafael Weinstein
Constructors
MutationObserver()
public MutationObserver()
MutationObserver(MutationCallback)
The DOM MutationObserver()
constructor — part of the MutationObserver interface — creates and
returns a new observer which invokes a specified callback when DOM events
occur.
public MutationObserver(MutationCallback callback)
Parameters
callbackMutationCallback
Remarks
DOM observation does not begin immediately; the
Observe(Node, MutationObserverInit) method must be called first to
establish which portion of the DOM to watch and what kinds of changes to watch for.
Methods
Disconnect()
The MutationObserver methoddisconnect() tells the observer to stop watching for
mutations.
[Value("disconnect")]
public GlobalObject.Undefined Disconnect()
Returns
- GlobalObject.Undefined
undefined.NOTE
All notifications of mutations that have already been
detected, but not yet reported to the observer, are discarded.
To hold on to and handle the detected but unreported mutations, use
the TakeRecords() method.
Remarks
The observer can be reused by calling its
Observe(Node, MutationObserverInit) method again.
Observe(Node, MutationObserverInit)
The MutationObserver method observe() configures the MutationObserver
callback to begin receiving notifications of changes to the DOM that match the given options.
[Value("observe")]
public GlobalObject.Undefined Observe(Node target, MutationObserverInit options = null)
Parameters
targetNodeoptionsMutationObserverInit
Returns
Remarks
Depending on the configuration, the observer may watch a single Node in the DOM tree, or that node and some or all of its descendant nodes. The same node can be observed by multiple observers, and the same MutationObserver can watch for changes to different parts of the DOM tree and/or different types of changes by calling observe() multiple times on the sameMutationObserver.
To stop the MutationObserver (so that none of its callbacks will be triggered any longer), call Disconnect().
TakeRecords()
The MutationObserver methodtakeRecords() returns a list of all matching DOM changes
that have been detected but not yet processed by the observer's callback function,
leaving the mutation queue empty.
[Value("takeRecords")]
public List<MutationRecord> TakeRecords()
Returns
- List<MutationRecord>
An array of MutationRecord objects, each describing one change applied to
the observed portion of the document's DOM tree.NOTE
The queue of mutations which have occurred, but not been
delivered to the observer's callback is left empty after callingtakeRecords().
Remarks
The most common use case for this is to
immediately fetch all pending mutation records immediately prior to disconnecting the
observer, so that any pending mutations can be processed when shutting down the
observer.