Skip to content

Instantly share code, notes, and snippets.

@fagci
Created July 11, 2022 14:23
Show Gist options
  • Save fagci/46a8f18e151d1b4851d8ffcff412c5c9 to your computer and use it in GitHub Desktop.
Save fagci/46a8f18e151d1b4851d8ffcff412c5c9 to your computer and use it in GitHub Desktop.
Function to wait for head. Used for userscripts (greasemonkey) with document-start.
function waitForHead(cb) {
const waiter = setInterval(function() {
if(document.head) {
clearInterval(waiter);
cb();
}
}, 0);
}
@fagci
Copy link
Author

fagci commented Jul 11, 2022

Explanation: createInterval executes function in another thread in same way as infinite loop. Then, after head exists, you can document.head.prepend() something by passing callback into waitForHead function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment