Advertisement
Kalashnikov

LOR Backwash

Mar 29th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name LOR Backwash
  3. // @description Превью и список ответов для комментариев.
  4. // @author Kalashnikov Ilya
  5. // @license CC NC SA
  6. // @version 0.1.0
  7. // @namespace http://www.linux.org.ru/forum/*
  8. // @namespace https://www.linux.org.ru/forum/*
  9. // @include http://www.linux.org.ru/forum/*
  10. // @include https://www.linux.org.ru/forum/*
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.     if (!document.getElementsByClassName('comment')[0])
  15.         return;
  16.  
  17.     // CSS
  18.     var style = document.createElement('style');
  19.     style.innerHTML = 'div#commentPreview {\
  20.         display:none;\
  21.         position:fixed;\
  22.     }\
  23.     div#commentPreview article {\
  24.         border:red 1px solid;\
  25.         box-shadow: 0 0 10px black;\
  26.     }';
  27.     document.getElementsByTagName('head')[0].appendChild(style);
  28.  
  29.  
  30.     var preview = document.createElement('div');
  31.     preview.id = 'commentPreview';
  32.     document.getElementsByClassName('comment')[0].appendChild(preview); // Втыкаем сюда чтоб применялись все стили для комментов
  33.  
  34.  
  35.     function commentPreview(id) {   // Генерирует попап
  36.         var comment = document.getElementById('comment-' + id);
  37.         if (!comment)
  38.             return;
  39.         comment = comment.cloneNode(true);
  40.         comment.removeChild( comment.children[0] ); // Удаляем заголовок
  41.         var msg = comment.getElementsByClassName('msg_body')[0];
  42.         msg.removeChild( msg.getElementsByClassName('reply')[0] ); // и [Ответить на Это сообщение]
  43.         preview.innerHTML = '';
  44.         preview.appendChild(comment);
  45.     };
  46.  
  47.     function showPreview(id) {  // Создает обработчик для конкрентного ид // Вообще, если покопаться в event.target, ид можно и оттуда выцепить
  48.         return function(event) {
  49.             commentPreview(id);
  50.             preview.style.left = event.x + 20 + 'px';
  51.             preview.style.top  = event.y + 'px';
  52.             preview.style.display = 'block';
  53.         }
  54.     };
  55.  
  56.     function hidePreview() {
  57.         preview.style.display = 'none';
  58.     };
  59.  
  60.     var links = document.querySelectorAll('.comment .msg .title a[onclick]');   // Костыльно, но хз как лучше
  61.     for (var i =0; i<links.length; i++) {
  62.         var container = links[i].parentNode.parentNode;
  63.  
  64.         var originalId = links[i].href.match(/.*-(\d+)/)[1];    // Комментарий на который мы нашли ответ
  65.         var original = document.getElementById('comment-'+originalId)
  66.  
  67.         if (!original)
  68.             continue; // Если он не на текущей странице
  69.        
  70.         var repliesList = original.getElementsByClassName('title')[0].getElementsByClassName('repliesList')[0];
  71.         if (!repliesList) {
  72.             repliesList = document.createElement('span');
  73.             repliesList.className = 'repliesList';
  74.             repliesList.innerText = 'Ответы: ';
  75.             original.getElementsByClassName('title')[0].appendChild(repliesList);
  76.         }
  77.  
  78.         var a = document.createElement('a');
  79.         a.href = links[i].href.replace(/#comment-\d+$/, '#'+container.id);  // линк на текущий комментарий
  80.  
  81.         var h2 = container.getElementsByClassName('msg_body')[0].getElementsByTagName('h2')[0];
  82.        
  83.         repliesList.appendChild(document.createTextNode(" ["));
  84.         if (h2)
  85.             a.innerText = h2.innerText;
  86.         else
  87.             a.innerText = 'от ' + container.getElementsByClassName('msg_body')[0].getElementsByClassName('sign')[0].getElementsByTagName('a')[0].text;
  88.         repliesList.appendChild(a);
  89.         repliesList.appendChild(document.createTextNode("]"));
  90.  
  91.         a.addEventListener('mouseover', showPreview( container.id.match(/comment-(\d+)/)[1] ));
  92.         a.addEventListener('mouseout',  hidePreview);
  93.         links[i].addEventListener('mouseover', showPreview(originalId));
  94.         links[i].addEventListener('mouseout',  hidePreview)
  95.     }
  96. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement