Advertisement
overloop

bnw-chan.js

Sep 5th, 2014
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        bnw-chan
  3. // @namespace   mugiseyebrows.ru
  4. // @description наводишь на ссылку и тебе прямо тама (как на бордах)
  5. // @include     http://meow.bnw.im/p/*
  6. // @include     https://meow.bnw.im/p/*
  7. // @version     1
  8. // @grant       none
  9. // ==/UserScript==
  10.  
  11. var relation = [];
  12. function traverse(parentId,comment) {
  13.     $(comment).children('div').each(function(i,item){
  14.         if ($(item).hasClass('comment')) {
  15.             var childId = $(item).attr('id');
  16.             relation[childId] = parentId;
  17.             parentId = childId;
  18.         } else {
  19.             traverse(parentId,item);
  20.         }
  21.     });
  22. }
  23.  
  24. function postReplies(item) {
  25.     var link = $(item).find('a.comment-id');
  26.     var id = '';
  27.     if (link.length>0) {
  28.         id = link.attr('href').replace('#','');
  29.     }
  30.     var links = [];
  31.     for (var child in relation) {
  32.         if (relation[child] == id) {
  33.             links.push('<a class="comment-reply-to label label-reset" href="#' + child + '">&gt;&gt;' + child + '</a>');
  34.         }
  35.     }
  36.     if (links.length > 0) {
  37.         var target = 'div.comment-header';
  38.         if (id == '') {
  39.             target = 'div.post-header';
  40.         }
  41.         $(item).find(target).append('<div class="bnw-chan-links">'+links.join(' ')+'</div>');
  42.     }
  43. }
  44.  
  45. function putReplyLinks() {
  46.     if ($('#single-post').length == 0 || $('#comments').length == 0) { // если не подгрузилось ещё
  47.         setTimeout(putReplyLinks,100);
  48.         return;
  49.     }
  50.     //запускаем голубка
  51.     $('#comments').children('div').each(function(i,item) {
  52.         traverse('',item);
  53.     });
  54.     $('div.comment').each(function(i,item) {
  55.         postReplies(item);
  56.     });
  57.     $('#single-post').each(function(i,item) {
  58.         postReplies(item);
  59.     });
  60. }
  61.  
  62. var chainIndex = 0;
  63. function bindLinkOnHover() {
  64.     $('body').on('hover','a.comment-reply-to',function(e){
  65.         if (e.type == 'mouseenter') {
  66.             var commentId = $(this).attr('href');
  67.             // да
  68.             var xPadding = 40 + 10;
  69.             var yPadding = 20 + 10;
  70.             var xPos = e.clientX + window.scrollX - xPadding;
  71.             var yPos = e.clientY + window.scrollY - yPadding;
  72.             var windowWidth = $(window).width();
  73.             var commentWidth = $(commentId).width();
  74.  
  75.             // чтоб за границу окна не уходило
  76.             xPos = Math.min(xPos + commentWidth + xPadding, windowWidth) - commentWidth - xPadding;
  77.  
  78.             var comment = $(commentId).html();
  79.             var hoverId = 'bnw-chan-comment-' + chainIndex;
  80.             chainIndex = chainIndex + 1;
  81.             $('body').append('<div id="' + hoverId + '" class="comment-wrapper comment well well-small bnw-chan-comment" style="position: absolute;">' + comment + '</div>');
  82.             $('#' + hoverId).css('left',xPos).css('top',yPos).css('width',commentWidth);
  83.         }
  84.     });
  85. }
  86.  
  87. var chain = [];
  88. var focused = null;
  89. function bindCommentOnHover() {
  90.     $('body').on('hover','div.bnw-chan-comment',function(e){
  91.     var id = $(this).attr('id');
  92.     if (e.type == 'mouseenter') {
  93.         focused = id;
  94.         var index = chain.indexOf(id);
  95.         if (index>-1) {
  96.             for (var i=chain.length-1;i>index;i--) {
  97.                 $('#' + chain[i]).remove();
  98.             }
  99.             chain = chain.slice(0,index+1);
  100.         } else {
  101.             chain.push(id);
  102.         }
  103.     } else if (e.type == 'mouseleave'){
  104.         focused = null;
  105.         setTimeout(function(){
  106.             if (focused == null) {
  107.                 for (var i=chain.length-1;i>=0;i--) {
  108.                     $('#' + chain[i]).remove();
  109.                 }
  110.                 chain = [];
  111.             }
  112.         },20);
  113.     }
  114.     });
  115. }
  116.  
  117. $(document).ready(function(){
  118.     putReplyLinks();
  119.     bindLinkOnHover();
  120.     bindCommentOnHover();
  121. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement