Created
July 11, 2022 14:23
-
-
Save fagci/46a8f18e151d1b4851d8ffcff412c5c9 to your computer and use it in GitHub Desktop.
Function to wait for head. Used for userscripts (greasemonkey) with document-start.
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 waitForHead(cb) { | |
const waiter = setInterval(function() { | |
if(document.head) { | |
clearInterval(waiter); | |
cb(); | |
} | |
}, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation:
createInterval
executes function in another thread in same way as infinite loop. Then, afterhead
exists, you candocument.head.prepend()
something by passing callback intowaitForHead
function.