Hack infinite scroll javascript with infinite auto-scroll to bottom of page

Alec Jacobson

January 07, 2010

weblog/

This is a hack to have your browser load all search results when a page is using jQuery's infinite scroll feature, like this site: http://instantwatcher.com/titles/all?infinite=1. Here's the client side javascript to keep auto-scrolling this page to the bottom, thus triggering infinite scroll to load more results. It runs until there are no more results to load:
    function scrollToBottom(){
      bottom = document.body.scrollHeight;
      current = window.innerHeight+ document.body.scrollTop;
      if((bottom-current) >0){
        window.scrollTo(0, bottom);
        setTimeout ( 'scrollToBottom()', 1000 );
      }
    };
    scrollToBottom();
I run this on Safari using this short applescript:
tell application "Safari"
  set doc to front document
  set this_url to URL of doc
  do JavaScript "
    function scrollToBottom(){
      bottom = document.body.scrollHeight;
      current = window.innerHeight+ document.body.scrollTop;
      if((bottom-current) >0){
        window.scrollTo(0, bottom);
        setTimeout ( 'scrollToBottom()', 1000 );
      }
    };
    scrollToBottom();
    " in doc
end tell
Try it!