Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Created February 18, 2021 16:41
Show Gist options
  • Save thomaswilburn/0f24581cd803acbc0c0fb3407124dc01 to your computer and use it in GitHub Desktop.
Save thomaswilburn/0f24581cd803acbc0c0fb3407124dc01 to your computer and use it in GitHub Desktop.
/*
Register functions to be notified if a specific selector matches (or stops matching)
*/
var watchList = new Map();
var glance = function(watch) {
var result = document.querySelector(watch.selector);
watch.callbacks.forEach(function(c) {
if (c.previous == result) return;
c(result);
c.previous = result;
})
};
var observer = new MutationObserver(function(mutations) {
watchList.forEach(glance);
});
observer.observe(document.body, {
subtree: true,
childList: true,
attributeFilter: ["id"]
});
var watchSelector = function(selector, callback) {
var watch = watchList.get(selector) || { selector, callbacks: [] };
if (watch.callbacks.includes(callback)) return;
watch.callbacks.push(callback);
try {
glance(watch);
watchList.set(selector, watch);
} catch (err) {
console.error(err);
}
};
var unwatchSelector = function(selector, callback) {
var watching = watchList.get(selector);
if (!watching) return;
watching.callbacks = watching.callbacks.filter(c => c != callback);
if (!watching.callbacks.length) {
watchList.delete(selector);
}
};
module.exports = { watchSelector, unwatchSelector };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment