Created
August 10, 2021 11:56
-
-
Save c7x43t/05fd361a4433fbd3817d22640dde5fcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadElementsSecure(query,root,timeout){ | |
if(timeout===undefined){ | |
timeout=10000; | |
} | |
if(root===undefined){ | |
root=document; | |
} | |
var t0=Date.now(); | |
return new Promise(function(resolve,reject){ | |
function _loadElements(){ | |
var t1=Date.now(); | |
if((t1-t0)<timeout){ | |
var elements=document.querySelectorAll(query); | |
if(elements.length>0){ | |
resolve(Array.from(elements)); | |
}else{ | |
window.requestAnimationFrame(_loadElements); | |
} | |
}else{ | |
reject('timeout'); | |
} | |
} | |
window.requestAnimationFrame(_loadElements); | |
}); | |
} | |
function loadElementSecure(...args){ | |
return loadElementsSecure(...args).then(e=>e[0]) | |
} | |
function loadScript(script,...args){ | |
if (document.readyState === 'complete' || document.readyState === 'interactive') { | |
script(...args); | |
}else{ | |
document.addEventListener('DOMContentLoaded',script.bind(this,...args)); | |
} | |
} | |
function getParentWithSelector(element,parentSelector){ | |
while((element=element.parentElement)!==null){ | |
if(element.matches(parentSelector)){ | |
return element; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment