Greasy Fork

RPS Comment Auto-Expander

Auto-expands truncated comments on Rock Paper Shotgun, and optionally auto expand replies as well

目前为 2024-11-11 提交的版本。查看 最新版本

// ==UserScript==
// @name        RPS Comment Auto-Expander
// @namespace   Violentmonkey Scripts
// @match       https://www.rockpapershotgun.com/*
// @grant       none
// @version     1.1
// @author      AshEnke
// @license     MIT
// @description Auto-expands truncated comments on Rock Paper Shotgun, and optionally auto expand replies as well
// ==/UserScript==

(function() {
    'use strict';

    const CONFIG = {
        DEBUG: true,           // Enable debug logging
        EXPAND_REPLIES: true,  // Enable auto-expanding of reply threads
        BATCH_SIZE: 10,        // Number of replies to expand in each batch
        BATCH_DELAY: 1000     // Delay between batches in milliseconds
    };

    const log = CONFIG.DEBUG ? (...args) => console.log('[RPS Comment Expander]', ...args) : () => {};
    
    // Queue to store reply buttons
    let replyQueue = [];
    let isProcessingQueue = false;

    async function processReplyQueue() {
        if (isProcessingQueue || replyQueue.length === 0) return;
        
        isProcessingQueue = true;
        
        while (replyQueue.length > 0) {
            const batch = replyQueue.splice(0, CONFIG.BATCH_SIZE);
            log(`Processing batch of ${batch.length} replies`);
            
            for (const button of batch) {
                button.click();
            }
            
            if (replyQueue.length > 0) {
                await new Promise(resolve => setTimeout(resolve, CONFIG.BATCH_DELAY));
            }
        }
        
        isProcessingQueue = false;
    }

    function expandContent(shadowRoot) {
        log('Expanding content');
        
        // Expand main comments
        const seeMoreSpans = Array.from(shadowRoot.querySelectorAll('span'))
            .filter(span => span.textContent.trim() === 'See more');

        seeMoreSpans.forEach(span => {
            log('Expanding comment');
            span.click();
        });

        // Expand replies if enabled
        if (CONFIG.EXPAND_REPLIES) {
            const replyButtons = Array.from(shadowRoot.querySelectorAll('span'))
                .filter(span =>
                    span.className.startsWith('Button__contentWrapper') &&
                    (span.textContent.includes('reply') || span.textContent.includes('replies'))
                );

            // Add new replies to the front of the queue (LIFO)
            replyQueue.unshift(...replyButtons);
            processReplyQueue();

            return seeMoreSpans.length > 0 || replyButtons.length > 0;
        }

        return seeMoreSpans.length > 0;
    }

    function initialize() {
        const owComponent = document.querySelector('#comments > div > *');
        if (!owComponent || !owComponent.shadowRoot) {
            setTimeout(initialize, 1000);
            return;
        }

        log('Found OpenWeb component');

        // Initial expansion
        expandContent(owComponent.shadowRoot);

        // Watch for new comments and replies
        new MutationObserver(() => expandContent(owComponent.shadowRoot))
            .observe(owComponent.shadowRoot, {
                childList: true,
                subtree: true
            });
    }

    // Start looking for comments when the page is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initialize);
    } else {
        initialize();
    }
})();