Skip to content

Instantly share code, notes, and snippets.

@ericandrewlewis
Last active January 27, 2019 00:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericandrewlewis/1f0424858b389c7ad7531ebba4fa3b30 to your computer and use it in GitHub Desktop.
Save ericandrewlewis/1f0424858b389c7ad7531ebba4fa3b30 to your computer and use it in GitHub Desktop.
Scroll to the rock bottom of a website

Scroll to the rock bottom of a website

Throw the script below in the browser's JS console.

It moves the viewport to the bottom of the page repeatedly with a delay in between. This is useful when the page loads data via infinite scroll and you want to do something with all the data.

const atPageBottom = () => {
  const scrolled = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
  const documentHeightMinusOneViewport =
  document.body.scrollHeight - Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  return Math.abs( documentHeightMinusOneViewport - scrolled ) < 3;
}

const scrollUntilAtPageBottom = () => {
  if (atPageBottom()) {
    return Promise.resolve(true);
  }
  window.scrollTo(0, document.body.scrollHeight);
  return (new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, 1000)
  }))
    .then(scrollUntilAtPageBottom);
}

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