RaaW

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

目前为 2014-08-25 提交的版本。查看 最新版本

// ==UserScript==
// @name RaaW
// @version 1.9.0
// @namespace RaaW
// @description Reddit as a Weapon script. Parts and idea by /u/noeatnosleep, enhanced by /u/enim
// @include http://www.reddit.com/user/*
// @include http://www.reddit.com/r/*
// @include http://*reddit.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
// ==/UserScript==

this.$ = this.jQuery = jQuery.noConflict(true);

var currentPage = document.URL;
var parsedPage = currentPage.split('/');
var modHash = null;
var currentUser = null;
var subreddit = null;
var commentIDs = [];
var topicIDs = [];
var subredditList = [];
var reportID = [];

var commentPage;	
commentPage = document.getElementsByClassName('comment');

function getHash(callback) {
    var query = new XMLHttpRequest();
    query.onreadystatechange = function () {
        if (query.readyState == 4) {
            var info = JSON.parse(query.responseText);
            modHash = info.data.modhash;
            callback();
        }
    }
    query.open('GET', 'http://www.reddit.com/api/me.json', true);
    query.send(null);
}

function generateToolbar () {
	$(document).find('#header')[0].style.paddingTop='18px';
    var newToolbar = document.createElement("div");
	newToolbar.id = "newToolbar";
	document.body.insertBefore(newToolbar,document.body.firstChild);
	var subredditbar = document.getElementById('sr-header-area');
	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;";
	newToolbar.style.backgroundColor='#f0f0f0';
	newToolbar.style.paddingLeft = '6px';
    newToolbar.style.paddingTop = '3px';
    newToolbar.style.paddingBottom = '3px';
    newToolbar.style.top='0px';
    newToolbar.innerHTML += "<a id='downvoteComment' style='color:black;' href='#'>DOWNVOTE ALL</a> | <a id='upvoteComment' style='color:black;' href='#'>UPVOTE ALL</a> |";
	if (commentPage.length == 0) {
		newToolbar.innerHTML += " <a id=reportComment style='color:black;' href='#'>REPORT POSTS</a> ";
	}
	else {
		newToolbar.innerHTML += "<a id=reportComment style='color:black;' href='#'> REPORT ALL</a> ";	
	}
	if (parsedPage[3] == 'user'){
		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>";
	}

 }


function doStuff() {
    $('#reportComment').on('click',function(e) {
       for(var i = 0; i < reportID.length; i++) {reportItem(i, 3);}
    alert('All items on this page were reported.');
    });   
    $('#downvoteComment').on('click',function(e){
    	theDownvoter();
    });
    $('#upvoteComment').on('click',function(e){
    	theUpvoter();
    });
    $('#upvoteAndOpen').on('click',function(e){
    	clickYourUpvotes();
    });
    $('#downvoteAndOpen').on('click',function(e){
    	clickYourDownvotes();
    });
    $('#reportUserToSpam').on('click',function(e){
    	reportToSpam();
    });
    $('#analyzeSend').on('click',function(e){
    	analyzeSend();
    });
	
}

function buildReportArray() {
	if (commentPage.length == 0) {
		var threads;
		threads = $('#siteTable').find('.thing');
		for (i = 0; i < threads.length; i++) {
			 reportID.push(threads[i].getAttribute('data-fullname'));
		}
	}
	else if (commentPage.length != 0) {
		var threads;
		threads = $('.commentarea').find('.thing');
		for (i = 0; i < threads.length; i++) {
			 reportID.push(threads[i].getAttribute('data-fullname'));
		}
	}
}

function reportItem(index, num) {
  if(num == 3) {var fullname = topicIDs[index];}
  else{var fullname = commentIDs[index];}
  $.post('http://www.reddit.com/api/report', {'id': fullname, 'uh': modHash});
}


function theDownvoter() {
	if (parsedPage[3] == 'user'){
		var items = $('#siteTable').find('.arrow.down');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	}
	else {
		if (commentPage.length == 0) {
			var items = $('#siteTable').find('.arrow.down');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	 	}
	 	else {
			var items = $('.commentarea').find('.arrow.down');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	 	}
	}
}

