
MutationObserver can be very useful. For example:MutationObserver , you will, at any time, be aware of what changes have occurred on the page, which means that you can easily undo them.
MutationObserver features may be useful. In fact, they are much more.MutationObserver in web applications is quite simple. You need to create an instance of MutationObserver , passing in the appropriate constructor a function that will be called every time changes occur in the DOM. The first argument of the function is a collection of all mutations that occurred in the form of a single package. For each mutation, information is provided about its type and about the changes it represents. var mutationObserver = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { console.log(mutation); }); }); observe method starts the DOM change tracking process. It takes two arguments — a DOM node to monitor, and an object with parameters.disconnect method stops monitoring changes.takeRecords method returns the current queue of the MutationObserver instance, after which it clears it. // HTML- mutationObserver.observe(document.documentElement, { attributes: true, characterData: true, childList: true, subtree: true, attributeOldValue: true, characterDataOldValue: true }); div element: <div id="sample-div" class="test"> Simple div </div> class attribute from this element: $("#sample-div").removeAttr("class"); mutationObserver.observe(...) , and the fact that the function that reacts to the arrival of a new package of changes, displays the data in the console, we will see in the console the contents of the corresponding MutationRecord object:
class attribute. // mutationObserver.disconnect(); MutationObserver API has broad browser support:
MutationObserver was not always available to developers. What did they use before the appearance of MutationObserver ? Here are a few options:MutationEvents .setInterval method, you can schedule periodic execution of a function that checks the DOM for changes. Naturally, the use of this method significantly reduces the performance of web applications.MutationEvents is deprecated and soon modern browsers will no longer support it.
MutationObserver in the form of CSS animations may seem somewhat strange. And here is the animation? In general, the idea here is to create an animation that will be called after the element is added to the DOM. At the time the animation starts, the animationstart event will be triggered. If you assign a handler for this event, you can find out the exact time of adding a new element to the DOM. At the same time, the execution time of the animation should be so small that it is almost invisible to the user. <div id="container-element"></div> @keyframes nodeInserted { from { opacity: 0.99; } to { opacity: 1; } } #container-element * { animation-duration: 0.001s; animation-name: nodeInserted; } container-element . When the animation ends, the corresponding event is raised.event.animationName in order to make sure that this is the animation that interests us. var insertionListener = function(event) { // , , if (event.animationName === "nodeInserted") { console.log("Node has been inserted: " + event.target); } } document.addEventListener("animationstart", insertionListener, false); // standard + firefox document.addEventListener("MSAnimationStart", insertionListener, false); // IE document.addEventListener("webkitAnimationStart", insertionListener, false); // Chrome + Safari 
MutationObserver API and alternative ways to monitor DOM changes. It should be noted that MutationObserver has many advantages over these alternatives. In general, we can say that this API is able to report any changes that may occur in the DOM, that it is well optimized, giving information about changes collected in packages. In addition, the MutationObserver API is supported by all major modern browsers, and there are polyfills for it based on MutationEvents .MutationObserver is central to the SessionStack library, which aims to organize the collection of data about what happens to web pages.
Source: https://habr.com/ru/post/351256/
All Articles