Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.1 kB
6
Indexable
Never
(async function() {
    // Function to fetch Reddit post data
    async function fetchRedditComments(postUrl) {
        const response = await fetch(`${postUrl}.json`);
        const json = await response.json();
        return json;
    }

    // Function to display top-level comments
    function displayTopLevelComments(comments) {
        comments[1].data.children.forEach(comment => {
            if (comment.kind === "t1") { // "t1" indicates a comment
                console.log(comment.data.author + ": " + comment.data.body);
                console.log("------");
            }
        });
    }

    // Get the Reddit post URL from the user
    const postUrl = prompt("Enter the Reddit post URL:");

    if (postUrl) {
        try {
            const comments = await fetchRedditComments(postUrl);
            displayTopLevelComments(comments);
        } catch (error) {
            console.error("Failed to fetch comments:", error);
        }
    } else {
        console.log("No URL provided.");
    }
})();
Leave a Comment