Hi there,
I’m building out a script which adds some messaging to a listing of products depending on some conditions (e.g. whether they have messaging applied already and if they’re not on an excluded list of URLs stored within an array).
The code I have put together runs on the first load but then fails to run again when the user changes the page number or uses the filtering options. I previously had multiple ways to check for this type of change which meant I could watch the page and act accordingly (i.e. re-fire the test). However, due to a change in the provider for the feed, these functions no longer work for whatever reason (any thoughts as to why?).
The main changes I would like to look for are the URL being amended (due to a user using the filtering options) or when the user navigates to another page of results which is loaded via Ajax I believe. Both of these changes modify the URL within the address bar and also add a result in the browser history so maybe if I can sniff that, I can reapply the messaging to the new set of products.
Option 1
watchPLP = cb => {
services.onMutate({
mutatedNode: document.querySelector("#productlist"),
settings: {
attributes: true,
childList: true
},
cb: function () {
setTimeout(function () {
if (dom.get(".label_wrapper") === null) {
cb();
}
}, 500);
}
});
console.log("watching");
};
}
Option 2
watchPLP = (cb) => {
window.addEventListener('popstate', function (event) {
setTimeout(() => {
if (document.querySelector('.plp-productbadge') === null) {
cb();
}
}, 500);
});
};
One further option which I have explored is MutationObserver, as follows which works as it returns the number of changes via a console log. Something which I could build in is if the number of changes is equal or greater than 1 (or something like that) and then fire the function.
However, the other issue is that I cannot seem to fire the messaging function again as I get a “is not defined” error when I try to call it from within the watchPLP function.
One other problem is that I cannot use “observer.disconnect();” as it stops the observe altogether, so the observe just continues to run which is surely causing a drain on performance?
Option 3
watchPLP = (cb) => {
// select the target node
var target = document.querySelector('#productlist');
// create an observer instance
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { childList: true, subtree: true }
// pass in the target node, as well as the observer options
observer.observe(target, config);
};
Thanks in advance - let me know if any further information would be helpful!