YARCO.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/*
  6. // @include http://*.reddit.com/user/*
  7. // @version 0.1
  8. // @run-at document-start
  9. // ==/UserScript==
  10. //EXTRA OPTIONS
  11. let generate_individual_delete_buttons = true //disabled by default
  12. let time_between_actions = 2000 //reddit API limit is 60 actions per minute so don't exceed that
  13. // TODO separate overwrite and delete
  14. // TODO only delete comments older than a day
  15. // TODO only delete comments from a certain subreddit
  16. // TODO check feedback for Reddit Overwrite for extra features
  17. // TODO consider caching comments array
  18. // TODO test Overview page
  19. // TODO add STOP button
  20. // TODO add optional confirmation dialog
  21. // TODO add status message while processing
  22. // reddit username
  23. unsafeWindow.user = '';
  24. // array of comments (more precisely author tags)
  25. unsafeWindow.comments = [];
  26. // number of detected user comments
  27. // TODO refactor this out
  28. unsafeWindow.num_user_comments = 0;
  29. // top section contents
  30. unsafeWindow.span = '';
  31. //TODO refactor these out
  32. unsafeWindow.to_delete = [];
  33. unsafeWindow.deleted = 0;
  34. // on page loaded, initialize the script
  35. window.addEventListener("DOMContentLoaded", init_script, false);
  36. function init_script(ev) {
  37. // get logged in username
  38. unsafeWindow.user = document.querySelector("span.user > a:not(.login-required)").innerHTML;
  39. // if not logged in exit
  40. if (!unsafeWindow.user) return;
  41. //retrieve all VISIBLE comments
  42. get_comments()
  43. // generate the top buttons
  44. generate_top_buttons();
  45. }
  46. function get_comments() {
  47. // find all author tags to eventually get comments
  48. let comments = document.querySelectorAll("a.author");
  49. // filter out other authors
  50. unsafeWindow.comments = [].filter.call(comments, filter_author)
  51. unsafeWindow.num_user_comments = unsafeWindow.comments.length;
  52. }
  53. // append buttons to page
  54. function generate_top_buttons() {
  55. if (unsafeWindow.num_user_comments) {
  56. unsafeWindow.span = document.createElement("div");
  57. unsafeWindow.span.setAttribute('class', 'nextprev secure_delete_all');
  58. unsafeWindow.span.innerHTML = "";
  59. unsafeWindow.span.style.marginBottom = "10px";
  60. // make Overwrite and Delete All link
  61. let odlink = document.createElement("a");
  62. odlink.setAttribute('class', 'bylink');
  63. odlink.setAttribute('onClick', 'javascript: recursive_process(true, true)');
  64. odlink.setAttribute('href', 'javascript:void(0)');
  65. odlink.style.marginLeft = "10px";
  66. odlink.appendChild(document.createTextNode('OVERWRITE AND DELETE ' +
  67. unsafeWindow.comments.length +
  68. ' COMMENTS'));
  69. unsafeWindow.span.appendChild(odlink);
  70. let br = document.createElement("br");
  71. unsafeWindow.span.appendChild(br);
  72. // make Overwrite All link
  73. let olink = document.createElement("a");
  74. olink.setAttribute('class', 'bylink');
  75. olink.setAttribute('onClick', 'javascript: recursive_process(true, false)');
  76. olink.setAttribute('href', 'javascript:void(0)');
  77. olink.style.marginLeft = "10px";
  78. olink.style.position = "relative";
  79. olink.style.top = "5px";
  80. olink.appendChild(document.createTextNode('OVERWRITE ' +
  81. unsafeWindow.comments.length +
  82. ' COMMENTS'));
  83. unsafeWindow.span.appendChild(olink);
  84. let br2 = document.createElement("br");
  85. unsafeWindow.span.appendChild(br2);
  86. // make Delete All link
  87. let dlink = document.createElement("a");
  88. dlink.setAttribute('class', 'bylink');
  89. dlink.setAttribute('onClick', 'javascript: recursive_process(false, true)');
  90. dlink.setAttribute('href', 'javascript:void(0)');
  91. dlink.style.marginLeft = "10px";
  92. dlink.style.position = "relative";
  93. dlink.style.top = "10px";
  94. dlink.appendChild(document.createTextNode('DELETE ' +
  95. unsafeWindow.comments.length +
  96. ' COMMENTS'));
  97. unsafeWindow.span.appendChild(dlink);
  98. // TODO test status message
  99. // let status_message = document.createTextNode("p");
  100. // status_message.innerHTML = "STATUS";
  101. // status_message.style.marginLeft = "10px";
  102. // status_message.style.position = "relative";
  103. // status_message.style.top = "10px";
  104. // unsafeWindow.span.appendChild(status_message);
  105. document.querySelector("div.content").insertBefore(unsafeWindow.span, document.querySelector("div.content").firstChild);
  106. //add per comment buttons (disabled by default)
  107. if (generate_individual_delete_buttons) unsafeWindow.generate_delete_buttons()
  108. } else if (unsafeWindow.span != null) {
  109. unsafeWindow.span.style.display = 'none';
  110. }
  111. }
  112. unsafeWindow.recursive_process = function (overwrite_all, delete_all) {
  113. //get comments again in case the user has scrolled and revealed more comments
  114. get_comments()
  115. let commentsArray = [];
  116. for (let i = 0; i < unsafeWindow.comments.length; i++) {
  117. //for each author, get ID of the input field of the comment
  118. let thing_id = unsafeWindow.comments[i].parentNode.parentNode.querySelector("form.usertext > input[name='thing_id']").value;
  119. if (commentsArray.indexOf(thing_id) == -1) {
  120. commentsArray.push(thing_id);
  121. //TODO refactor this out
  122. unsafeWindow.num_user_comments++;
  123. }
  124. }
  125. if (overwrite_all && delete_all) {
  126. unsafeWindow.overwrite_all(commentsArray, true);
  127. } else if (overwrite_all) {
  128. unsafeWindow.overwrite_all(commentsArray, false);
  129. } else if (delete_all) {
  130. unsafeWindow.delete_all(commentsArray);
  131. }
  132. }
  133. unsafeWindow.overwrite_all = function (comments, also_delete) {
  134. //get next comment id
  135. let thing_id = comments.shift();
  136. //overwrite the next comment in the stack
  137. unsafeWindow.overwrite_comment(thing_id);
  138. //if also deleting, add a timeout and delete the comment
  139. if (also_delete) unsafeWindow.setTimeout(unsafeWindow.delete_comment(thing_id), time_between_actions);
  140. //if there are still comments left, get next comment
  141. //increase timeout if also deleting
  142. if (comments.length) unsafeWindow.setTimeout(unsafeWindow.overwrite_all, also_delete ? time_between_actions * 2 : time_between_actions, comments, also_delete);
  143. }
  144. unsafeWindow.delete_all = function (comments) {
  145. unsafeWindow.delete_comment(comments.shift());
  146. //if there are still comments left, get next comment
  147. if (comments.length) unsafeWindow.setTimeout(unsafeWindow.delete_all, time_between_actions, comments);
  148. }
  149. unsafeWindow.overwrite_comment = function (thing_id) {
  150. try {
  151. //find edit form (hidden on page but active)
  152. let edit_form = document.querySelector("input[name='thing_id'][value='" + thing_id + "']").parentNode;
  153. //if comment is currently being edited, cancel out of that
  154. let edit_cancel_btn = edit_form.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > button.cancel");
  155. edit_cancel_btn.click();
  156. //find edit button and click it
  157. let edit_btn = edit_form.parentNode.querySelector("ul > li > a.edit-usertext");
  158. if (edit_btn) edit_btn.click();
  159. //find edit textbox and replace the string with random chars
  160. let edit_textbox = edit_form.querySelector("div.usertext-edit > div > textarea");
  161. let repl_str = '';
  162. let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz><.-,+!#$%^&*();:[]~";
  163. for (let i = 0; i < edit_textbox.value.length; i++) {
  164. if (edit_textbox.value.substr(i, 1) == '\n') {
  165. repl_str += '\n';
  166. } else {
  167. let random_char = Math.floor(Math.random() * chars.length);
  168. repl_str += chars.charAt(random_char, 1);
  169. }
  170. }
  171. //set edited value to the random string
  172. edit_textbox.value = repl_str;
  173. //find save comment button and click it
  174. let edit_save_btn = edit_form.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > button.save");
  175. edit_save_btn.click();
  176. } catch (e) {
  177. alert("Error interacting with overwrite form: " + e);
  178. }
  179. }
  180. unsafeWindow.delete_comment = function (thing_id) {
  181. try {
  182. // get current status of comment editing box to prevent deleting comment before overwrite is complete
  183. let thing = document.querySelector("input[name='thing_id'][value='" + thing_id + "']");
  184. let status = thing.parentNode.querySelector("div.usertext-edit > div.bottom-area > div.usertext-buttons > span.status").innerHTML;
  185. if (status.indexOf("error") != -1) {
  186. alert("Failed to overwrite comment " + thing_id + " due to an unknown reddit error, skipping.");
  187. return;
  188. }
  189. // if status is submitting, there may be an internet connectivity error, so we retry
  190. if (status.indexOf("submitting") != -1) {
  191. unsafeWindow.setTimeout(unsafeWindow.delete_comment, time_between_actions * 5, thing_id);
  192. return;
  193. }
  194. // find delete button and click it and then yes confirmation button
  195. let del_form = thing.parentNode.parentNode.querySelector("ul.buttons > li > form.del-button");
  196. unsafeWindow.toggle(del_form.querySelector("span.main > a"));
  197. del_form.querySelector("span.error > a.yes").click();
  198. } catch (e) {
  199. alert("Error deleting comment: " + e);
  200. }
  201. }
  202. //[UTILITY FUNCTIONS]
  203. function filter_author(comment) {
  204. return comment.innerHTML == unsafeWindow.user
  205. }
  206. unsafeWindow.overwrite_delete = function (thing_id) {
  207. unsafeWindow.overwrite_comment(thing_id)
  208. unsafeWindow.setTimeout(unsafeWindow.delete_comment, time_between_actions, thing_id)
  209. }
  210. //function to regenerate secure delete buttons after only overwriting a comment
  211. unsafeWindow.overwrite_reload = function (thing_id) {
  212. unsafeWindow.overwrite_comment(thing_id)
  213. unsafeWindow.setTimeout(unsafeWindow.generate_delete_buttons, 500)
  214. }
  215. //[EXTRA FEATURES]
  216. //Add a "SECURE DELETE" button near each comment delete button
  217. unsafeWindow.generate_delete_buttons = function () {
  218. get_comments()
  219. for (let i = 0; i < unsafeWindow.comments.length; i++) {
  220. try {
  221. // get the parent
  222. let main_parent = unsafeWindow.comments[i].parentNode.parentNode;
  223. let thing_id = main_parent.querySelector("form > input[name='thing_id']").value;
  224. let list = main_parent.querySelector("ul.flat-list");
  225. // if it already contains the tags, skip
  226. if (list.querySelector("li.secure_delete") && list.querySelector("li.overwrite")) continue;
  227. unsafeWindow.num_user_comments++;
  228. // add SECURE DELETE link to comments
  229. let secure_delete_link = document.createElement("li");
  230. secure_delete_link.setAttribute('class', 'secure_delete');
  231. let dlink = document.createElement("a");
  232. dlink.setAttribute('class', 'bylink secure_delete');
  233. dlink.setAttribute('onClick', 'javascript: overwrite_delete("' + thing_id + '")');
  234. dlink.setAttribute('href', 'javascript:void(0)');
  235. dlink.appendChild(document.createTextNode('SECURE DELETE'));
  236. secure_delete_link.appendChild(dlink);
  237. main_parent.querySelector("ul.flat-list").appendChild(secure_delete_link);
  238. // add OVERWRITE link to comments
  239. let overwrite_link = document.createElement("li");
  240. overwrite_link.setAttribute('class', 'overwrite');
  241. let olink = document.createElement("a");
  242. olink.setAttribute('class', 'bylink secure_delete');
  243. olink.setAttribute('onClick', 'javascript: overwrite_reload("' + thing_id + '")');
  244. olink.setAttribute('href', 'javascript:void(0)');
  245. olink.appendChild(document.createTextNode('OVERWRITE'));
  246. overwrite_link.appendChild(olink);
  247. main_parent.querySelector("ul.flat-list").appendChild(overwrite_link);
  248. } catch (e) {
  249. alert("Error adding Secure Delete links to comments.\nError: " + e + " Stack:" + e.stack);
  250. }
  251. }
  252. }