Вариант 1.
const box = document.querySelector('.box');
const debounce = (callback, delay) => {
let timeout = null;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(callback, delay, ...args);
};
};
const update = () => {
const balloon = document.querySelector('[class$="balloon__content"]');
if (balloon !== null) {
box.textContent = balloon.textContent;
}
};
const debouncedUpdate = debounce(update, 300);
window.addEventListener('click', () => {
debouncedUpdate();
});
Вариант 2.
const box = document.querySelector('.box');
const observer = new MutationObserver(mutations => {
for (let mutation of mutations) {
if (mutation.target.className.includes('balloon__content')) {
box.textContent = mutation.target.textContent;
}
}
});
observer.observe(document.querySelector('ymaps'), {
childList: true,
attributes: false,
subtree: true
});