Skip to content

Instantly share code, notes, and snippets.

Created April 18, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1e2338b281b2acded55e920f46dc1fec to your computer and use it in GitHub Desktop.
Save anonymous/1e2338b281b2acded55e920f46dc1fec to your computer and use it in GitHub Desktop.
BnW ajax handlers patch: turns off ajax autoremove deleted messages/comments; turns off 20 posts per page limit; hides every anonymous post until it gets recommendation.
// ==UserScript==
// @name BnW FsB GvN
// @namespace bnw_default_interface_fix
// @description Turns off ajax autoremove deleted messages/comments; turns off 20 posts per page limit; hides every anonymous post until it gets recommendation
// @include http://bnw.im/*
// @include https://bnw.im/*
// @version 1.1
// @grant none
// ==/UserScript==
(function (window, undefined) {
var w;
if (typeof unsafeWindow != undefined) {
w = unsafeWindow
} else {
w = window;
}
if (w.self != w.top) {
return;
}
var patch_bnw = function() {
w.ws.onclose = function() {};
w.ws.close();
var add_node = function(html, to, at_top) {
var node = $(html).hide();
node.addClass("outerborder_added");
node.find("img.avatar").removeClass("avatar_ps");
node.find("img.imgpreview_ps").each(function() {
$(this).removeClass("imgpreview_ps");
});
$("code",node).each(function(i, e) {hljs.highlightBlock(e)});
node.mouseover(function() {
$(this).removeClass("outerborder_added");
$(this).unbind("mouseover");
});
if (at_top) {
node.prependTo(to);
} else {
node.appendTo(to);
}
node.fadeIn("slow");
w.change_favicon();
}
var main_page_handler = function(e) {
var d = JSON.parse(e.data);
if (d.type == "new_message" &&
window.location.search.indexOf("page") == -1) {
// Add new messages only to first page.
add_node(d.html, "#messages", true);
w.add_main_page_actions(d.id, d.user);
// make anonymous msg invisible until it gets recommendation
if (w.is_anonymous_filtered && d.user == 'anonymous') {
var msg = $("#"+d.id);
if (msg.length) {
msg.hide();
}
}
} else if (d.type == "del_message") {
var msg = $("#"+d.id);
if (msg.length) {
msg.removeClass("outerborder_added"
).addClass("outerborder_deleted");
}
} else if (d.type == "upd_comments_count") {
var msg = $("#"+d.id);
if (msg.length) {
var t = msg.find("div.sign").contents()[3];
t.nodeValue = t.nodeValue.replace(/\([0-9]+(\+)?/, "("+d.num+"$1")
}
} else if (d.type == "upd_recommendations_count") {
var msg = $("#"+d.id);
if (msg.length) {
var t = msg.find("div.sign").contents()[3];
var val = t.nodeValue;
var re = /\+[0-9]+\)/;
var new_val = d.num ? "+"+d.num+")" : ")";
if (val.match(re)) {
t.nodeValue = val.replace(re, new_val);
} else {
t.nodeValue = val.replace(/\)/, new_val);
}
// make anonymous msg visible since it got recommendation
if (w.is_anonymous_filtered && $(msg).find(".usrid").text() == '@anonymous') {
if (d.num > 0) {
msg.show();
} else {
msg.hide();
}
}
}
}
}
var message_page_handler = function(e) {
var d = JSON.parse(e.data);
if (d.type == "new_comment") {
add_node(d.html, "#comments", false);
w.add_message_page_actions(d.id, d.user);
} else if (d.type == "del_comment") {
var short_id = d.id.split("/")[1];
var comment = $("#"+short_id);
if (comment.length) {
comment.removeClass("outerborder_added"
).addClass("outerborder_deleted");
}
}
}
w.is_anonymous_filtered = localStorage.is_anonymous_filtered ? true : false;
filter_anonymous_with_no_recommends();
switch (w.page_type) {
case "main":
w.onmessage = main_page_handler;
w.openws();
// filter control button
$("#login_button").before("<a href='#' class='headlink' id='filter_anon_button'>"+
(w.is_anonymous_filtered ? "Вернуть пуки" : "Откачать пуки")+
"</a>")
$("#filter_anon_button").click(function() {
change_filter_state();
filter_anonymous_with_no_recommends();
});
break;
case "message":
w.onmessage = message_page_handler;
w.openws();
break;
case "user":
w.onmessage = main_page_handler;
w.openws();
break;
}
function change_filter_state() {
w.is_anonymous_filtered = w.is_anonymous_filtered ? false : true;
localStorage.is_anonymous_filtered = w.is_anonymous_filtered;
var caption = w.is_anonymous_filtered ? "Вернуть пуки" : "Откачать пуки";
$("#filter_anon_button").text(caption);
}
function filter_anonymous_with_no_recommends() {
// preloaded posts
$(".message").each(function() {
var msg = $(this);
if ($(this).find(".usrid").text() == "@anonymous") {
var t = msg.find("div.sign").contents()[3];
var val = t.nodeValue;
var re = /\+[0-9]+\)/;
if (w.is_anonymous_filtered && !val.match(re)) {
// seems u got no recommends
msg.hide();
} else {
msg.show();
};
}
});
}
console.log('BnW ajax handlers have been successfuly patched');
}
if (w.addEventListener)
w.addEventListener("load", patch_bnw, false);
else if (w.attachEvent)
w.attachEvent("onload", patch_bnw);
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment