Version 0.2 - first implementation
authorindvd00m (gotoindvdum[at]gmail[dot]com)
Fri, 20 Sep 2013 14:37:18 +0400
changeset 10e20cd7b95c5
parent 0 652ce52f9c44
child 2 8fdf72a91e6b
Version 0.2 - first implementation
comment-answers.lor.user.js
     1.1 --- a/comment-answers.lor.user.js	Fri Sep 20 11:15:45 2013 +0400
     1.2 +++ b/comment-answers.lor.user.js	Fri Sep 20 14:37:18 2013 +0400
     1.3 @@ -1,36 +1,74 @@
     1.4  // ==UserScript==
     1.5 -// @name comment-answers
     1.6 -// @description Ответы на комментарии 
     1.7 +// @name LOR comment-answers
     1.8 +// @description Ответы на комментарии для linux.org.ru. Отображаются только те ответы, которые есть на текущей странице.
     1.9  // @author indvd00m <gotoindvdum [at] gmail [dot] com>
    1.10 -// @license GPL
    1.11 -// @version 0.1
    1.12 +// @license Creative Commons Attribution 3.0 Unported
    1.13 +// @version 0.2
    1.14  // @namespace http://www.linux.org.ru/*
    1.15  // @namespace https://www.linux.org.ru/*
    1.16  // @include http://www.linux.org.ru/*
    1.17  // @include https://www.linux.org.ru/*
    1.18  // ==/UserScript==
    1.19 -// [1] Оборачиваем скрипт в замыкание, для кроссбраузерности (opera, ie)
    1.20 -(function (window, undefined) {  // [2] нормализуем window
    1.21 +
    1.22 +// running js-code in a page context 
    1.23 +var execute = function (body) {
    1.24 +	if(typeof body === "function") { body = "(" + body + ")();"; }
    1.25 +	var el = document.createElement("script");
    1.26 +	el.textContent = body;
    1.27 +	document.body.appendChild(el);
    1.28 +	return el;
    1.29 +};
    1.30 +
    1.31 +(function (window, undefined) {
    1.32      var w;
    1.33      if (typeof unsafeWindow != undefined) {
    1.34          w = unsafeWindow
    1.35      } else {
    1.36          w = window;
    1.37      }
    1.38 -    // В юзерскрипты можно вставлять практически любые javascript-библиотеки.
    1.39 -    // Код библиотеки копируется прямо в юзерскрипт.
    1.40 -    // При подключении библиотеки нужно передать w в качестве параметра окна window
    1.41 -    // Пример: подключение jquery.min.js
    1.42 -    // (function(a,b){function ci(a) ... a.jQuery=a.$=d})(w);
    1.43  
    1.44 -    // [3] не запускаем скрипт во фреймах
    1.45 -    // без этого условия скрипт будет запускаться несколько раз на странице с фреймами
    1.46      if (w.self != w.top) {
    1.47          return;
    1.48      }
    1.49 -    // [4] дополнительная проверка наряду с @include
    1.50 +
    1.51      if (/https?:\/\/(www\.)?linux.org.ru/.test(w.location.href)) {
    1.52 -        //Ниже идёт непосредственно код скрипта
    1.53 -        alert("Userscripts приветствует вас навязчивым окном.");
    1.54 +
    1.55 +		execute(function() {
    1.56 +
    1.57 +			var url = $(location).attr("href").replace(/#.*$/, "");
    1.58 +
    1.59 +			$(".title").has("a[data-samepage='samePage']").each(function(index) {
    1.60 +
    1.61 +				var replyUrl = $("a", $(this)).prop("href");
    1.62 +				var replyMsgId = replyUrl.match(/.*[\?\&]?cid=(\d+).*/)[1];
    1.63 +
    1.64 +				var nick = $("a[itemprop='creator']", $(this).next()).text();
    1.65 +				var msgId = $(this).parent().prop("id").match(/comment-(\d+)/)[1];
    1.66 +				var anchorName = "anchor-msg-" + msgId;
    1.67 +				var anchor = $("<a name='" + anchorName + "'></a>");
    1.68 +				$(this).prepend(anchor);
    1.69 +
    1.70 +				$("#comment-" + replyMsgId).each(function() {
    1.71 +
    1.72 +					var href = url + "#" + anchorName;
    1.73 +					var link = $("<a href='" + href + "'>" + nick + "</a>");
    1.74 +
    1.75 +					var container = $(".msg_body", $(this));
    1.76 +					var answersClass = "answers";
    1.77 +					var answers = $("." + answersClass, container);
    1.78 +					if (!answers.length) {
    1.79 +						answers = $("<div class='" + answersClass + "'>Ответы: </div>");
    1.80 +						answers.css("font-size", "smaller");
    1.81 +						container.append(answers);
    1.82 +					}
    1.83 +					if (answers.children().length) {
    1.84 +						answers.append(", ");
    1.85 +					}
    1.86 +					answers.append(link);
    1.87 +				});
    1.88 +			});
    1.89 +		});
    1.90 +
    1.91      }
    1.92  })(window);
    1.93 +