I have this highly specific problem that I can not seem to solve, i need to listen to the DOM change events and I have used Mutation Observer API and I can register addition of div
to the DOM but I need to be able to react to clicks on that div
and persist data that I get back across navigation for that I resorted to localStorage
but I can not seem to trigger observe
method when user clicks the link that has been dynamically added.
For better clarification here is what I want to achieve.
- Notification is sent and
MutationObserver
registeter it gets triggered viaobserve
- User clicks on newly added
div
it expands navigation - Get the notification message, parse it push it to array then store it in
localStorage
- On next click user is taken to new page where I also need to pickup some data and append it to
localStorage
So far I can react to new DOM additions. And I can filter additions of what I need.
Here is the code
function watchForOrder() {
var mutationObserver = new MutationObserver(function(mutation) {
mutation.forEach(function(mutation) {
console.log(mutation);
//Possible place to create object to store information that I need.
});
});
mutationObserver.observe(document.body, {
// attributes: true,
// characterData: true,
childList: true,
subtree: true
});
I also can not disconnect observer
because I need it to listen for new additions all the time so my concern is that it may impact performance severely.
tl;dr How can I combine MutationRecord
tap into that and set up regular event listener or is that even good idea ?