function waitForDocumentReadyState() {
  if (document.readyState === 'complete') {
    emitReady();
    return;
  }
  document.addEventListener('readystatechange', function onReadyStateChange() {
    if (document.readyState !== 'complete') {
      return;
    }
    document.removeEventListener('readystatechange', onReadyStateChange);
    emitReady();
  });
}
Waits for the document to be ready.