YARCO.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // ==UserScript==
  2. // @name Yet Another Reddit Comment Overwriter
  3. // @namespace https://github.com/adriantache/YARCO/
  4. // @description Local script to overwrite all your comments with random ASCII characters and delete them. This works because Reddit doesn't store editing history, so technically this is the only way to obfuscate the contents of the comments. Based on Reddit Overwrite script v.1.4.8.
  5. // @include https://*.reddit.com/user/*/comments/
  6. // @include http://*.reddit.com/user/*/comments/
  7. // @version 0.3
  8. // @run-at document-start
  9. // ==/UserScript==
  10. //EXTRA OPTIONS (disabled by default)
  11. let set_default_settings = false //set the default options I use [[overrides all the below]]
  12. let show_overwrite_button = false //show separate button to overwrite
  13. let show_delete_button = false //show separate button to delete
  14. let generate_individual_delete_buttons = false //generate per comment delete and overwrite links
  15. let only_delete_old_comments = false //ignore comments newer than the limit below
  16. let old_comments_limit = 2 //if above is active, number of days after which a comment is considered old
  17. let only_delete_by_subreddit = false //ignore comments from subreddits other than the one chosen in the dropdown
  18. let time_between_actions = 2000 //reddit API limit is 60 actions per minute so don't exceed that
  19. let only_delete_downvoted = false //only delete comments under a certain karma
  20. let downvote_limit = -1 //if above is active, only delete comments with karma <= to this
  21. let ignore_upvoted = false //ignore comments over a certain karma (useless if only_delete_downvoted is active)
  22. let upvote_limit = 50 //if above is active, ignore comments with karma >= to this
  23. let auto_delete = false //automatically delete comments when navigating to comments page [[USE WITH FILTERS!]]
  24. let reload_on_completion = false //reload page on completion
  25. let highlight_comments = false //highlight comments selected for deletion
  26. //DEBUG
  27. let safeMode = false //process comments without performing any actions, used for debugging
  28. // TODO add STOP button
  29. // TODO add optional confirmation dialog OR start up delay
  30. // TODO add logic to avoid posts and enable using script on other user pages (Overview and Submitted)
  31. // TODO check compatibility with new reddit
  32. // TODO implement dictionary instead of random characters to defeat overwrite detection
  33. // TODO add buttons to exclude individual comments
  34. // TODO add color coding to comments selected for deletion (or to exclude buttons)
  35. // reddit username
  36. unsafeWindow.user = '';
  37. // array of comments (more precisely author tags)
  38. unsafeWindow.comments = [];
  39. // top section contents
  40. unsafeWindow.div = null;
  41. //status text
  42. unsafeWindow.status_message = null;
  43. // subreddit selected for deletion
  44. unsafeWindow.subreddit = "ALL";
  45. unsafeWindow.subreddit_array = [];
  46. // on page loaded, initialize the script
  47. window.addEventListener("DOMContentLoaded", init_script, false);
  48. function init_script(ev) {
  49. //if activated, set default settings for the extra options above
  50. if (set_default_settings) setDefaults();
  51. if (safeMode) setSafeModeDefaults();
  52. // get logged in username
  53. unsafeWindow.user = document.querySelector("span.user > a:not(.login-required)").innerHTML;
  54. // if not logged in exit
  55. if (!unsafeWindow.user) return;
  56. // retrieve all VISIBLE comments
  57. get_comments();
  58. // automatically start deletion process instead of generating buttons, if active
  59. if (auto_delete) {
  60. unsafeWindow.start_processing_comments(true, true);
  61. }
  62. else {
  63. // generate the top buttons
  64. generate_top_buttons();
  65. }
  66. }
  67. function get_comments() {
  68. // find all author tags to eventually get comments
  69. let comments = document.querySelectorAll("a.author");
  70. // filter out other authors
  71. unsafeWindow.comments = [].filter.call(comments, filter_author);
  72. // remove duplicates to fix double processing of comments to own posts
  73. unsafeWindow.comments = filter_duplicates(unsafeWindow.comments);
  74. // if active, filter out comments from the past 24 hours
  75. if (only_delete_old_comments) {
  76. unsafeWindow.comments = [].filter.call(unsafeWindow.comments, filter_time);
  77. }
  78. // if active, filter out comments from other subreddits than the chosen one
  79. if (only_delete_by_subreddit && unsafeWindow.subreddit !== "ALL") {
  80. unsafeWindow.comments = [].filter.call(unsafeWindow.comments, filter_subreddit);
  81. }
  82. // if active, filter out non downvoted comments
  83. if (only_delete_downvoted) {
  84. unsafeWindow.comments = [].filter.call(unsafeWindow.comments, filter_downvotes);
  85. }
  86. // if active, filter out upvoted comments
  87. if (ignore_upvoted) {
  88. unsafeWindow.comments = [].filter.call(unsafeWindow.comments, filter_upvotes);
  89. }
  90. update_status_text();
  91. if(highlight_comments) update_highlighting();
  92. }
  93. // append buttons to page
  94. function generate_top_buttons() {
  95. if (unsafeWindow.comments.length) {
  96. unsafeWindow.div = document.createElement("div");
  97. unsafeWindow.div.setAttribute('class', 'nextprev secure_delete_all');
  98. unsafeWindow.div.innerHTML = "";
  99. unsafeWindow.div.style.marginBottom = "10px";
  100. unsafeWindow.div.style.display = "flex";
  101. unsafeWindow.div.style.justifyContent = "flex-start";
  102. unsafeWindow.div.style.alignItems = "center";
  103. // make Subreddit Filter
  104. if (only_delete_by_subreddit) {
  105. //Create array of subreddits from comments
  106. unsafeWindow.subreddit_array = get_subreddit_array();
  107. let selectList = document.createElement("select");
  108. selectList.id = "subredditSelect";
  109. selectList.setAttribute('onChange', 'javascript: subreddit_select(this.value)')
  110. let selectedTitle = document.createElement("option");
  111. selectedTitle.selected = true;
  112. selectedTitle.disabled = true;
  113. selectedTitle.label = "Subreddit";
  114. selectList.append(selectedTitle);
  115. //Create and append the options
  116. for (let i = 0; i < unsafeWindow.subreddit_array.length; i++) {
  117. let option = document.createElement("option");
  118. option.value = unsafeWindow.subreddit_array[i];
  119. option.text = unsafeWindow.subreddit_array[i];
  120. selectList.appendChild(option);
  121. }
  122. unsafeWindow.div.appendChild(selectList);
  123. }
  124. // make Status message
  125. let status_div = document.createElement("div");
  126. status_div.style.marginLeft = "10px";
  127. unsafeWindow.status_message = document.createElement("p");
  128. unsafeWindow.status_message.setAttribute('class', 'status_message');
  129. unsafeWindow.status_message.innerHTML = "ERROR";
  130. status_div.appendChild(unsafeWindow.status_message);
  131. unsafeWindow.div.appendChild(status_div);
  132. // make Overwrite and Delete All link
  133. let odlink = document.createElement("a");
  134. odlink.setAttribute('class', 'bylink');
  135. odlink.setAttribute('onClick', 'javascript: start_processing_comments(true, true)');
  136. odlink.setAttribute('href', 'javascript:void(0)');
  137. odlink.style.marginLeft = "10px";
  138. odlink.appendChild(document.createTextNode('OVERWRITE AND DELETE'));
  139. unsafeWindow.div.appendChild(odlink);
  140. let br = document.createElement("br");
  141. unsafeWindow.div.appendChild(br);
  142. if (show_overwrite_button) {
  143. // make Overwrite All link
  144. let olink = document.createElement("a");
  145. olink.setAttribute('class', 'bylink');
  146. olink.setAttribute('onClick', 'javascript: start_processing_comments(true, false)');
  147. olink.setAttribute('href', 'javascript:void(0)');
  148. olink.style.marginLeft = "10px";
  149. olink.appendChild(document.createTextNode('OVERWRITE'));
  150. unsafeWindow.div.appendChild(olink);
  151. let br2 = document.createElement("br");
  152. unsafeWindow.div.appendChild(br2);
  153. }
  154. if (show_delete_button) {
  155. // make Delete All link
  156. let dlink = document.createElement("a");
  157. dlink.setAttribute('class', 'bylink');
  158. dlink.setAttribute('onClick', 'javascript: start_processing_comments(false, true)');
  159. dlink.setAttribute('href', 'javascript:void(0)');
  160. dlink.style.marginLeft = "10px";
  161. dlink.appendChild(document.createTextNode('DELETE'));
  162. unsafeWindow.div.appendChild(dlink);
  163. }
  164. //add our div to the webpage
  165. document.querySelector("div.content")
  166. .insertBefore(unsafeWindow.div, document.querySelector("div.content").firstChild);
  167. //update status text now that we have defined unsafeWindow.status_message
  168. update_status_text();
  169. } else {
  170. let div = document.createElement("div");
  171. div.style.marginLeft = "15px";
  172. div.innerHTML = "YARCO: No comments found. Please check your active filters and try again.";
  173. document.querySelector("div.content").insertBefore(div, document.querySelector("div.content").firstChild);
  174. }
  175. //add individual comment buttons
  176. if (generate_individual_delete_buttons) unsafeWindow.generate_delete_buttons();
  177. }
  178. unsafeWindow.start_processing_comments = function (overwrite_all, delete_all) {
  179. //get comments again in case the user has scrolled and revealed more comments
  180. get_comments();
  181. let commentsArray = [];
  182. for (let i = 0; i < unsafeWindow.comments.length; i++) {
  183. //for each author, get ID of the input field of the comment
  184. let thing_id = unsafeWindow.comments[i].parentNode.parentNode.querySelector("form.usertext > input[name='thing_id']").value;
  185. //TODO remove this
  186. if (!thing_id) {
  187. console.log("ERROR! Thing ID undefined for", unsafeWindow.comments[i]);
  188. continue;
  189. }
  190. if (commentsArray.indexOf(thing_id) == -1) {
  191. commentsArray.push(thing_id);
  192. }
  193. }
  194. if (overwrite_all && delete_all) {
  195. unsafeWindow.overwrite_all(commentsArray, true);
  196. } else if (overwrite_all) {
  197. unsafeWindow.overwrite_all(commentsArray, false);
  198. } else if (delete_all) {
  199. unsafeWindow.delete_all(commentsArray);
  200. }
  201. //set status message while working
  202. if (unsafeWindow.status_message) {
  203. unsafeWindow.status_message.innerHTML = `Processing... ${unsafeWindow.comments.length} comments left.`;
  204. }
  205. }
  206. unsafeWindow.overwrite_all = function (comments, also_delete) {
  207. //get next comment id
  208. let thing_id = comments.shift();
  209. //overwrite the next comment in the stack
  210. unsafeWindow.overwrite_comment(thing_id);
  211. //if also deleting, add a timeout and delete the comment
  212. if (also_delete) unsafeWindow.setTimeout(unsafeWindow.delete_comment(thing_id), time_between_actions);
  213. //if there are still comments left, get next comment
  214. //increase timeout if also deleting
  215. if (comments.length) {
  216. unsafeWindow.setTimeout(unsafeWindow.overwrite_all, also_delete ? time_between_actions * 2 : time_between_actions, comments, also_delete);
  217. } else if (reload_on_completion) {
  218. if (unsafeWindow.status_message) unsafeWindow.status_message.innerHTML = "Reloading page...";
  219. unsafeWindow.setTimeout(reload_page, time_between_actions * 5);
  220. } else get_comments();
  221. }
  222. unsafeWindow.delete_all = function (comments) {
  223. unsafeWindow.delete_comment(comments.shift());
  224. //if there are still comments left, get next comment
  225. if (comments.length) unsafeWindow.setTimeout(unsafeWindow.delete_all, time_between_actions, comments);
  226. else if (reload_on_completion) {
  227. if (unsafeWindow.status_message) unsafeWindow.status_message.innerHTML = "Reloading page...";
  228. unsafeWindow.setTimeout(reload_page, time_between_actions * 5);
  229. }
  230. else get_comments();
  231. }
  232. unsafeWindow.overwrite_comment = function (thing_id) {
  233. if (safeMode) {
  234. console.log(`Safe mode active! Accessing overwrite function for ${thing_id}.`);
  235. //add your debug code here
  236. return;
  237. }
  238. try {
  239. //find edit form (hidden on page but active)
  240. let edit_form = document.querySelector(`input[name="thing_id"][value="${thing_id}"]`).parentNode;
  241. //if comment is currently being edited, cancel out of that
  242. let edit_cancel_btn = edit_form.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > button.cancel");
  243. edit_cancel_btn.click();
  244. //find edit button and click it
  245. let edit_btn = edit_form.parentNode.querySelector("ul > li > a.edit-usertext");
  246. if (edit_btn) edit_btn.click();
  247. //find edit textbox and replace the string with random chars
  248. let edit_textbox = edit_form.querySelector("div.usertext-edit > div > textarea");
  249. let repl_str = '';
  250. let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz><.-,+!#$%^&*();:[]~";
  251. for (let i = 0; i < edit_textbox.value.length; i++) {
  252. if (edit_textbox.value.substr(i, 1) == '\n') {
  253. repl_str += '\n';
  254. } else {
  255. let random_char = Math.floor(Math.random() * chars.length);
  256. repl_str += chars.charAt(random_char, 1);
  257. }
  258. }
  259. //set edited value to the random string
  260. edit_textbox.value = repl_str;
  261. //find save comment button and click it
  262. let edit_save_btn = edit_form.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > button.save");
  263. edit_save_btn.click();
  264. } catch (e) {
  265. alert("Error interacting with overwrite form: " + e);
  266. console.log(e.stack);
  267. }
  268. }
  269. unsafeWindow.delete_comment = function (thing_id) {
  270. if (safeMode) {
  271. console.log(`Safe mode active! Accessing delete function for ${thing_id}.`);
  272. //add your debug code here
  273. return;
  274. }
  275. try {
  276. // get current status of comment editing box to prevent deleting comment before overwrite is complete
  277. let thing = document.querySelector("input[name='thing_id'][value='" + thing_id + "']");
  278. let status = thing.parentNode.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > span.status").innerHTML;
  279. //TODO remove this, just testing out where the weird bug is coming from
  280. if (status === null) throw Error("Status is null");
  281. if (status.indexOf("error") != -1) {
  282. alert("Failed to overwrite comment " + thing_id + " due to an unknown reddit error, skipping.");
  283. return;
  284. }
  285. // if status is submitting, there may be an internet connectivity error, so we retry
  286. if (status.indexOf("submitting") != -1) {
  287. unsafeWindow.setTimeout(unsafeWindow.delete_comment, time_between_actions * 2.5, thing_id);
  288. return;
  289. }
  290. // find delete button and click it and then yes confirmation button
  291. let del_form = thing.parentNode.parentNode.querySelector("ul.buttons > li > form.del-button");
  292. //TODO remove this, just testing out where the weird bug is coming from
  293. if (del_form === null) throw Error("Del_form is null");
  294. unsafeWindow.toggle(del_form.querySelector("span.main > a"));
  295. del_form.querySelector("span.error > a.yes").click();
  296. } catch (e) {
  297. alert("Error deleting comment: " + e);
  298. console.log(e.stack);
  299. }
  300. }
  301. //[UTILITY FUNCTIONS]
  302. function filter_author(comment) {
  303. return comment.innerHTML == unsafeWindow.user;
  304. }
  305. function filter_time(comment) {
  306. let time = comment.parentNode.parentNode.querySelector("time").innerHTML;
  307. //always exclude comments from the past day
  308. if (time.indexOf("days") === -1) return false;
  309. let num_days = time.split(" ");
  310. return parseInt(num_days[0]) >= old_comments_limit;
  311. }
  312. function filter_subreddit(comment) {
  313. return comment.parentNode.parentNode.parentNode.querySelector("a.subreddit").innerHTML == unsafeWindow.subreddit;
  314. }
  315. function filter_downvotes(comment) {
  316. let score = comment.parentNode.parentNode.querySelector("span.score.likes")
  317. //if we do not have a score (may be hidden) we exclude the comment for this filter
  318. if (score == null || score.title == null) return false
  319. return score.title <= downvote_limit;
  320. }
  321. function filter_upvotes(comment) {
  322. let score = comment.parentNode.parentNode.querySelector("span.score.likes")
  323. //if we do not have a score (may be hidden) we include the comment for this filter
  324. if (score == null || score.title == null) return true
  325. return score.title <= upvote_limit;
  326. }
  327. function filter_duplicates(comments) {
  328. let array = [];
  329. // For self-posts, the same author tag will show up twice, once for the post author and
  330. //then for the comment author. this gets the thing_id for that tag and if there are two
  331. //consecutive tags it only keeps the second one. Otherwise, the script would process some
  332. //comments twice, leading to some filters not working properly.
  333. for (let i = 0; i < comments.length - 1; i++) {
  334. let this_comment = comments[i].parentNode.parentNode.querySelector("form.usertext > input[name='thing_id']").value;
  335. let next_comment = comments[i + 1].parentNode.parentNode.querySelector("form.usertext > input[name='thing_id']").value;
  336. if (this_comment != next_comment) array.push(comments[i]);
  337. }
  338. //since the loop excludes the final item, add it here (will be a comment author)
  339. array.push(comments[comments.length - 1]);
  340. return array;
  341. }
  342. function reload_page() {
  343. //scroll to top first to prevent scrolling to the last deleted comment position after reload
  344. unsafeWindow.scrollTo(0, 0);
  345. unsafeWindow.location.reload();
  346. }
  347. function get_subreddit_array() {
  348. let array = [];
  349. for (let i = 0; i < unsafeWindow.comments.length; i++) {
  350. let sub = unsafeWindow.comments[i].parentNode.parentNode.parentNode.querySelector("a.subreddit").innerHTML;
  351. if (array.indexOf(sub) === -1) array.push(sub);
  352. }
  353. // Sort the array case insensitive and add option to disable subreddit filtering
  354. array = array.sort(sort_ignore_caps);
  355. array.unshift("ALL");
  356. return array;
  357. }
  358. function sort_ignore_caps(a, b) {
  359. return a.toLowerCase().localeCompare(b.toLowerCase());
  360. }
  361. function update_status_text() {
  362. if (unsafeWindow.status_message === null) return;
  363. if (noCommentsFound()) {
  364. unsafeWindow.status_message = "Problem getting comments!";
  365. console.log("No comments found!", unsafeWindow.comments)
  366. }
  367. let message = "FOUND " + unsafeWindow.comments.length + " COMMENT";
  368. if (unsafeWindow.comments.length > 1) message += "S";
  369. if ((only_delete_by_subreddit && unsafeWindow.subreddit !== "ALL") ||
  370. only_delete_downvoted ||
  371. ignore_upvoted ||
  372. only_delete_old_comments) {
  373. message += "\n(filters active)";
  374. }
  375. unsafeWindow.status_message.innerHTML = message;
  376. }
  377. function update_highlighting(){
  378. unsafeWindow.comments.forEach((value) => {
  379. let bottomBar = value.parentNode.parentNode.parentNode.querySelector(".child");
  380. bottomBar.style.height = "5px";
  381. bottomBar.style.background = "red";
  382. })
  383. }
  384. function noCommentsFound() {
  385. return unsafeWindow.comments == null ||
  386. unsafeWindow.comments.length == 0 ||
  387. unsafeWindow.comments.some(value => value == null);
  388. }
  389. unsafeWindow.overwrite_delete = function (thing_id) {
  390. unsafeWindow.overwrite_comment(thing_id);
  391. unsafeWindow.setTimeout(unsafeWindow.delete_comment, time_between_actions, thing_id);
  392. }
  393. //function to regenerate secure delete buttons after only overwriting a comment
  394. unsafeWindow.overwrite_reload = function (thing_id) {
  395. unsafeWindow.overwrite_comment(thing_id);
  396. unsafeWindow.setTimeout(unsafeWindow.generate_delete_buttons, 500);
  397. }
  398. unsafeWindow.subreddit_select = function (option) {
  399. unsafeWindow.subreddit = option;
  400. get_comments();
  401. }
  402. //[EXTRA FEATURES]
  403. //Add a "SECURE DELETE" button near each comment delete button
  404. unsafeWindow.generate_delete_buttons = function () {
  405. // first get comments again to bypass any active filters (see get_comments() for explanation)
  406. let comments = document.querySelectorAll("a.author");
  407. comments = [].filter.call(comments, filter_author);
  408. comments = filter_duplicates(comments);
  409. for (let i = 0; i < comments.length; i++) {
  410. try {
  411. // get the parent
  412. let main_parent = comments[i].parentNode.parentNode;
  413. let thing_id = main_parent.querySelector("form > input[name='thing_id']").value;
  414. let list = main_parent.querySelector("ul.flat-list");
  415. // add SECURE DELETE link to comments
  416. let secure_delete_link = document.createElement("li");
  417. secure_delete_link.setAttribute('class', 'secure_delete');
  418. let dlink = document.createElement("a");
  419. dlink.setAttribute('class', 'bylink secure_delete');
  420. dlink.setAttribute('onClick', 'javascript: overwrite_delete("' + thing_id + '")');
  421. dlink.setAttribute('href', 'javascript:void(0)');
  422. dlink.appendChild(document.createTextNode('SECURE DELETE'));
  423. secure_delete_link.appendChild(dlink);
  424. list.appendChild(secure_delete_link);
  425. // add OVERWRITE link to comments
  426. let overwrite_link = document.createElement("li");
  427. overwrite_link.setAttribute('class', 'overwrite');
  428. let olink = document.createElement("a");
  429. olink.setAttribute('class', 'bylink secure_delete');
  430. olink.setAttribute('onClick', 'javascript: overwrite_reload("' + thing_id + '")');
  431. olink.setAttribute('href', 'javascript:void(0)');
  432. olink.appendChild(document.createTextNode('OVERWRITE'));
  433. overwrite_link.appendChild(olink);
  434. list.appendChild(overwrite_link);
  435. } catch (e) {
  436. alert("Error adding Secure Delete links to comments.\nError: " + e);
  437. console.log(e.stack);
  438. }
  439. }
  440. }
  441. //sets the defaults I like (tired of copy pasting for every update)
  442. function setDefaults() {
  443. generate_individual_delete_buttons = true;
  444. only_delete_old_comments = true;
  445. ignore_upvoted = true;
  446. upvote_limit = 10;
  447. reload_on_completion = true;
  448. highlight_comments = true;
  449. }
  450. //if we are in safe mode we're not interacting with reddit so we eliminate the delays
  451. function setSafeModeDefaults() {
  452. time_between_actions = 0;
  453. reload_on_completion = false;
  454. }