updownuser

Upvote or Downvote from a userpage on reddit

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// @include http://www.reddit.com/user/*
// @name updownuser
// @version 0.0.1.20140825143127
// @namespace https://greasyfork.org/users/636
// @description Upvote or Downvote from a userpage on reddit
// ==/UserScript==

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);

}