MineGeekYT / CF Voting Buffer

// ==UserScript==
// @name         CF Voting Buffer
// @namespace    http://codeforces.com/profile/PikMike
// @version      0.1
// @description  Plugin for those who missclick that tiny vote button all the time!
// @author       PikMike
// @include      *codeforces.*/blog/entry/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var votes = {};

    $(".vote-for-comment").unbind("click");
    $(".vote-for-comment").removeAttr("href");
    $(".vote-for-comment").css("cursor", "pointer");
    $("[votedirection='0']").attr("dd", "z");

    $(".vote-for-comment").click(function(){
        var vote = $(this);
        var commentId = vote.parent().attr("commentid");
        var voteDirection = (vote.attr("dd") === 'x' ? 1 :
                             vote.attr("dd") === 'y' ? -1 : 0);
        if (votes[commentId] === undefined || votes[commentId] === 0){
            if (voteDirection !== 0){
                if (voteDirection === 1)
                    vote.find("img").attr("src", "http://st.codeforces.com/s/32530/images/actions/comment-voteup.png");
                else
                    vote.find("img").attr("src", "http://st.codeforces.com/s/32530/images/actions/comment-votedown.png");
                vote.find("img").attr("popacity", "1.0");
                vote.find("img").css("opacity", "1.0");
            }
            votes[commentId] = voteDirection;
            vote.attr("votedirection", (votes[commentId] === 0 ? voteDirection.toString() : "0"));
        }
        else{
            if (voteDirection !== 0){
                vote.parent().children().first().find("img").attr("src", "http://st.codeforces.com/s/32530/images/actions/comment-voteup-blue.png");
                vote.parent().children().last().find("img").attr("src", "http://st.codeforces.com/s/32530/images/actions/comment-votedown-blue.png");
                vote.parent().find("img").attr("popacity", "0.35");
                vote.parent().find("img").css("opacity", "0.35");
            }
            votes[commentId] = 0;
            vote.parent().children().first().attr("votedirection", "1");
            vote.parent().children().last().attr("votedirection", "-1");
        }
    });

    function apply(){
        var fl = false;
        for (var entry in votes){
            var vote;

            if (votes[entry] === 1)
                vote = $('span[commentid="' + entry + '"]').first().children().first();
            else if (votes[entry] === -1)
                vote = $('span[commentid="' + entry + '"]').first().children().last();
            else
                continue;

            fl = true;
            var commentId = entry;
            var commentRating = vote.parent().attr("data-commentRating");
            var direction = votes[entry].toString();

            $.post("/data/comment/vote", {commentId: commentId, _tta: Codeforces.tta(), vote: direction, commentRating: commentRating}, function() {}, "json");
        }

        votes = {};
    }

    var btn = $(".up").clone();
    btn.html("✔");
    btn.removeClass("up").addClass("comm");
    btn.attr("title", "Save votes");
    btn.click(apply);
    $(".up").after(btn);

    if ($(".new-comments-box").css("display") === "none"){
        $(".up").hide();
        $(".down").hide();
    }
    else{
        var hr = document.createElement("hr");
        $(".comm").after(hr);
    }

    $(".new-comments-box").css({display: ''});

    $(window).bind("beforeunload", apply);
})();