Guest User

lor-2ch-style

a guest
Feb 12th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @id www.linux.org.ru-2ch-style@scriptish
  3. // @name LOR 2ch-style
  4. // @version 0.1
  5. // @author mm3 and other
  6. // @description enter something useful
  7. // @match http://www.linux.org.ru/forum/*
  8. // @match https://www.linux.org.ru/forum/*
  9. // @namespace http://www.linux.org.ru/forum/*
  10. // @namespace https://www.linux.org.ru/forum/*
  11. // @include http://www.linux.org.ru/forum/*
  12. // @include https://www.linux.org.ru/forum/*
  13. // @grant       none
  14. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js
  15. // @run-at document-end
  16. // ==/UserScript==
  17. (function () {
  18. //
  19. // Settings
  20. //
  21. var timeoutOnShow = 500;
  22. var timeoutOnHide = 300;
  23. //
  24. var locationurl = $(location).attr("href").replace(/#.*$/, "");    
  25. var locatTimer = null;
  26. var response = null;
  27. var cleanTimer = function (){
  28. if (locatTimer){
  29. clearTimeout(locatTimer);
  30. locatTimer = null;
  31. }
  32. };
  33. var processMouseOut = function (msg){
  34. cleanTimer();
  35. locatTimer = setTimeout(function(){
  36. locatTimer = null;
  37. $('.2ch_popup').remove();
  38. }, timeoutOnHide);
  39. };
  40. var processTimerOnShow = function (msg){
  41. cleanTimer();
  42. locatTimer = setTimeout(function(){
  43. locatTimer = null;
  44. msg();
  45. }, timeoutOnShow);
  46. };
  47. var getMsgById = function (msgId){
  48. return $("#comment-" + msgId);
  49. };
  50. var getMsgIdByLink = function(element)
  51. {
  52.     var msgId = null;
  53.     var href = $(element).prop("href");
  54.     var t = href.match(/comment-(\d+)/);
  55.     if(t != null)
  56.     {
  57.        var msgId = t[1];
  58.        if(msgId != null && msgId.length > 0)
  59.        {
  60.           return msgId;
  61.        }
  62.     }
  63.     t = href.match(/.*[\?\&]?cid=(\d+).*/);    
  64.     if(t != null)
  65.     {
  66.        msgId = t[1];
  67.     }
  68.     return msgId;
  69. };
  70. var processLink = function (lnk, elem)
  71. {
  72.     msg = lnk.parents('article');
  73.     showPopup(msg, elem);
  74. };
  75.  
  76. var showPopup = function (msg, elem)
  77. {
  78.     var msg_title = msg.find('.title');
  79.     var msg_body = msg.find('.msg_body.message-w-userpic');
  80.     var popup = null;
  81.  
  82.     if(msg_title.length > 0 && msg_body.length > 0)
  83.     {
  84.         popup = $('<div class="2ch_popup">' + '<div class="msg_title">' + msg_title.html() + '</div>' + '<div class="msg_body">' + msg_body.html() + '</div>' + '</div>');
  85.     }
  86.     else
  87.     {
  88.         popup = $('<div class="2ch_popup">' + msg.html() + '</div>');
  89.     }
  90.     popup.find('.hideon-phone').parent().remove();
  91.     popup.find('div.msg_title > a').each(function (idx,elem){$(elem).mouseover(function(){processTimerOnShow(function(){processMouseOver(elem);});});})
  92.     .mouseout(function(){cleanTimer();}).click(function(){cleanTimer();});
  93.     popup.find('div.answers > a').each(function (idx,elem){$(elem).mouseover(function(){processTimerOnShow(function(){showPopup(getMsgById(getMsgIdByLink(elem)), elem);});});})
  94.     .mouseout(function(){cleanTimer();}).click(function(){cleanTimer();});
  95.     popup.mouseenter(function(){cleanTimer();}).mouseleave(function(){processMouseOut();});
  96.     cleanTimer();
  97.     $('body').remove('.2ch_popup');
  98.     $(popup).css('z-index', '9999')
  99.     .css('position', 'absolute')
  100.     .css('top', ($(elem).offset().top+$(elem).height()+5)+'px')
  101.     .css('left', ($(elem).offset().left+20)+'px')
  102.     .css('background-color','#101010')
  103.     .css('width', '600px')
  104.     .css('border-radius', '5px')
  105.     .css('border-width', '1px')
  106.     .css('border-style', 'solid')
  107.     .css('border-color','#face8d')
  108.     .css('padding', '5px')
  109.     .attr('rel', $(elem).attr('href'));
  110.     $('body').append(popup);
  111. };
  112. var findLinks = function (msg, elem)
  113. {
  114.     return $(msg).find('a').filter(function(){return ($(this).text() === "Ссылка" && $(this).attr('href') == $(elem).attr('href')); });
  115.     //return document.querySelectorAll('#comment .msg_title .title a[onclick]');
  116. };
  117. var processMouseOver = function (elem)
  118. {
  119.     var links = findLinks($('#comments'), elem);
  120.     if(links.length > 0)
  121.     {
  122.     processLink(links, elem);
  123.     return false;
  124.     }
  125.     console.log('not found');
  126.     if($('.2ch_popup').length > 0 && $('.2ch_popup').attr('rel') == $(elem).attr('href'))
  127.         return false;
  128.     if(response != null)
  129.     {
  130.         links = findLinks(response,elem);
  131.         if(links.length > 0)
  132.         {
  133.             processLink(links, elem);
  134.             return false;
  135.         }
  136.     }
  137.     showPopup($('<div>loading</div>'), elem);
  138.     $.ajax({type: "GET", url: $(elem).attr('href')}).done(function(msg){response = $(msg);processLink(findLinks(msg,elem), elem);})
  139. };
  140.  
  141. var processLinkComments = function (elem, msgId)
  142. {
  143.     $(elem).mouseover(function(){processTimerOnShow(function(){showPopup(getMsgById(msgId), elem);});})
  144.     .mouseout(function(){processMouseOut();});
  145. };
  146.  
  147. var processTitle = function (element)
  148. {
  149.     $(element).mouseover(function(){processTimerOnShow(function(){processMouseOver(element);});})
  150.     .mouseout(function(){processMouseOut();}).click(function(){cleanTimer();});
  151.    
  152.     var msg = element.parents('article');    
  153.     var msgId = msg.prop("id").match(/comment-(\d+)/)[1];
  154.     var nick = $("a[itemprop='creator']", msg).text();
  155.     if (nick == null || nick == "")
  156.     nick = "anonymous";
  157.     $("#comment-" + getMsgIdByLink(element)).each(function()
  158.     {
  159.        var href = locationurl + "#comment-" + msgId;
  160.        var link = $("<a href='" + href + "'>" + nick + "</a>");          
  161.        processLinkComments(link, msgId);
  162.        var container = $(".msg_body", $(this));
  163.        var answersClass = "answers";
  164.        var answers = $("." + answersClass, container);
  165.        if (!answers.length)
  166.        {
  167.           answers = $("<div class='" + answersClass + "'>Ответы: </div>");
  168.           answers.css("font-size", "smaller");
  169.           container.append(answers);
  170.        }
  171.        if (answers.children().length)
  172.        {
  173.           answers.append(", ");
  174.        }
  175.        answers.append(link);
  176.     });
  177. };
  178.  
  179. $('div.title > a').each(function (idx,elem){processTitle($(elem));});
  180. }());
Advertisement
Add Comment
Please, Sign In to add comment