YARCO.js 20 KB

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