comment-answers.lor.user.js
changeset 1 0e20cd7b95c5
parent 0 652ce52f9c44
child 2 8fdf72a91e6b
equal deleted inserted replaced
0:652ce52f9c44 1:0e20cd7b95c5
     1 // ==UserScript==
     1 // ==UserScript==
     2 // @name comment-answers
     2 // @name LOR comment-answers
     3 // @description Ответы на комментарии 
     3 // @description Ответы на комментарии для linux.org.ru. Отображаются только те ответы, которые есть на текущей странице.
     4 // @author indvd00m <gotoindvdum [at] gmail [dot] com>
     4 // @author indvd00m <gotoindvdum [at] gmail [dot] com>
     5 // @license GPL
     5 // @license Creative Commons Attribution 3.0 Unported
     6 // @version 0.1
     6 // @version 0.2
     7 // @namespace http://www.linux.org.ru/*
     7 // @namespace http://www.linux.org.ru/*
     8 // @namespace https://www.linux.org.ru/*
     8 // @namespace https://www.linux.org.ru/*
     9 // @include http://www.linux.org.ru/*
     9 // @include http://www.linux.org.ru/*
    10 // @include https://www.linux.org.ru/*
    10 // @include https://www.linux.org.ru/*
    11 // ==/UserScript==
    11 // ==/UserScript==
    12 // [1] Оборачиваем скрипт в замыкание, для кроссбраузерности (opera, ie)
    12 
    13 (function (window, undefined) {  // [2] нормализуем window
    13 // running js-code in a page context 
       
    14 var execute = function (body) {
       
    15 	if(typeof body === "function") { body = "(" + body + ")();"; }
       
    16 	var el = document.createElement("script");
       
    17 	el.textContent = body;
       
    18 	document.body.appendChild(el);
       
    19 	return el;
       
    20 };
       
    21 
       
    22 (function (window, undefined) {
    14     var w;
    23     var w;
    15     if (typeof unsafeWindow != undefined) {
    24     if (typeof unsafeWindow != undefined) {
    16         w = unsafeWindow
    25         w = unsafeWindow
    17     } else {
    26     } else {
    18         w = window;
    27         w = window;
    19     }
    28     }
    20     // В юзерскрипты можно вставлять практически любые javascript-библиотеки.
       
    21     // Код библиотеки копируется прямо в юзерскрипт.
       
    22     // При подключении библиотеки нужно передать w в качестве параметра окна window
       
    23     // Пример: подключение jquery.min.js
       
    24     // (function(a,b){function ci(a) ... a.jQuery=a.$=d})(w);
       
    25 
    29 
    26     // [3] не запускаем скрипт во фреймах
       
    27     // без этого условия скрипт будет запускаться несколько раз на странице с фреймами
       
    28     if (w.self != w.top) {
    30     if (w.self != w.top) {
    29         return;
    31         return;
    30     }
    32     }
    31     // [4] дополнительная проверка наряду с @include
    33 
    32     if (/https?:\/\/(www\.)?linux.org.ru/.test(w.location.href)) {
    34     if (/https?:\/\/(www\.)?linux.org.ru/.test(w.location.href)) {
    33         //Ниже идёт непосредственно код скрипта
    35 
    34         alert("Userscripts приветствует вас навязчивым окном.");
    36 		execute(function() {
       
    37 
       
    38 			var url = $(location).attr("href").replace(/#.*$/, "");
       
    39 
       
    40 			$(".title").has("a[data-samepage='samePage']").each(function(index) {
       
    41 
       
    42 				var replyUrl = $("a", $(this)).prop("href");
       
    43 				var replyMsgId = replyUrl.match(/.*[\?\&]?cid=(\d+).*/)[1];
       
    44 
       
    45 				var nick = $("a[itemprop='creator']", $(this).next()).text();
       
    46 				var msgId = $(this).parent().prop("id").match(/comment-(\d+)/)[1];
       
    47 				var anchorName = "anchor-msg-" + msgId;
       
    48 				var anchor = $("<a name='" + anchorName + "'></a>");
       
    49 				$(this).prepend(anchor);
       
    50 
       
    51 				$("#comment-" + replyMsgId).each(function() {
       
    52 
       
    53 					var href = url + "#" + anchorName;
       
    54 					var link = $("<a href='" + href + "'>" + nick + "</a>");
       
    55 
       
    56 					var container = $(".msg_body", $(this));
       
    57 					var answersClass = "answers";
       
    58 					var answers = $("." + answersClass, container);
       
    59 					if (!answers.length) {
       
    60 						answers = $("<div class='" + answersClass + "'>Ответы: </div>");
       
    61 						answers.css("font-size", "smaller");
       
    62 						container.append(answers);
       
    63 					}
       
    64 					if (answers.children().length) {
       
    65 						answers.append(", ");
       
    66 					}
       
    67 					answers.append(link);
       
    68 				});
       
    69 			});
       
    70 		});
       
    71 
    35     }
    72     }
    36 })(window);
    73 })(window);
       
    74