I’ve developed this Mutation Observer to monitor changes in a Shopify cart drawer. The logic is quite simple:
function callback(mutationList) {
console.log("Observer callback fired");
for (const mutation of mutationList) {
if (mutation.type === "childList") {
var cartDrawerBody = document.querySelector("div.cart-drawer__body");
if (cartDrawerBody) {
var children = cartDrawerBody.querySelectorAll(".cart-item__container").length;
if (children > 0) {
observer.disconnect(); // Disconnect the observer to prevent loop
adjustPadding();
var cartDrawerFooter = document.querySelector(".cart-drawer__footer");
if (!cartDrawerFooter) {
console.log("No footer found");
return;
}
if (!$(cartDrawerFooter).find(".post-footer-content").length) {
var postFooterContent = $(`
<div class="post-footer-content">
<!-- Simplified content -->
<div class="post-footer-row">Content</div>
</div>
`);
$(cartDrawerFooter).append(postFooterContent);
console.log("Content appended to footer");
} else {
console.log("Content already exists in footer, not appending");
}
observer.observe(document.querySelector("#cd-cart"), {
attributes: true,
childList: true,
subtree: true,
characterData: true,
});
} else {
console.log("no children to append");
}
} else {
console.log("no body");
}
}
}
}
var observer = new MutationObserver(callback);
observer.observe(cartDrawer, {
attributes: true,
childList: true,
subtree: true,
characterData: true,
});
However, no mutation is being triggered on iOS devices, everywhere else it works perfectly, including Safari Desktop and Android devices.
Has anyone encountered a similar issue or has any idea why this might be happening? Any help would be greatly appreciated!
Thank you!
Than you!