Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit old comment deleter
- // @namespace http://tampermonkey.net/
- // @version 1.1
- // @description deletes reddit comments older than a day, activate by visiting https://old.reddit.com/u/me#trim
- // @author You
- // @match https://old.reddit.com/*
- // @run-at document-idle
- // ==/UserScript==
- (function(){
- 'use strict'; if(!location.href.includes('#trim')) return;
- /* variables */
- var username = "original";
- var otherpart = "original";
- var recheck_minutes = 10;
- /* refresh function */
- function reloadIn(seconds) {
- setTimeout(function(){
- location.href="https://old.reddit.com/user/me/#trim";
- window.reload();
- }, seconds*1000);
- };
- /* logic variables */
- var deleted = false;
- var comments = document.querySelectorAll("div[class*=comment]");
- if(r.config['modhash'] === "" & username !== "optional" & otherpart !== "optional") {
- /* log in part */
- document.evaluate("//a[text()='Log in']", document).iterateNext().click();
- setTimeout(() => {
- document.querySelector('#user_login').value=username;
- document.querySelector('#passwd_login').value=otherpart;
- document.evaluate("//button[text()='log in']", document).iterateNext().click();
- },250);
- reloadIn(2);
- };
- /* sends request to delete comment */
- function remove(c) {
- var id = c.id.replace('thing_','');
- console.warn("deleting", id, c); deleted = true;
- var request = ("id=" + id + "&executed=deleted&r=" + r.config['post_site'] + "&uh=" + (r.config['modhash']) + "&renderstyle=html");
- var xhttp = new XMLHttpRequest();
- xhttp.open("POST", "https://old.reddit.com/api/del", true);
- xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xhttp.send(request);
- };
- /* going through each comment */
- function handler(c) {
- if(c.querySelector('time').innerText.includes('day')) {
- remove(c);
- };
- };
- comments.forEach(handler);
- if(deleted) {
- reloadIn(2);
- } else {
- setTimeout(function () {
- var nextButton = document.querySelector('a[rel="nofollow next"]');
- if(nextButton === null) return;
- location.href = nextButton.href + "#trim";
- window.reload();
- }, 2000);
- reloadIn(recheck_minutes*60);
- };
- })();
Add Comment
Please, Sign In to add comment