Skip to content

Instantly share code, notes, and snippets.

@vikhyat
Created April 1, 2010 15:11
Show Gist options
  • Save vikhyat/351914 to your computer and use it in GitHub Desktop.
Save vikhyat/351914 to your computer and use it in GitHub Desktop.
function is_superman() {
return true;
}
function superman_inf() {
return "∞";
}
$(function () {
var titleunhover = function () {
$(this).find("a.title").next("a.censor").remove();
};
var titlehover = function () {
var parent = $(this).parent();
parent.find("a.title").next("a.censor").remove();
parent.find("a.title").after("<a class='pretty-button negative censor'>edit / censor</a>");
parent.find("a.censor").click(function () {
var a_censor = $(this);
var a_title = a_censor.prev();
var p_title = a_censor.parent();
a_censor.replaceWith("<a class='pretty-button positive save'>save</a>");
var save_button = p_title.find("a.save");
a_title.attr("contentEditable", "true").addClass("editbox").unbind();
save_button.click(function () {
var p_title = $(this).parent();
var title = p_title.find("a.title").html();
p_title.thing().store_state('title', title);
p_title.find("a.positive").remove().end().find("a.title").attr("contentEditable", "false").hover(titlehover, function () {}).removeClass("editbox").end().hover(function () {}, titleunhover);
})
}).show();
};
$(".link a.title").hover(titlehover, function () {});
$("p.title").hover(function () {}, titleunhover);
$(".content").addClass("magic");
});
(function ($) {
$.fn.superban = function (kind, nostore) {
if (!$.defined(nostore) || !nostore) {
$(this).store_state('ban', kind, "author");
}
$(this).same_author().each(function () {
$(this).removeClass("ban").removeClass("x-ban").removeClass("ninjaban").addClass(kind).find(".superman-box").hide();
});
return false;
};
$.fn.findshills = function (stage) {
var thing = $(this);
if (stage == 0) {
thing.children('.entry').find('.find-shills.saving').show();
setTimeout(function () {
thing.findshills(1);
}, 1500);
} else {
thing.children('.entry').find('.find-shills.saving').hide();
thing.children('.entry').find('.find-shills.jobid').show();
}
};
$.fn.refudge = function () {
var thing = $(this);
var base_score = parseInt(thing.attr("base_score"));
var fudge = parseInt(thing.attr("votefudge"));
var fake_score = base_score + fudge;
if (isNaN(fake_score)) {
return;
}
var score_likes = fake_score + 1;
var score_unvoted = fake_score;
var score_dislikes = fake_score - 1;
if (fudge >= 10) {
score_likes = superman_inf();
}
if (fudge <= -10) {
score_dislikes = "-" + superman_inf();
}
if (thing.hasClass("comment") || thing.hasClass("compressed")) {
score_likes += Math.abs(score_likes) == 1 ? " point" : " points";
score_unvoted += Math.abs(score_unvoted) == 1 ? " point" : " points";
score_dislikes += Math.abs(score_dislikes) == 1 ? " point" : " points";
}
thing.children(".entry, .midcol").find(".score.likes").html(score_likes);
thing.children(".entry, .midcol").find(".score.unvoted").html(score_unvoted);
thing.children(".entry, .midcol").find(".score.dislikes").html(score_dislikes);
}
$.fn.realvote = $.fn.vote;
$.fn.vote = function (vh, callback, event) {
var elem = $(this);
if (!is_superman()) {
return elem.realvote(vh, callback, event);
}
var thing = elem.thing();
var up = elem.hasClass("upmod") || elem.hasClass("up");
var mod = elem.hasClass("upmod") || elem.hasClass("downmod");
var fudge = parseInt(thing.attr("votefudge"));
if (isNaN(fudge)) {
fudge = 0;
}
if (mod) {
if (up) {
if (++fudge > 10) {
fudge = 10;
}
} else {
if (--fudge < -10) {
fudge = -10;
}
}
thing.store_state('fudge', fudge);
thing.attr("votefudge", fudge);
thing.refudge();
} else {
elem.realvote(vh, callback, event);
}
};
var state_cookie = "state";
$.fn.store_state = function (action, data, thing_id_sel) {
thing_id_sel = $.with_default(thing_id_sel, "thing");
$.cookie_name_prefix('');
var c = $.cookie_read(state_cookie).data || [];
if (reddit.logged) {
$.cookie_name_prefix(reddit.logged);
}
var tid = $(this).thing_id(thing_id_sel);
var found = false;
for (var i = 0; i < c.length; i++) {
if (c[i][0] == tid && c[i][1] == action) {
found = true;
c[i][2] = data;
}
}
if (!found) {
c.push([tid, action, data]);
}
var i;
for (i = 0; i < 100; i++) {
var len = $.toJSON(c).length;
if (len > 4000) {
c.shift();
}
}
$.cookie_name_prefix("");
$.cookie_write({
name: state_cookie,
data: c
});
if (reddit.logged) {
$.cookie_name_prefix(reddit.logged);
}
};
$.read_state = function () {
$.cookie_name_prefix("");
var c = $.cookie_read(state_cookie);
if (reddit.logged) {
$.cookie_name_prefix(reddit.logged);
}
if (c) {
c = c.data;
for (var i = 0; i < c.length; i++) {
var things = $.things(c[i][0]);
if (things.length == 0) {
things = $(".author.id-" + c[i][0]).parents(".thing:first");
}
things.each(function () {
var thing = $(this);
var action = c[i][1];
var data = c[i][2];
if (action == 'title') {
thing.find("a.title").html(data);
} else if (action == 'fudge') {
thing.attr("votefudge", parseInt(data));
thing.refudge();
} else if (action == 'ban') {
thing.superban(data, true);
}
});
}
}
};
})(jQuery);
$(function () {
$.read_state();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment