Nesse link aqui https://stackoverflow.com/questions/5525071/how-to-wait-until-an-element-exists
o rapaz diz que com o MutationObserver você não precisa de timer:
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
e usaria assim:
waitForElm('.some-class').then((elm) => {
console.log('Element is ready');
console.log(elm.textContent);
});
//ou usando await
const elm = await waitForElm('.some-class');