Reddit - Auto Load More Comments

Automatically load more comments at the bottom of a Reddit comment page

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Reddit - Auto Load More Comments
// @description Automatically load more comments at the bottom of a Reddit comment page
// @namespace   valacar.reddit.auto-load-more-comments
// @include     /^https?://www\.reddit\.com/r/[^/]+/comments//
// @version     0.1.1
// @grant       none
// @compatible  firefox Firefox with GreaseMonkey
// @compatible  chrome Chrome with TamperMonkey
// @author      valacar
// ==/UserScript==

// Note: most of this code is adapted from StackOverflow...
// http://stackoverflow.com/a/7557433
// http://stackoverflow.com/a/19343058

function TriggerMouseEvent(node, eventType) {
	var clickEvent = document.createEvent('MouseEvents');
	clickEvent.initEvent (eventType, true, true);
	//console.log('clicking');
	node.dispatchEvent (clickEvent);
}

function MoreCommentsLinkHandler() {
	var el = document.querySelector('.nestedlisting > div:nth-last-of-type(2) .morecomments a');

	if (el) {
		var rect = el.getBoundingClientRect();

		if (rect.top >= 0 && rect.left >= 0 &&
			rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
			rect.right <= (window.innerWidth || document.documentElement.clientWidth)
		   )
		{
			//console.log('visible');

			if (MoreCommentsLinkHandler.lastTime == undefined)
			{
				MoreCommentsLinkHandler.lastTime = new Date();
				MoreCommentsLinkHandler.firstTime = true;
			}
			else
			{
				var now = new Date();
				MoreCommentsLinkHandler.firstTime = false;
				var elapsedTime = now - MoreCommentsLinkHandler.lastTime;
				//console.log('elapsed: ' + elapsedTime);
				MoreCommentsLinkHandler.lastTime = now;
			}

			/* only click if one second has passed since the last time the link was visible */
			if (MoreCommentsLinkHandler.firstTime || elapsedTime > 1000) {
				TriggerMouseEvent(el, "click");
			}
		}
	}

}

document.addEventListener('DOMContentLoaded', MoreCommentsLinkHandler, false);
document.addEventListener('load', MoreCommentsLinkHandler, false);
document.addEventListener('scroll', MoreCommentsLinkHandler, false);
document.addEventListener('resize', MoreCommentsLinkHandler, false);