YARCO.js 19 KB

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