Skip to content

Instantly share code, notes, and snippets.

@kiki67100
Last active January 9, 2023 15:25
Show Gist options
  • Save kiki67100/65e32488f80c8450d45b07329ba452b2 to your computer and use it in GitHub Desktop.
Save kiki67100/65e32488f80c8450d45b07329ba452b2 to your computer and use it in GitHub Desktop.
/*kiki67100 github 09/01/2023*/
/*Waiting in javascript a html element perfect for scrapping and more easy to use than MutationObserver */
let check_available_el = function (querySelector, debug = false, fn_on_not_found = null, max_tries = 1000, test_interval = 500) {
return new Promise(function (resolve, reject) {
let iterator_count = 0;
let __interval = setInterval(function () {
iterator_count++;
if (iterator_count > max_tries) {
clearInterval(__interval);
reject("maximum tries reached");
}
var check_el = document.querySelector(querySelector);
if (check_el) {
if (debug) console.info('[check_available_el] ' + querySelector + ' is found');
clearInterval(__interval);
resolve();
} else {
if (debug) console.info('[check_available_el] ' + querySelector + ' NOT found');
if (fn_on_not_found !== null) {
if (debug) console.log('[check_available_el] fn_on_not_found() ')
if (typeof fn_on_not_found === "function") {
fn_on_not_found();
} else {
if (debug) console.warn('fn_on_not_found must be a function');
}
}
}
}, test_interval)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment