🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

Remove Reddit Comments

Removes Reddit Comments

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Remove Reddit Comments
// @namespace    http://tampermonkey.net/
// @version      2024-08-01
// @description  Removes Reddit Comments
// @author       You
// @match        https://www.reddit.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// ==/UserScript==

// Go to your user profile page before running this script
// https://www.reddit.com/user/spez/

let postAge = 30; // Delete posts older than X days
let authorName = "spez"; // Your account username
let postData = "Lorem ipsum dolor sit amet"; // Text to replace posts with before deleting it

// Used to add waits in milliseconds to account for website loading time
const delay = ms => new Promise(res => setTimeout(res, ms));

async function deletePosts() {

    let url = window.location.href;
    let userUrl = /(http|https):\/.*reddit.com\/user\/.*($|\/$)/; // https://www.reddit.com/user/spez/
    let postUrl = /(http|https):\/.*reddit.com\/r\/.*\/comments\/.*\/comment\/.*($|\/$)/; // // https://www.reddit.com/r/reddit/comments/145bram/comment/jnk45rr/

    if (url.match(userUrl)) {
        await delay(3000);

        // Make sure there are posts on the user profile page
        if (document.querySelector('shreddit-profile-comment') !== null ) {

            // Store all visible posts as an array to loop through later
            let userPosts = document.querySelectorAll('shreddit-profile-comment');

            let i;
            for (i = 0; i < userPosts.length; i++) {
                let todayDate = new Date();
                let postDate = Date.parse( userPosts[i].querySelector('time').getAttribute('datetime') );
                // Calculating the number of days between the two dates
                let dateDiff = Math.ceil((todayDate - postDate) / 86400000);

                if (dateDiff <= postAge) {
                    // Hide posts that are too new to make room for older posts to be loaded
                    userPosts[i].style.display = "none";
                } else {
                    // Post is old enough to be deleted
                    // Click edit post button to be taken to the post page
                    userPosts[i].querySelector('shreddit-comment-action-row > shreddit-overflow-menu').shadowRoot.querySelector('faceplate-dropdown-menu > faceplate-menu > faceplate-tracker[noun="edit"] > li > a').click();
                    await delay(3000);
                    // Make sure there are posts from your username on the post page
                    if (document.querySelector('shreddit-comment[author="' + authorName + '"]') !== null) {
                        // Store all visible posts from your username as an array to loop through later
                        let userPosts = document.querySelectorAll('shreddit-comment[author="' + authorName + '"]');

                        let i;
                        for (i = 0; i < userPosts.length; i++) {
                            let todayDate = new Date();
                            let postDate = Date.parse( userPosts[i].querySelector('time').getAttribute('datetime') );
                            // Calculating the number of days between the two dates
                            let dateDiff = Math.ceil((todayDate - postDate) / 86400000);

                            if (dateDiff > postAge) {
                                // Post is old enough to be deleted
                                try {
                                    // Click edit post button to get the edit comment text area
                                    userPosts[i].querySelector('shreddit-comment-action-row > shreddit-overflow-menu').shadowRoot.querySelector('faceplate-dropdown-menu > faceplate-menu > faceplate-tracker[noun="edit"] > li > div').click();
                                    await delay(1000);
                                    // Edit the post comment
                                    document.querySelector('shreddit-comment shreddit-composer').shadowRoot.querySelector('shreddit-markdown-composer').shadowRoot.querySelector('textarea').value = postData;
                                    await delay(1000);
                                    // Click save edits button
                                    document.querySelector('shreddit-comment shreddit-composer > button[type="submit"]').click();
                                    await delay(1000);
                                    // Click delete button
                                    userPosts[i].querySelector('shreddit-comment-action-row > shreddit-overflow-menu').shadowRoot.querySelector('faceplate-dropdown-menu > faceplate-menu > faceplate-tracker[noun="delete"] > li > div').click();
                                    await delay(1000);
                                    // Click delete button in confirmation modal
                                    document.querySelector("#comment-deletion-modal").shadowRoot.querySelector("#deleteBtn").click();
                                    await delay(3000);
                                } catch(err) {
                                    // Error encountered, usually with selecting a shadowRoot element
                                    // Go back to the profile page and reload to restart the script
                                    document.querySelector('#user-drawer-content faceplate-tracker[noun="profile"] > li > a').click();
                                    location.reload();
                                }
                            }

                        }


                    }
                }


            }

            // Went though all the posts that were visible on the user profile page
            // Go back to the profile page and reload to find more posts to delete
            document.querySelector('#user-drawer-content faceplate-tracker[noun="profile"] > li > a').click();
            location.reload();
        }

    }

}

deletePosts();