I added a simple function that you can use "MutationObserver" action easily.
(function (win) {
'use strict';
let listeners = [],
doc = win.document,
MutationObserver = win.MutationObserver || win.WebKitMutationObserver,
observer;
function DOMElementReady(selector, fn) {
listeners.push({
selector: selector,
fn: fn
});
if (!observer) {
observer = new MutationObserver(checkState);
observer.observe(doc.documentElement, {
childList: true,
subtree: true
});
}
checkState();
}
function checkState() {
for (let i = 0, len = listeners.length, listener, elements; i < len; i++) {
listener = listeners[i];
elements = doc.querySelectorAll(listener.selector);
for (let j = 0, jLen = elements.length, element; j < jLen; j++) {
element = elements[j];
if (!element.ready) {
element.ready = true;
listener.fn.call(element, element);
}
}
}
}
win.DOMElementReady = DOMElementReady;
})(this);
DOMElementReady('.selector-name', ()=> {
console.log('DOM Element is ready!');
})
🖖 Happy hacking.