Reddit Comment Context Copier

Copy full context of Reddit comments to clipboard

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

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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 Comment Context Copier
// @namespace    http://greasyfork.org/
// @version      0.1
// @description  Copy full context of Reddit comments to clipboard
// @author       Chicken & ChatGPT
// @match        https://www.reddit.com/*
// @grant        GM_setClipboard
// @license MIT
// ==/UserScript==
function addButtonToComment(comment) {
    const listItem = document.createElement('li');
    const anchor = document.createElement('a');
    anchor.textContent = 'copy-context';
    anchor.style.cursor = 'pointer';
    anchor.addEventListener('click', function(event) {
        event.preventDefault();
        copyCommentContext(comment);
    });

    listItem.appendChild(anchor);
    comment.querySelector('.buttons').appendChild(listItem);
}

window.addEventListener('load', function() {
    // Function to handle added nodes
    function handleAddedNodes(nodes) {
        nodes.forEach(node => {
            if (node.classList && node.classList.contains('comment')) {
                addButtonToComment(node);
            } else if (node.querySelectorAll) {
                node.querySelectorAll('.comment').forEach(addButtonToComment);
            }
        });
    }

    // Set up the observer
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            handleAddedNodes(Array.from(mutation.addedNodes));
        });
    });

    // Start observing
    const config = { childList: true, subtree: true };
    observer.observe(document.body, config);

    // Add buttons to existing comments
    document.querySelectorAll('.comment').forEach(addButtonToComment);
});


function copyCommentContext(comment) {
    let context = '';

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            return node.nodeValue;
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            if (node.tagName.toLowerCase() === 'p') {
                return node.textContent + '\n';
            } else if (node.tagName.toLowerCase() === 'blockquote') {
                // Process each child node and prepend '>' to each line
                return Array.from(node.childNodes)
                            .map(child => child.tagName && child.tagName.toLowerCase() === 'p'
                                ? '> ' + child.textContent
                                : processNode(child))
                            .join('\n') + '\n';
            }
            return Array.from(node.childNodes).map(processNode).join('');
        }
        return '';
    }

    let currentElement = comment;
    while (currentElement && currentElement.classList.contains('comment')) {
        const author = "**"+currentElement.querySelector('.author').textContent+"**";
        const commentBody = currentElement.querySelector('.usertext .md');
        const commentText = Array.from(commentBody.childNodes).map(processNode).join('');
        context = author + ': ' + commentText + '\n' + context;
        currentElement = currentElement.parentElement.closest('.comment');
    }
    GM_setClipboard(context.trim());
}