Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  123] [ 3]  / answers: 1 / hits: 8040  / 3 Years ago, sat, july 10, 2021, 12:00:00

I'm developing a Chrome extension that clicks elements on a website, but some of the elements appear after the check and the code doesn't work. I need to make the function wait until the selected element is loaded.


And NOT wait in delays in seconds like on setTimeout function.


How can I do it?


More From » html

 Answers
4

Can be done with MutationObserver:


function handleSomeDiv(someDiv) { 
console.log("div was handled");
}

const observer = new MutationObserver(function (mutations, mutationInstance) {
const someDiv = document.getElementById('some-div');
if (someDiv) {
handleSomeDiv(someDiv);
mutationInstance.disconnect();
}
});


observer.observe(document, {
childList: true,
subtree: true
});

[#1116] Sunday, July 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;