whenReady = (callback) => {
  if (!types.WINDOW.document) {
    return;
  }

  if (types.WINDOW.document.prerendering) {
    addEventListener('prerenderingchange', () => whenReady(callback), true);
  } else if (types.WINDOW.document.readyState !== 'complete') {
    addEventListener('load', () => whenReady(callback), true);
  } else {
    // Queue a task so the callback runs after `loadEventEnd`.
    setTimeout(callback, 0);
  }
}
Runs in the next task after the page is done loading and/or prerendering.
@param callback