emerginginstance

Reddit comment deleter

Jun 23rd, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Reddit old comment deleter
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.1
  5. // @description  deletes reddit comments older than a day, activate by visiting https://old.reddit.com/u/me#trim
  6. // @author       You
  7. // @match        https://old.reddit.com/*
  8. // @run-at       document-idle
  9. // ==/UserScript==
  10.  
  11. (function(){
  12.     'use strict'; if(!location.href.includes('#trim')) return;
  13.    
  14.     /* variables */
  15.     var username = "original";
  16.     var otherpart = "original";
  17.     var recheck_minutes = 10;
  18.    
  19.     /* refresh function */
  20.     function reloadIn(seconds) {
  21.         setTimeout(function(){
  22.                 location.href="https://old.reddit.com/user/me/#trim";
  23.                 window.reload();
  24.             }, seconds*1000);
  25.     };
  26.  
  27.     /* logic variables */
  28.     var deleted = false;
  29.     var comments = document.querySelectorAll("div[class*=comment]");
  30.  
  31.     if(r.config['modhash'] === "" & username !== "optional" & otherpart !== "optional") {
  32.         /* log in part */
  33.         document.evaluate("//a[text()='Log in']", document).iterateNext().click();
  34.         setTimeout(() => {
  35.             document.querySelector('#user_login').value=username;
  36.             document.querySelector('#passwd_login').value=otherpart;
  37.             document.evaluate("//button[text()='log in']", document).iterateNext().click();
  38.         },250);
  39.         reloadIn(2);
  40.     };
  41.  
  42.     /* sends request to delete comment */
  43.     function remove(c) {
  44.         var id = c.id.replace('thing_','');
  45.         console.warn("deleting", id, c); deleted = true;
  46.         var request = ("id=" + id + "&executed=deleted&r=" + r.config['post_site'] + "&uh=" + (r.config['modhash']) + "&renderstyle=html");
  47.         var xhttp = new XMLHttpRequest();
  48.             xhttp.open("POST", "https://old.reddit.com/api/del", true);
  49.             xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  50.             xhttp.send(request);
  51.     };
  52.  
  53.     /* going through each comment */
  54.     function handler(c) {
  55.         if(c.querySelector('time').innerText.includes('day')) {
  56.             remove(c);
  57.         };
  58.     };
  59.  
  60.     comments.forEach(handler);
  61.  
  62.     if(deleted) {
  63.         reloadIn(2);
  64.     } else {
  65.        
  66.         setTimeout(function () {
  67.             var nextButton = document.querySelector('a[rel="nofollow next"]');
  68.             if(nextButton === null) return;
  69.             location.href = nextButton.href + "#trim";
  70.             window.reload();
  71.         }, 2000);
  72.        
  73.         reloadIn(recheck_minutes*60);
  74.     };
  75.  
  76. })();
Add Comment
Please, Sign In to add comment