// ==UserScript==
// @name Reddit Auto Expand Comments on Scroll
// @namespace http://tampermonkey.net/
// @version 1.7
// @description Automatically expands Reddit comments and replies as you scroll down the page. Comments only expand when they come into view.
// @author MarkingTheWay
// @match *://*.reddit.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to simulate clicking an element
function simulateClick(element) {
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
element.dispatchEvent(event);
}
// Function to observe and expand comments when they come into view
function observeCommentsAndReplies() {
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const element = entry.target;
// Click the "more comments" or "more replies" buttons when they enter the viewport
simulateClick(element);
// Unobserve the element after clicking it to avoid repeated clicks
observer.unobserve(element);
}
});
});
// Select all expandable elements: "more comments" and "more replies" buttons
const moreRepliesSpans = document.querySelectorAll('span.text-secondary-weak.font-normal');
const moreCommentsButtons = document.querySelectorAll('button[aria-label="more comments"]');
const loadMoreButtons = document.querySelectorAll('button[role="button"]:not([aria-label])');
// Observe each element that should be clicked as it comes into view
moreRepliesSpans.forEach(span => {
if (span.textContent.includes("more reply") || span.textContent.includes("more replies")) {
observer.observe(span);
}
});
moreCommentsButtons.forEach(button => {
observer.observe(button);
});
loadMoreButtons.forEach(button => {
if (button.textContent.includes("load more") || button.textContent.includes("more replies")) {
observer.observe(button);
}
});
}
// Re-observe new comments that may load dynamically
function observeNewlyLoadedComments() {
const observer = new MutationObserver(() => {
observeCommentsAndReplies(); // Observe newly added comments or replies
});
// Observe the body for any changes (like new comments being loaded dynamically)
observer.observe(document.body, { childList: true, subtree: true });
}
// Start observing comments once the page is loaded
window.addEventListener('load', function() {
observeCommentsAndReplies(); // Observe initial comments and replies
observeNewlyLoadedComments(); // Re-observe dynamically loaded content
});
})();