I am building a JS library and one of the use case requires me trigger an event on DOM change especially if its a single page application E.g.: github search bar
After some research I came across MutationObserver
:
// Dom change event listner
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations, observer) {
// fired when a mutation occurs
attachListners();
// ...
});
observer.observe(documentAlias, {
subtree: true,
childList: true
//...
});
Problem : The library is I'm building designed to be plugged into any website so I have no control on the html implementation. I'm bit concerned using mutation observer might enter infinite loop. Have seen many stack overflow question on same line.
Is there any alternate/better approach? How can I detect DOM update/change safely and efficiently? Or is mutation observer best bet