function theUpvoter() {
	if (parsedPage[3] == 'user'){
		var items = $('#siteTable').find('.arrow.up');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	}
	else {
		if (commentPage.length == 0) {
			var items = $('#siteTable').find('.arrow.up');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	 	}
	 	else {
			var items = $('.commentarea').find('.arrow.up');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
	 	}
	}
}

function clickYourUpvotes() {
	var clickandupvote = [];
	$('#siteTable').find('.flat-list.buttons').find('.first').find('a.comments, a.bylink').each(function(index,value){
		clickandupvote.push($(this).attr('href'));	
	});
	for (i=0;i < clickandupvote.length;i++){
		window.open(clickandupvote[i]);
	}
	var items = $('#siteTable').find('.arrow.up');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
}

function clickYourDownvotes() {
	var clickandupvote = [];
	$('#siteTable').find('.flat-list.buttons').find('.first').find('a.comments, a.bylink').each(function(index,value){
		clickandupvote.push($(this).attr('href'));	
	});
	for (i=0;i < clickandupvote.length;i++){
		window.open(clickandupvote[i]);
	}
	var items = $('#siteTable').find('.arrow.down');
		    Array.prototype.forEach.call(items, function(el, i){
		      setTimeout(function(){
		        el.click();
		      },100 + ( i * 400 ));
		    });
		    return false;
}
		
function reportToSpam(){
	var username = $(document).find('.pagename.selected').text();
	window.open('http://www.reddit.com/r/spam/submit?title=overview for '+ username + '&url=http://www.reddit.com/user/' + username );
}

function analyzeSend(){
	var username = $(document).find('.pagename.selected').text();
	window.open('http://www.reddit.com/message/compose/?to=analyzereddit&subject=analyze&message='+ username);
}

if (document.documentElement.lang === 'np') {
    document.documentElement.lang = 'en-us';
}
document.body.classList.add('subscriber');

delete_function = function(thread_root) {
    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)"]');
    for(var i=0; i < elmnts.length; i++) {
	setTimeout(
	    (function(_elmnt) {
		return function() {
		    var event = document.createEvent('UIEvents');
		    event.initUIEvent('click', true, true, window, 1);
		    _elmnt.dispatchEvent(event);
		}}
	    )(elmnts[i]), 1500*i); // 1.5s timeout prevents overloading reddit.
    };
}

delete_function = function(thread_root) {
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)"]');
for(var i=0; i < elmnts.length; i++) {
setTimeout(
(function(_elmnt) {
return function() {
var event = document.createEvent('UIEvents');
event.initUIEvent('click', true, true, window, 1);
_elmnt.dispatchEvent(event);
}}
)(elmnts[i]), 1500*i); // 1.5s timeout prevents overloading reddit.
};
}
 

//source reveal (creesch)

$('.comments-page .comment .flat-list.buttons').each(function () { // this targets each flat-list belonging to comments on a comment page. 
    $(this).append('<li><a class="view-source" href="javascript:void(0)">view source</a></li>'); // it then adds the view source button in the belonging function
});

$('body').on('click', '.view-source', function () { // On clicking of the view source button we do what we want to do. Note that we start with body since that is a constant dom element. If you try to target added dom elements directly it will not work. 
    var $this = $(this), // We posisbly want to reuse $(this), it is cleaner to define jquery objects you want to reuse. 
        $parentThing = $this.closest('.thing'),
        thingId = $parentThing.attr('data-fullname'); // we need an id to throw at the api, luckily it is is present in the html.
    
    if($parentThing.find('#box-' + thingId).length) { // Lets see if we already did do this before.
        $parentThing.find('#box-' + thingId).toggle(); // we did, toggle
    } else { // we did not, grab the info.
        $.getJSON('/api/info.json?id=' + thingId, function () { // lets do an ajax call to grab the info we need.
            console.log("success"); // you can remove this, basically lets you know a json call is done.
        })
            .done(function (data) { // by doing the stuff we need to do in .done we make sure we have the data needed since ajax is async.
            var commentBody = data.data.children[0].data.body; // json is basically an object. 
                var commentSourceBox = '<textarea style="display:block" rows="10" cols="50">'+ commentBody + '</textarea>'; // build the source box.
            $parentThing.find('.flat-list').first().before(commentSourceBox); // and add it to the .thing, note that I use .first() if I didn't do that it would add the source box for all child comments as well. 
        });
    }
});

