I have an observer that does a check for the existence of an element in the DOM. And also, inside observer I have a fetch request which doesn't work.
Please help with the code, or explain why it doesn't work. If I make this fetch query outside of observer, it works fine.
let observer = new MutationObserver(() => {
if (document.querySelector("any_selector")) {
let options = {
method: "GET",
headers: { Accept: "application/json", "Content-Type": "application/json" },
};
fetch('https://any_url', options)
.then((response) => response.json())
.then(get_any_data);
function get_any_data(data) {
console.log("Work!!!");
}
observer.disconnect();
}
});
observer.observe(document.querySelector("body"), { childList: true });