Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name LOR Backwash
- // @description Превью и список ответов для комментариев.
- // @author Kalashnikov Ilya
- // @license CC NC SA
- // @version 0.1.0
- // @namespace http://www.linux.org.ru/forum/*
- // @namespace https://www.linux.org.ru/forum/*
- // @include http://www.linux.org.ru/forum/*
- // @include https://www.linux.org.ru/forum/*
- // ==/UserScript==
- (function() {
- if (!document.getElementsByClassName('comment')[0])
- return;
- // CSS
- var style = document.createElement('style');
- style.innerHTML = 'div#commentPreview {\
- display:none;\
- position:fixed;\
- }\
- div#commentPreview article {\
- border:red 1px solid;\
- box-shadow: 0 0 10px black;\
- }';
- document.getElementsByTagName('head')[0].appendChild(style);
- var preview = document.createElement('div');
- preview.id = 'commentPreview';
- document.getElementsByClassName('comment')[0].appendChild(preview); // Втыкаем сюда чтоб применялись все стили для комментов
- function commentPreview(id) { // Генерирует попап
- var comment = document.getElementById('comment-' + id);
- if (!comment)
- return;
- comment = comment.cloneNode(true);
- comment.removeChild( comment.children[0] ); // Удаляем заголовок
- var msg = comment.getElementsByClassName('msg_body')[0];
- msg.removeChild( msg.getElementsByClassName('reply')[0] ); // и [Ответить на Это сообщение]
- preview.innerHTML = '';
- preview.appendChild(comment);
- };
- function showPreview(id) { // Создает обработчик для конкрентного ид // Вообще, если покопаться в event.target, ид можно и оттуда выцепить
- return function(event) {
- commentPreview(id);
- preview.style.left = event.x + 20 + 'px';
- preview.style.top = event.y + 'px';
- preview.style.display = 'block';
- }
- };
- function hidePreview() {
- preview.style.display = 'none';
- };
- var links = document.querySelectorAll('.comment .msg .title a[onclick]'); // Костыльно, но хз как лучше
- for (var i =0; i<links.length; i++) {
- var container = links[i].parentNode.parentNode;
- var originalId = links[i].href.match(/.*-(\d+)/)[1]; // Комментарий на который мы нашли ответ
- var original = document.getElementById('comment-'+originalId)
- if (!original)
- continue; // Если он не на текущей странице
- var repliesList = original.getElementsByClassName('title')[0].getElementsByClassName('repliesList')[0];
- if (!repliesList) {
- repliesList = document.createElement('span');
- repliesList.className = 'repliesList';
- repliesList.innerText = 'Ответы: ';
- original.getElementsByClassName('title')[0].appendChild(repliesList);
- }
- var a = document.createElement('a');
- a.href = links[i].href.replace(/#comment-\d+$/, '#'+container.id); // линк на текущий комментарий
- var h2 = container.getElementsByClassName('msg_body')[0].getElementsByTagName('h2')[0];
- repliesList.appendChild(document.createTextNode(" ["));
- if (h2)
- a.innerText = h2.innerText;
- else
- a.innerText = 'от ' + container.getElementsByClassName('msg_body')[0].getElementsByClassName('sign')[0].getElementsByTagName('a')[0].text;
- repliesList.appendChild(a);
- repliesList.appendChild(document.createTextNode("]"));
- a.addEventListener('mouseover', showPreview( container.id.match(/comment-(\d+)/)[1] ));
- a.addEventListener('mouseout', hidePreview);
- links[i].addEventListener('mouseover', showPreview(originalId));
- links[i].addEventListener('mouseout', hidePreview)
- }
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement