Guest User

Untitled

a guest
Jun 14th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        BnW FsB GvN
  3. // @namespace   bnw_default_interface_fix
  4. // @description Turns off ajax autoremoval deleted messages/comments; turns off 20 posts per page limit; hides every blacklisted post until it gets comments/recommendation
  5. // @include     http://bnw.im/*
  6. // @include     https://bnw.im/*
  7. // @version     1.4
  8. // @grant       unsafeWindow
  9. // ==/UserScript==
  10. (function (window, undefined) {
  11.     'use strict';
  12.     var w;
  13.     if (typeof unsafeWindow !== undefined) {
  14.         w = unsafeWindow;
  15.     } else {
  16.         w = window;
  17.     }
  18.     if (w.self != w.top) {
  19.         return;
  20.     }
  21.  
  22.     var BLACKLIST = ['anonymous', 'moskvano', 'x01eg'];
  23.  
  24.     var patch_bnw = function() {
  25.         w.ws.onclose = function() {};
  26.         w.ws.close();
  27.  
  28.         var add_node = function(html, to, at_top) {
  29.             var node = $(html).hide();
  30.             node.addClass("outerborder_added");
  31.             node.find("img.avatar").removeClass("avatar_ps");
  32.             node.find("img.imgpreview_ps").each(function() {
  33.                 $(this).removeClass("imgpreview_ps");
  34.             });
  35.             $("code",node).each(function(i, e) {hljs.highlightBlock(e);});
  36.             node.mouseover(function() {
  37.                 $(this).removeClass("outerborder_added");
  38.                 $(this).unbind("mouseover");
  39.             });
  40.             if (at_top) {
  41.                 node.prependTo(to);
  42.             } else {
  43.                 node.appendTo(to);
  44.             }
  45.             node.fadeIn("slow");
  46.             w.change_favicon();
  47.         };
  48.  
  49.         var main_page_handler = function(e) {
  50.             var d = JSON.parse(e.data);
  51.             var msg;
  52.             if (d.type == "new_message" &&
  53.                 window.location.search.indexOf("page") == -1) {
  54.                 // Add new messages only to first page.
  55.                     add_node(d.html, "#messages", true);
  56.                     w.add_main_page_actions(d.id, d.user);
  57.                     // make anonymous msg invisible until it gets recommendation
  58.                     if (w.is_anonymous_filtered && BLACKLIST.includes(d.user)) {
  59.                         msg = $("#"+d.id);
  60.                         if (msg.length) {
  61.                             msg.hide();
  62.                         }
  63.                     }
  64.             } else if (d.type == "del_message") {
  65.                 msg = $("#"+d.id);
  66.                 if (msg.length) {
  67.                     msg.removeClass("outerborder_added"
  68.                     ).addClass("outerborder_deleted");
  69.                 }
  70.             } else if (d.type == "upd_comments_count") {
  71.                 msg = $("#"+d.id);
  72.                 var t;
  73.                 if (msg.length) {
  74.                     t = msg.find("div.sign").contents()[3];
  75.                     t.nodeValue = t.nodeValue.replace(/\([0-9]+(\+)?/, "("+d.num+"$1");
  76.                 }
  77.                 // make anonymous msg visible since it got a comment
  78.                 if (w.is_anonymous_filtered && BLACKLIST.includes(msg.find(".usrid").text().slice(1))) {
  79.                     if (d.num > 0) {
  80.                         msg.show();
  81.                     } else {
  82.                         msg.hide();
  83.                     }
  84.                 }
  85.             } else if (d.type == "upd_recommendations_count") {
  86.                 msg = $("#"+d.id);
  87.                 if (msg.length) {
  88.                     t = msg.find("div.sign").contents()[3];
  89.                     var val = t.nodeValue;
  90.                     var re = /\+[0-9]+\)/;
  91.                     var new_val = d.num ? "+"+d.num+")" : ")";
  92.                     if (val.match(re)) {
  93.                         t.nodeValue = val.replace(re, new_val);
  94.                     } else {
  95.                         t.nodeValue = val.replace(/\)/, new_val);
  96.                     }
  97.                     // make anonymous msg visible since it got recommendation
  98.                     if (w.is_anonymous_filtered && BLACKLIST.includes(msg.find(".usrid").text().slice(1))) {
  99.                         if (d.num > 0) {
  100.                             msg.show();
  101.                         } else {
  102.                             msg.hide();
  103.                         }
  104.                     }
  105.                 }
  106.             }
  107.         };
  108.  
  109.         var message_page_handler = function(e) {
  110.             var d = JSON.parse(e.data);
  111.             if (d.type == "new_comment") {
  112.                 add_node(d.html, "#comments", false);
  113.                 w.add_message_page_actions(d.id, d.user);
  114.             } else if (d.type == "del_comment") {
  115.                 var short_id = d.id.split("/")[1];
  116.                 var comment = $("#"+short_id);
  117.                 if (comment.length) {
  118.                     comment.removeClass("outerborder_added"
  119.                     ).addClass("outerborder_deleted");
  120.                 }
  121.             }
  122.         };
  123.  
  124.         w.is_anonymous_filtered = localStorage.is_anonymous_filtered == "true" ? true : false;
  125.         filter_anonymous_with_no_recommends();
  126.  
  127.         switch (w.page_type) {
  128.             case "main":
  129.                 w.onmessage = main_page_handler;
  130.                 w.openws();
  131.                 // filter control button
  132.                 $("#login_button").before("<a href='#' class='headlink' id='filter_anon_button'>"+
  133.                     (w.is_anonymous_filtered ? "Вернуть пуки" : "Откачать пуки")+
  134.                     "</a>");
  135.                 $("#filter_anon_button").click(function() {
  136.                     change_filter_state();
  137.                     filter_anonymous_with_no_recommends();
  138.                 });
  139.                 break;
  140.             case "message":
  141.                 w.onmessage = message_page_handler;
  142.                 w.openws();
  143.                 break;
  144.             case "user":
  145.                 w.onmessage = main_page_handler;
  146.                 w.openws();
  147.                 break;
  148.         }
  149.  
  150.         function change_filter_state() {
  151.             w.is_anonymous_filtered = w.is_anonymous_filtered ? false : true;
  152.             localStorage.is_anonymous_filtered = w.is_anonymous_filtered;
  153.             var caption = w.is_anonymous_filtered ? "Вернуть пуки" : "Откачать пуки";
  154.             $("#filter_anon_button").text(caption);
  155.         }
  156.  
  157.         function filter_anonymous_with_no_recommends() {
  158.             // preloaded posts
  159.             $(".message").each(function() {
  160.                 var msg = $(this);
  161.                 if (BLACKLIST.includes(msg.find(".usrid").text().slice(1))) {
  162.                     var t = msg.find("div.sign").contents()[3];
  163.                     var val = t.nodeValue;
  164.                     var comments_re = /\(([1-9][0-9]*)\+?/;
  165.                     var recommends_re = /\+[0-9]+\)/;
  166.                     if (w.is_anonymous_filtered && !val.match(recommends_re) && !val.match(comments_re)) {
  167.                         // seems u got no recommends
  168.                         msg.hide();
  169.                     } else {
  170.                         msg.show();
  171.                     };
  172.                 }
  173.             });
  174.         }
  175.  
  176.         console.log('BnW ajax handlers have been successfuly patched');
  177.     };
  178.  
  179.     if (w.addEventListener)
  180.         w.addEventListener("load", patch_bnw, false);
  181.     else if (w.attachEvent)
  182.         w.attachEvent("onload", patch_bnw);
  183. })(window);
Add Comment
Please, Sign In to add comment