Advertisement
Guest User

NN.RU User Hide

a guest
Jun 7th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        User Hide
  3. // @namespace   nnru
  4. // @description Hides user messages for selected users
  5. // @include     http://www.nn.ru/*
  6. // @version     1
  7. // @grant       none
  8.  
  9. // ==/UserScript==
  10.  
  11. function BranchItem() {
  12. }
  13. BranchItem.getItemHeader = function(el) {
  14.   return el.querySelector(".branch-head");
  15. }
  16. BranchItem.getItemBody = function(el) {
  17.   return el.querySelector(".branch-body");
  18. }
  19. BranchItem.getId = function(el) {
  20.   return el.querySelector(".PortraitLink").getAttribute("usid");
  21. }
  22. BranchItem.getUserControl = function(el) {
  23.   return el.querySelector(".userControl");
  24. }
  25. BranchItem.getMessageHideControl = function(el) {
  26.   return el.querySelector(".messageHide");
  27. }
  28. BranchItem.getOverlay = function(el) {
  29.   return el.querySelector(".shadowOverlay");
  30. }
  31. BranchItem.getContainer = function(a) {
  32.   return a.parentNode.parentNode;
  33. }
  34.  
  35. function User(id = 0, name = "") {
  36.   this.id = id;
  37.   this.name = "";
  38.  
  39.   this.valid = function() {
  40.     return this.id != 0;
  41.   }
  42.   this.init = function(obj) {
  43.     if (obj) {
  44.       if ("id" in obj) this.id = obj.id;
  45.       if ("name" in obj) this.name = obj.name;
  46.     }
  47.     if (this.id && !this.name) {
  48.       var el = document.querySelector("a[usid='" + this.id + "']");
  49.       if (el) this.name = el.text;
  50.     }
  51.     //console.log("User: id: " + this.id + ", name: " + this.name);
  52.   }
  53.   this.data = function() {
  54.     var obj = {
  55.       id: this.id,
  56.       name: this.name
  57.     };
  58.     return obj;
  59.   }
  60.   this.hide = function(el) {
  61.     var b = BranchItem.getItemBody(el);
  62.     var o = BranchItem.getOverlay(el);
  63.     b.style.display = 'none';
  64.     o.style.display = 'block';
  65.   }
  66.   this.show = function(el) {
  67.     var b = BranchItem.getItemBody(el);
  68.     if (b.style.display != undefined)
  69.       b.style.display = 'block';
  70.     var o = BranchItem.getOverlay(el);
  71.     o.style.display = 'none';
  72.   }
  73.  
  74.   this.init();
  75. }
  76.  
  77. function UserControl(el, userManager) {
  78.   this.parent = el;
  79.   this.toggleBox = document.createElement("a");
  80.   this.div = document.createElement("div");
  81.   this.div1 = document.createElement("div");
  82.   this.label = document.createElement("label");
  83.   this.checkbox = document.createElement("input");
  84.   this.vline = document.createElement("label");
  85.   this.optButton = document.createElement("label");
  86.   this.overlay = document.createElement("div");
  87.   // -- Options --
  88.   /*this.options = document.createElement("div");
  89.   this.input = document.createElement("input");
  90.   this.import = document.createElement("button");
  91.   this.export = document.createElement("button");*/
  92.   // ----
  93.   this.userManager = userManager;
  94.   this.id = BranchItem.getId(el);
  95.   this.mid = Math.floor(Math.random() * (999999 - 100000)) + 100000;
  96.  
  97.   this.hidden = function() {
  98.    
  99.   }
  100.   this.show = function() {
  101.     this.div1.style.border = "1px solid #AAA";
  102.     this.checkbox.checked = !!this.userManager.find(this.id);
  103.     this.label.style.display = 'inline';
  104.     this.checkbox.style.display = 'inline';
  105.     this.vline.style.display = 'inline';
  106.     this.optButton.style.display = 'inline';
  107.   }
  108.   this.hide = function() {
  109.     this.div1.style.border = "0px";
  110.     this.label.style.display = 'none';
  111.     this.checkbox.style.display = 'none';
  112.     this.vline.style.display = 'none';
  113.     this.optButton.style.display = 'none';
  114.   }
  115.   this.showOptions = function() {
  116.     console.log("Show options");
  117.     /*console.log(this.options, this.input);
  118.     this.input.value = this.userManager.getJSON();
  119.     this.options.style.position = 'absolute';
  120.     this.options.top = window.width / 2;
  121.     this.options.left = window.height / 2;
  122.    
  123.     document.body.appendChild(this.options);*/
  124.     var json = prompt("Enter JSON string to import new values OR copy the contents to export configuration to another browser",
  125.                                                                    this.userManager.getJSON());
  126.     //console.log(json);
  127.     if (json) {
  128.       this.userManager.importJSON(json);
  129.       this.userManager.update();
  130.     }
  131.   }
  132.   this.hideOptions = function() {
  133.     var b = BranchItem.getItemBody(this.parent);
  134.     return b.style.display == 'none';
  135.   }
  136.   this.createOverlay = function() {
  137.     //console.log("Creating overlay");
  138.     var b = BranchItem.getItemHeader(this.parent);
  139.     b.style.position = 'relative';
  140.     var firstElement = b.querySelector("a");
  141.     var left = firstElement.offsetLeft;
  142.     var top = 0;
  143.     var width = this.div.offsetLeft - left;
  144.     var height = 50;
  145.     var color = getComputedStyle(document.querySelector(".author-item"), null).backgroundColor;
  146.    
  147.     this.overlay.className = "shadowOverlay";
  148.     this.overlay.style.position = 'absolute';
  149.     this.overlay.style.left = left + "px";
  150.     this.overlay.style.top = top + "px";
  151.     this.overlay.style.width = width + "px";
  152.     this.overlay.style.height = height + "px";
  153.     this.overlay.style.backgroundColor = color;
  154.     this.overlay.style.opacity = 0.8;
  155.     this.overlay.style.borderRadius = "5px";
  156.     this.overlay.style.pointerEvents = 'none';
  157.     this.overlay.style.display = 'none';
  158.    
  159.     b.appendChild(this.overlay);
  160.   }
  161.   this.init = function() {
  162.     var self = this;
  163.     BranchItem.getItemHeader(this.parent).appendChild(this.div);
  164.     this.div.style.display = 'inline-block';
  165.     this.div.className = "userControl";
  166.    
  167.     this.div.appendChild(this.toggleBox);  
  168.     this.div.appendChild(this.div1);
  169.    
  170.     this.toggleBox.text = "[⨪]";
  171.     this.toggleBox.style.color = "grey";
  172.     this.toggleBox.className = "messageHide";
  173.     this.toggleBox.title = "Hide/Show Message";
  174.     this.toggleBox.style.textDecoration = 'none';
  175.     this.toggleBox.style.cursor = 'default';
  176.     this.toggleBox.style.paddingLeft = '0px';
  177.     this.toggleBox.style.paddingRight = '3px';
  178.     this.toggleBox.onclick = function() {
  179.       console.log("Hide/Show message event");
  180.       var b = BranchItem.getItemBody(self.parent);
  181.       if (b.style.display == 'none') {
  182.         b.style.display = 'block';
  183.         self.overlay.style.display = 'none';
  184.       } else {
  185.         b.style.display = 'none';
  186.         self.overlay.style.display = 'block';
  187.       }
  188.     }
  189.     this.div1.style.display = 'inline-block';
  190.     this.div1.style.borderRadius = "4px";
  191.     this.div1.style.paddingTop = "2px";
  192.    
  193.     var label = document.createElement("a");
  194.     label.text = "⪢";
  195.     label.style.color = "grey";
  196.     label.style.textDecoration = 'none';
  197.     label.style.cursor = 'default';
  198.     label.onclick = function() { return false; }
  199.     this.div1.appendChild(label);
  200.    
  201.     this.label.innerHTML = "Block User";
  202.     this.label.for = "userCheckbox" + this.mid;
  203.     this.label.style.display = 'none';
  204.     this.label.style.paddingLeft = "2px";
  205.     this.div1.appendChild(this.label);
  206.    
  207.     this.checkbox.title = "Block User";
  208.     this.checkbox.type = 'checkbox';
  209.     this.checkbox.className = "userCheckbox";
  210.     this.checkbox.id = "userCheckbox" +  this.mid;
  211.     this.checkbox.style.top = '-1px';
  212.     this.checkbox.style.position = 'relative';
  213.     this.checkbox.style.verticalAlign = 'middle';
  214.     this.checkbox.checked = !!this.userManager.find(this.id);
  215.     this.checkbox.style.display = 'none'
  216.     this.checkbox.onclick = function() {
  217.       if (self.checkbox.checked) {
  218.         self.userManager.addUser(self.id);
  219.       } else {
  220.         self.userManager.removeUser(self.id);
  221.       }
  222.     }
  223.     this.div1.appendChild(this.checkbox);
  224.    
  225.     this.vline.innerHTML = "|";
  226.     this.vline.style.font = '16px';
  227.     this.vline.style.color = 'grey';
  228.     this.vline.style.padding = "1px";
  229.     this.vline.style.top = '1px';
  230.     this.vline.style.position = 'relative';
  231.     this.vline.style.verticalAlign = 'top';
  232.     this.vline.style.display = 'none';
  233.     this.div1.appendChild(this.vline);
  234.    
  235.     this.optButton.innerHTML = '⚙';
  236.     this.optButton.style.backgroundImage = 'linear-gradient(to bottom, #fafafa, #ebf1f5)';
  237.     this.optButton.style.verticalAlign = 'middle';
  238.     this.optButton.style.top = '-1px';
  239.     this.optButton.style.paddingLeft = '2px';
  240.     this.optButton.style.paddingRight = '2px';
  241.     this.optButton.style.position = 'relative';
  242.     this.optButton.style.margin = '2px';
  243.     this.optButton.style.display = 'none';
  244.     this.optButton.onmouseover = function() {
  245.       self.optButton.style.backgroundImage = 'linear-gradient(to bottom, #e8edf0, #caced1)';
  246.     }
  247.     this.optButton.onmouseout = function() {
  248.       self.optButton.style.backgroundImage = 'linear-gradient(to bottom, #fafafa, #ebf1f5)';
  249.     }
  250.     this.optButton.onclick = function(event) {
  251.       self.showOptions(event);
  252.     }
  253.     this.div1.appendChild(this.optButton);
  254.    
  255.     this.div1.onmouseover = function() {
  256.       self.show();
  257.     }
  258.     this.div1.onmouseout = function() {
  259.       self.hide();
  260.     }
  261.    
  262.     //setTimeout(this.createOverlay.bind(this), 600);
  263.     this.createOverlay();
  264.     // Options
  265.     /*this.input.type = 'text';
  266.     this.input.width = 80;
  267.     this.input.value = this.userManager.getJSON();
  268.     this.options.appendChild(this.input);
  269.    
  270.     this.options.style.textDecorations = 'none';
  271.     this.options.style.color = 'grey';
  272.     this.options.title = "Import";
  273.     this.options.appendChild(this.import);
  274.    
  275.     this.options.appendChild(this.export);*/
  276.   }
  277.   this.init();
  278. }
  279. UserControl.updateOverlays = function() {
  280.   console.log("Updating overlays");
  281.   var ol = document.querySelectorAll(".branch-head");
  282.   for (var b of ol) {
  283.     var overlay = BranchItem.getOverlay(b);
  284.     var userControl = BranchItem.getUserControl(b);
  285.     var firstElement = b.querySelector("a");
  286.     var width = userControl.offsetLeft - firstElement.offsetLeft;
  287.     overlay.style.width = width + "px";
  288.   }
  289.   console.log(ol.length + " overlays updated");
  290. }
  291.  
  292. function UserManager() {
  293.   this.users = {};
  294.   this.length = 0;
  295.   this.obs = null;
  296.  
  297.   this.init = function() {
  298.     console.log("Initializing UserManager...");
  299.     this.loadFromStorage();
  300.     this.register();
  301.     this.refresh();
  302.     setTimeout(UserControl.updateOverlays, 2000);
  303.     console.log("UserManager initialization complete");
  304.     console.log("==================================================");
  305.   }
  306.   this.register = function() {
  307.     console.log("Registering...");
  308.     var self = this;
  309.     //r target = document.getElementById("contentMainAll");
  310.     var target = document.querySelector("div");
  311.     var conf = { childList: true, subtree: true };    
  312.     this.obs = new MutationObserver(function(mutations) {
  313.       //console.log(mutations.length);
  314.       var nodes = [];
  315.       var targetNode = null;
  316.       for (var i = mutations.length - 1; i >= 0; --i) {
  317.         var mutation = mutations[i];
  318.         //console.log(mutation.type, mutation.target, mutation.addedNodes.length);
  319.         //Array.prototype.map.call(mutation.addedNodes, function (node) {
  320.         //   console.log(node);
  321.         //});
  322.         if (mutation.target.className == 'active_new_topic') {
  323.           setTimeout(self.refresh.bind(self), 500);
  324.           return;
  325.         }
  326.         if (mutation.target.className == 'branch-cont' &&
  327.             mutation.addedNodes.length > 0) {
  328.           nodes = Array.prototype.filter.call(mutation.target.querySelectorAll(".branch-item"),
  329.                                               function(node) {
  330.                                                 return !BranchItem.getMessageHideControl(node);
  331.                                               });
  332.           break;
  333.         }
  334.       }
  335.       console.log("DOM Changed. " + mutations.length + " mutations, " + nodes.length + " branch-item nodes");
  336.       self.refresh(nodes);
  337.       self.observe(target, conf);
  338.     });
  339.     this.obs.observe(target, conf);
  340.   }
  341.   this.loadFromArray = function(a) {
  342.     console.log("Loading ids from array");
  343.     // a - is the array of User Ids
  344.     var self = this;
  345.     a.forEach(function(id) {
  346.       if (!(id in self.users)) {
  347.         self.users[id] = new User(id);
  348.         ++self.length;
  349.       }
  350.     });
  351.     console.log("Loaded " + a.length + " records");
  352.   }
  353.   this.loadFromStorage = function() {
  354.     console.log("Loading from storage...");
  355.     var newIds = this.importJSON(localStorage.getItem("nnru_blacklist"));
  356.     console.log(newIds.length + " records loaded");
  357.   }
  358.   this.saveToStorage = function() {
  359.     console.log("Saving to storage...")
  360.     localStorage.setItem("nnru_blacklist", this.getJSON());
  361.     console.log("Records saved");
  362.   }
  363.   this.clearStorage = function() {
  364.     localStorage.setItem("nnru_blacklist", "[]");
  365.   }
  366.   this.find = function(id) {
  367.     if (id in this.users) {
  368.       return this.users[id];
  369.     }
  370.     return undefined;
  371.   }
  372.   this.getJSON = function() {
  373.     var a = [];
  374.     for (var id in this.users) {
  375.       //console.log(this.users[id].data(), id != 0);
  376.       if (id != "0") a.push(this.users[id].data());
  377.     }
  378.     console.log(JSON.stringify(a));
  379.     return JSON.stringify(a);
  380.   }
  381.   this.importJSON = function(json) {
  382.     var a = JSON.parse(json);
  383.     var newIds = [];
  384.     if (a) {
  385.       console.log("Reading records: " +json);
  386.       for (var user of a) {
  387.         if (user.id != "0") {
  388.           if (!(user.id in this.users)) {
  389.             this.users[user.id] = new User();
  390.             newIds.push(this.users[user.id]);
  391.             ++this.length;
  392.           }
  393.           //console.log(user);
  394.           this.users[user.id].init(user);
  395.           //console.log(this.users[user.id]);
  396.         }
  397.       }
  398.     }
  399.     return newIds;
  400.   }
  401.   this.addUser = function(id) {
  402.     console.log("Adding User " + id)
  403.     if (!(id in this.users)) {
  404.       var user = new User(id);
  405.       this.users[id] = user;
  406.       ++this.length;
  407.       var nodes = Array.prototype.map.call(document.querySelectorAll("a[usid='" + id + "']"), function(a) {
  408.         return BranchItem.getContainer(a);
  409.       });
  410.       this.update(nodes);
  411.       this.saveToStorage();
  412.     }
  413.   }
  414.   this.removeUser = function(id) {
  415.     console.log("Removing User " + id)
  416.     var user = this.find(id);
  417.     if (user) {
  418.       var nodes = Array.prototype.forEach.call(document.querySelectorAll("a[usid='" + id + "']"), function(a) {
  419.         user.show(BranchItem.getContainer(a), false);
  420.       });
  421.       delete this.users[id];
  422.       --this.length;
  423.       this.saveToStorage();
  424.     }
  425.   }
  426.   this.refresh = function(nodes = null) {
  427.     console.log("Refreshing...");
  428.     this.augment(nodes);
  429.     this.update(nodes);
  430.   }
  431.   this.augment = function(nodes = null) {
  432.     console.log("Augmenting nodes");
  433.     if (!nodes) nodes = document.querySelectorAll(".branch-item");
  434.     for (var node of nodes) {
  435.       var userControl = new UserControl(node, this);
  436.     }
  437.     console.log(nodes.length + " nodes augmented");
  438.   }
  439.   this.update = function(nodes = null) {
  440.     console.log("Updating nodes");
  441.     var nodesAffected = 0;
  442.     if (!nodes) nodes = document.querySelectorAll(".branch-item");
  443.     for (var node of nodes) {
  444.       var id = BranchItem.getId(node);
  445.       var user = this.find(id);
  446.       if (user) {
  447.         user.hide(node);
  448.         nodesAffected++;
  449.       }
  450.     }
  451.     console.log(nodes.length + " nodes checked, " + nodesAffected + " nodes from blacklist");
  452.   }
  453. }
  454.  
  455. var userManager = new UserManager();
  456.  
  457. function onLoad() {
  458.   console.log("UserHide init...");
  459.   userManager.init();
  460. }
  461.  
  462. onLoad();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement