Is there a way to connect a MutationObserver and disconnect it using the same button? I know how to do each separately, but I want to use just one button. How can I achieve this? Apologies for my poor English.
var target = document.getElementsByClassName('message')[0];
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.info("EVENT TRIGGERED");
});
});
var config = {
attributes: true,
childList: true,
characterData: true
};
function disconnect() {
observer.disconnect();
}
function observe() {
observer.observe(target, config);
}
// Simulate change
refreshIntervalId = setInterval(function() {
document.getElementsByClassName('message')[0].innerHTML = Math.random().toString(36).substring(2, 15);
}, 1000);