RaaW

Reddit as a Weapon script. Parts and idea by /u/noeatnosleep, enhanced by /u/enim

当前为 2014-08-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name RaaW
  3. // @version 1.8.2
  4. // @namespace RaaW
  5. // @description Reddit as a Weapon script. Parts and idea by /u/noeatnosleep, enhanced by /u/enim
  6. // @include http://www.reddit.com/user/*
  7. // @include http://www.reddit.com/r/*
  8. // @include http://*reddit.com/*
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
  10. // ==/UserScript==
  11.  
  12. this.$ = this.jQuery = jQuery.noConflict(true);
  13.  
  14. var currentPage = document.URL;
  15. var parsedPage = currentPage.split('/');
  16. var modHash = null;
  17. var currentUser = null;
  18. var subreddit = null;
  19. var commentIDs = [];
  20. var topicIDs = [];
  21. var subredditList = [];
  22. var reportID = [];
  23.  
  24. var commentPage;
  25. commentPage = document.getElementsByClassName('comment');
  26.  
  27. function getHash(callback) {
  28. var query = new XMLHttpRequest();
  29. query.onreadystatechange = function () {
  30. if (query.readyState == 4) {
  31. var info = JSON.parse(query.responseText);
  32. modHash = info.data.modhash;
  33. callback();
  34. }
  35. }
  36. query.open('GET', 'http://www.reddit.com/api/me.json', true);
  37. query.send(null);
  38. }
  39.  
  40. function generateToolbar () {
  41. $(document).find('#header')[0].style.paddingTop='18px';
  42. var newToolbar = document.createElement("div");
  43. newToolbar.id = "newToolbar";
  44. document.body.insertBefore(newToolbar,document.body.firstChild);
  45. var subredditbar = document.getElementById('sr-header-area');
  46. newToolbar.style.cssText="color:black;background-color:'#f0f0f0';border-bottom:1px black solid;font-family:verdana, arial, helvetica, sans-serif;font-size:90%;height:12px;padding:3px 0px 3px 6px;text-transform:uppercase;width:100%;z-index:+999999;position:fixed;top:0;";
  47. newToolbar.style.backgroundColor='#f0f0f0';
  48. newToolbar.style.paddingLeft = '6px';
  49. newToolbar.style.paddingTop = '3px';
  50. newToolbar.style.paddingBottom = '3px';
  51. newToolbar.style.top='0px';
  52. newToolbar.innerHTML += "<a id='downvoteComment' style='color:black;' href='#'>DOWNVOTE ALL</a> | <a id='upvoteComment' style='color:black;' href='#'>UPVOTE ALL</a> |";
  53. if (commentPage.length == 0) {
  54. newToolbar.innerHTML += " <a id=reportComment style='color:black;' href='#'>REPORT POSTS</a> ";
  55. }
  56. else {
  57. newToolbar.innerHTML += "<a id=reportComment style='color:black;' href='#'> REPORT ALL</a> ";
  58. }
  59. if (parsedPage[3] == 'user'){
  60. newToolbar.innerHTML += "<a id='upvoteAndOpen' style='color:black;' href='#'> | OPEN AND UPVOTE </a> <a id='downvoteAndOpen' style='color:black;' href='#'> | OPEN AND DOWNVOTE </a> <a id='analyzeSend' style='color:black;' href='#'> | ANALYZE </a> <a id='reportUserToSpam' style='color:black;' href='#'> | REPORT ON /R/SPAM </a>";
  61. }
  62.  
  63. }
  64.  
  65.  
  66. function doStuff() {
  67. $('#reportComment').on('click',function(e) {
  68. for(var i = 0; i < reportID.length; i++) {reportItem(i, 3);}
  69. alert('All items on this page were reported.');
  70. });
  71. $('#downvoteComment').on('click',function(e){
  72. theDownvoter();
  73. });
  74. $('#upvoteComment').on('click',function(e){
  75. theUpvoter();
  76. });
  77. $('#upvoteAndOpen').on('click',function(e){
  78. clickYourUpvotes();
  79. });
  80. $('#downvoteAndOpen').on('click',function(e){
  81. clickYourDownvotes();
  82. });
  83. $('#reportUserToSpam').on('click',function(e){
  84. reportToSpam();
  85. });
  86. $('#analyzeSend').on('click',function(e){
  87. analyzeSend();
  88. });
  89. }
  90.  
  91. function buildReportArray() {
  92. if (commentPage.length == 0) {
  93. var threads;
  94. threads = $('#siteTable').find('.thing');
  95. for (i = 0; i < threads.length; i++) {
  96. reportID.push(threads[i].getAttribute('data-fullname'));
  97. }
  98. }
  99. else if (commentPage.length != 0) {
  100. var threads;
  101. threads = $('.commentarea').find('.thing');
  102. for (i = 0; i < threads.length; i++) {
  103. reportID.push(threads[i].getAttribute('data-fullname'));
  104. }
  105. }
  106. }
  107.  
  108. function reportItem(index, num) {
  109. if(num == 3) {var fullname = topicIDs[index];}
  110. else{var fullname = commentIDs[index];}
  111. $.post('http://www.reddit.com/api/report', {'id': fullname, 'uh': modHash});
  112. }
  113.  
  114.  
  115. function theDownvoter() {
  116. if (parsedPage[3] == 'user'){
  117. var items = $('#siteTable').find('.arrow.down');
  118. Array.prototype.forEach.call(items, function(el, i){
  119. setTimeout(function(){
  120. el.click();
  121. },100 + ( i * 400 ));
  122. });
  123. return false;
  124. }
  125. else {
  126. if (commentPage.length == 0) {
  127. var items = $('#siteTable').find('.arrow.down');
  128. Array.prototype.forEach.call(items, function(el, i){
  129. setTimeout(function(){
  130. el.click();
  131. },100 + ( i * 400 ));
  132. });
  133. return false;
  134. }
  135. else {
  136. var items = $('.commentarea').find('.arrow.down');
  137. Array.prototype.forEach.call(items, function(el, i){
  138. setTimeout(function(){
  139. el.click();
  140. },100 + ( i * 400 ));
  141. });
  142. return false;
  143. }
  144. }
  145. }
  146.  
  147. function theUpvoter() {
  148. if (parsedPage[3] == 'user'){
  149. var items = $('#siteTable').find('.arrow.up');
  150. Array.prototype.forEach.call(items, function(el, i){
  151. setTimeout(function(){
  152. el.click();
  153. },100 + ( i * 400 ));
  154. });
  155. return false;
  156. }
  157. else {
  158. if (commentPage.length == 0) {
  159. var items = $('#siteTable').find('.arrow.up');
  160. Array.prototype.forEach.call(items, function(el, i){
  161. setTimeout(function(){
  162. el.click();
  163. },100 + ( i * 400 ));
  164. });
  165. return false;
  166. }
  167. else {
  168. var items = $('.commentarea').find('.arrow.up');
  169. Array.prototype.forEach.call(items, function(el, i){
  170. setTimeout(function(){
  171. el.click();
  172. },100 + ( i * 400 ));
  173. });
  174. return false;
  175. }
  176. }
  177. }
  178.  
  179. function clickYourUpvotes() {
  180. var clickandupvote = [];
  181. $('#siteTable').find('.flat-list.buttons').find('.first').find('a.comments, a.bylink').each(function(index,value){
  182. clickandupvote.push($(this).attr('href'));
  183. });
  184. for (i=0;i < clickandupvote.length;i++){
  185. window.open(clickandupvote[i]);
  186. }
  187. var items = $('#siteTable').find('.arrow.up');
  188. Array.prototype.forEach.call(items, function(el, i){
  189. setTimeout(function(){
  190. el.click();
  191. },100 + ( i * 400 ));
  192. });
  193. return false;
  194. }
  195.  
  196. function clickYourDownvotes() {
  197. var clickandupvote = [];
  198. $('#siteTable').find('.flat-list.buttons').find('.first').find('a.comments, a.bylink').each(function(index,value){
  199. clickandupvote.push($(this).attr('href'));
  200. });
  201. for (i=0;i < clickandupvote.length;i++){
  202. window.open(clickandupvote[i]);
  203. }
  204. var items = $('#siteTable').find('.arrow.down');
  205. Array.prototype.forEach.call(items, function(el, i){
  206. setTimeout(function(){
  207. el.click();
  208. },100 + ( i * 400 ));
  209. });
  210. return false;
  211. }
  212. function reportToSpam(){
  213. var username = $(document).find('.pagename.selected').text();
  214. window.open('http://www.reddit.com/r/spam/submit?title=overview for '+ username + '&url=http://www.reddit.com/user/' + username );
  215. }
  216.  
  217. function analyzeSend(){
  218. var username = $(document).find('.pagename.selected').text();
  219. window.open('http://www.reddit.com/message/compose/?to=analyzereddit&subject=analyze&message='+ username);
  220. }
  221.  
  222. if (document.documentElement.lang === 'np') {
  223. document.documentElement.lang = 'en-us';
  224. }
  225. document.body.classList.add('subscriber');
  226.  
  227. delete_function = function(thread_root) {
  228. var elmnts = document.getElementsByClassName('id-'+thread_root)[0].querySelectorAll('form input[value="removed"]~span.option.error a.yes,a[onclick^="return big_mod_action($(this), -1)"]');
  229. for(var i=0; i < elmnts.length; i++) {
  230. setTimeout(
  231. (function(_elmnt) {
  232. return function() {
  233. var event = document.createEvent('UIEvents');
  234. event.initUIEvent('click', true, true, window, 1);
  235. _elmnt.dispatchEvent(event);
  236. }}
  237. )(elmnts[i]), 1500*i); // 1.5s timeout prevents overloading reddit.
  238. };
  239. }
  240.  
  241. delete_function = function(thread_root) {
  242. var elmnts = document.getElementsByClassName('id-'+thread_root)[0].querySelectorAll('form input[value="removed"]~span.option.error a.yes,a[onclick^="return big_mod_action($(this), -1)"]');
  243. for(var i=0; i < elmnts.length; i++) {
  244. setTimeout(
  245. (function(_elmnt) {
  246. return function() {
  247. var event = document.createEvent('UIEvents');
  248. event.initUIEvent('click', true, true, window, 1);
  249. _elmnt.dispatchEvent(event);
  250. }}
  251. )(elmnts[i]), 1500*i); // 1.5s timeout prevents overloading reddit.
  252. };
  253. }
  254. //nuke (EnigmaBlade)
  255.  
  256. delete_function = function(thread_root) {
  257. var elmnts = document.getElementsByClassName('id-'+thread_root)[0].querySelectorAll('form input[value="removed"]~span.option.error a.yes,a[onclick^="return big_mod_action($(this), -1)"]');
  258. for(var i=0; i < elmnts.length; i++) {
  259. setTimeout(
  260. (function(_elmnt) {
  261. return function() {
  262. var event = document.createEvent('UIEvents');
  263. event.initUIEvent('click', true, true, window, 1);
  264. _elmnt.dispatchEvent(event);
  265. }}
  266. )(elmnts[i]), 1500*i); // 1.5s timeout prevents overloading reddit.
  267. };
  268. }
  269.  
  270. if(document.querySelector('body.moderator')){ // only execute if you are a moderator
  271. var nuke_button = new Array();
  272. var divels = document.querySelectorAll('div.noncollapsed');
  273. var comment_ids = new Array();
  274.  
  275.  
  276. for (var i = 0; i < divels.length; i++) {
  277. var author_link = divels[i].querySelector('p.tagline>a.author,p.tagline>span.author,p.tagline>em');
  278. // p.tagline>a.author is normal comment;
  279. // some author deleted comments seem to have either
  280. // p.tagline>span.author or p.tagline>em
  281. comment_ids[i] = divels[i].parentElement.parentElement.getAttribute('data-fullname');
  282. if(author_link) {
  283. // create link DOM element
  284. nuke_button[i] = document.createElement('a')
  285. nuke_button[i].setAttribute('href', 'javascript:void(0)');
  286. nuke_button[i].setAttribute('title', 'Nuke!');
  287. nuke_button[i].setAttribute('id', 'nuke_'+i);
  288. nuke_button[i].innerHTML= "[<strong>Nuke</strong>]";
  289. // append after the author's name
  290. author_link.parentNode.insertBefore(nuke_button[i], author_link.nextSibling);
  291.  
  292. // Add listener for click; using IIFE to function with _i as value of i when created; not when clicked
  293. nuke_button[i].addEventListener('click',
  294. (function(_i) {
  295. return function() {
  296. var continue_thread = divels[_i].parentElement.parentElement.querySelectorAll('span.morecomments>a');
  297. var comment_str = " comments?";
  298. if(continue_thread.length > 0) {
  299. comment_str = "+ comments (more after expanding collapsed threads; there will be a pause before the first deletion to retrieve more comments)?";
  300. }
  301. var delete_button = divels[_i].parentElement.parentElement.querySelectorAll('form input[value="removed"]~span.option.error a.yes,a[onclick^="return big_mod_action($(this), -1)"]');
  302. // form input[value="removed"]~span.option.error a.yes -- finds the yes for normal deleting comments.
  303. // a.pretty-button.neutral finds the 'remove' button for flagged comments
  304. if (confirm("Are you sure you want to nuke the following " + delete_button.length + comment_str)) {
  305. for (var indx=0; indx < continue_thread.length; indx++) {
  306. var elmnt = continue_thread[indx];
  307. setTimeout(
  308. function() {
  309. var event = document.createEvent('UIEvents');
  310. event.initUIEvent('click', true, true, window, 1);
  311. elmnt.dispatchEvent(event);
  312. }, 2000*indx); // wait two seconds before each ajax call before clicking each "load more comments"
  313. }
  314. if(indx > 0) {
  315. setTimeout(function() {delete_function(comment_ids[_i])},
  316. 2000*(indx + 2)); // wait 4s after last ajax "load more comments"
  317. } else {
  318. delete_function(comment_ids[_i]); // call immediately if not "load more comments"
  319. }
  320. }
  321. }
  322. }
  323. )(i)); // end of IIFE (immediately invoked function expression)
  324. }
  325. }
  326. }
  327.  
  328. //source reveal (creesch)
  329.  
  330. $('.comments-page .comment .flat-list.buttons').each(function () {
  331. $(this).append('<li><a class="view-source" href="javascript:void(0)">view source</a></li>');
  332.  
  333. $(".commentSourceBox").toggle();
  334.  
  335. $('body').on('click', '.view-source', function () {
  336. var $this = $(this),
  337. $parentThing = $this.closest('.thing'),
  338. thingId = $parentThing.attr('data-fullname');
  339.  
  340. $.getJSON('/api/info.json?id=' + thingId, function () {
  341. console.log("success");
  342. })
  343. .done(function (data) {
  344. var commentBody = data.data.children[0].data.body;
  345. var commentSourceBox = '<textarea style="display:block" rows="10" cols="50">'+ commentBody + '</textarea>';
  346. $parentThing.find('.flat-list').first().before(commentSourceBox);
  347. });
  348. });
  349. generateToolbar(), getHash(),buildReportArray(), doStuff();