//updownuser

var currentPage = document.URL;
var parsedPage = currentPage.split('/');
var modHash = null;
var currentUser = null;
var commentIDs = [];
var topicIDs = [];
var subredditList = [];

function upVote(index) {
    var fullname = commentIDs[index];
     $.post('http://www.reddit.com/api/vote', {'id': fullname, 'dir': 1, 'uh': modHash});
}
function neutralVote(index) {
    var fullname = commentIDs[index];
     $.post('http://www.reddit.com/api/vote', {'id': fullname, 'dir': 0, 'uh': modHash});
}
function downVote(index) {
    var fullname = commentIDs[index];
    $.post('http://www.reddit.com/api/vote', {'id': fullname, 'dir': -1, 'uh': modHash});
}
function getHash(callback) {
    var query = new XMLHttpRequest;
    query.onreadystatechange = function () {
        if (query.readyState == 4) {
            var info = JSON.parse(query.responseText);
            modHash = info.data.modhash;
            callback();
        }
    }
    query.open('GET', 'http://www.reddit.com/api/me.json', true);
    query.send(null);
}
function init() {
    $('div.menuarea').append('<div id=UPVOTE></div>');
    $('div.menuarea').append('<div id=NEUTRAL></div>');
    $('div.menuarea').append('<div id=DOWNVOTE></div>');
    var upButton = document.createElement('button');
    var downButton = document.createElement('button');
    var neutral = document.createElement('button');
    var upText = document.createTextNode('upvote');
    var nText = document.createTextNode('neutral');
    var downText = document.createTextNode('downvote');
    upButton.appendChild(upText);
    neutral.appendChild(nText);
    downButton.appendChild(downText);
    $('#UPVOTE').append(upButton);
    $('#NEUTRAL').append(neutral);
    $('#DOWNVOTE').append(downButton);
    $('#UPVOTE').css({'position':'absolute','top':'77px', 'left':'150px'});
    $('#NEUTRAL').css({'position':'absolute','left':'218px','top':'77px'});
    $('#DOWNVOTE').css({'position':'absolute','left':'286px','top':'77px'});

    $('#UPVOTE').click(function() {
        for(var i = 0; i < 25; i++) {
            upVote(i);
            var strippedTopic = topicIDs[i].split('_');
            var strippedComment = commentIDs[i].split('_');
            var url = 'http://www.reddit.com/r/' + subredditList[i] + '/comments/' + strippedTopic[1] + '/xml/' + strippedComment[1];
            window.open(url);
        }
    });

    $('#DOWNVOTE').click(function() {
        for(var i = 0; i < 25; i++) {
            downVote(i);
            var strippedTopic = topicIDs[i].split('_');
            var strippedComment = commentIDs[i].split('_');
            var url = 'http://www.reddit.com/r/' + subredditList[i] + '/comments/' + strippedTopic[1] + '/xml/' + strippedComment[1];
            window.open(url);
        }
    });

    $('#NEUTRAL').click(function() {
        for(var i = 0; i < 25; i++) {
            neutralVote(i);
            var strippedTopic = topicIDs[i].split('_');
            var strippedComment = commentIDs[i].split('_');
            var url = 'http://www.reddit.com/r/' + subredditList[i] + '/comments/' + strippedTopic[1] + '/xml/' + strippedComment[1];
            window.open(url);
        }

    });

}

function getIDs() {
    var query = new XMLHttpRequest();
    query.onreadystatechange = function() {
        if(query.readyState == 4) {
            var info = JSON.parse(query.responseText);
            for(i = 0; i < info.data.children.length; i++){
                commentIDs.push('t1_' + info.data.children[i].data.id);
                topicIDs.push(info.data.children[i].data.link_id);
                subredditList.push(info.data.children[i].data.subreddit);

            }
        }
    }

    query.open('GET', 'http://www.reddit.com/user/' + currentUser + '/comments.json', true);
    query.send(null);
}

if (parsedPage[3] == 'user') {
    currentUser = parsedPage[4];
    getIDs();
    getHash(init);

}    


generateToolbar(), getHash(),buildReportArray(), doStuff();