Skip to content

Instantly share code, notes, and snippets.

@franzese
Created June 8, 2016 22:10
Show Gist options
  • Save franzese/504f5cf157994446417dd5ca1bac10d1 to your computer and use it in GitHub Desktop.
Save franzese/504f5cf157994446417dd5ca1bac10d1 to your computer and use it in GitHub Desktop.
Continuously scrolls to the bottom of a web page. Great for infinite scroll pages such as Facebook and Pinterest.
// call this when you are done digging
var stopDigging;
(function () {
var dig = true, tryScroll, observer;
// create an observer instance
observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
tryScroll();
});
});
// if dig is enabled and the page is not currently scrolled to the bottom
tryScroll = function() {
if ((document.body.scrollTop < document.body.scrollHeight) && dig) {
window.scrollTo(0,document.body.scrollHeight);
}
}
// pass in the target node, as well as the observer options
observer.observe(document.body, {attributes: true, childList: true, characterData: true, subtree: true });
// toggle dig flag to stop scrolling
stopDigging = function() {
dig = false;
observer.disconnect();
console.warn('Digging stopped');
}
tryScroll();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment