qsa-observer
Social Media Photo by Kevin Maillefer on Unsplash
Given an array of selectors, handles any matching element that was either connected, or disconnected, from an observed element, or document.
Differently from MutationObserver
- each element is granted to be handled, whenever it's observed via one, or more, selectors
- all observed elements will pass through
handle(element, connected, selector)
utility, per each matching selector - elements injected through
innerHTML
, or created offline, will be handled too once live
Example
import QSAO from 'qsa-observer';
const query = ['div'];
const root = document;
const {
drop,
flush,
observer,
parse
} = QSAO({
query,
root,
handle(element, connected, selector) {
const event = connected ? 'connected' : 'disconnected';
console.log(element, event, selector);
}
});
const observeMore = selector => {
if (!query.includes(selector)) {
query.push(selector);
parse(root.querySelectorAll(selector));
}
};
setTimeout(observeMore, 1000, 'p');