comment-answers.lor.user.js
author indvd00m (gotoindvdum[at]gmail[dot]com)
Fri, 20 Sep 2013 14:49:04 +0400
changeset 2 8fdf72a91e6b
parent 1 0e20cd7b95c5
permissions -rw-r--r--
Opera fix
     1 // ==UserScript==
     2 // @name LOR comment-answers
     3 // @description Ответы на комментарии для linux.org.ru. Отображаются только те ответы, которые есть на текущей странице.
     4 // @author indvd00m <gotoindvdum [at] gmail [dot] com>
     5 // @license Creative Commons Attribution 3.0 Unported
     6 // @version 0.3
     7 // @namespace http://www.linux.org.ru/*
     8 // @namespace https://www.linux.org.ru/*
     9 // @include http://www.linux.org.ru/*
    10 // @include https://www.linux.org.ru/*
    11 // ==/UserScript==
    12 
    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) {
    23     var w;
    24     if (typeof unsafeWindow != "undefined") {
    25         w = unsafeWindow;
    26     } else {
    27         w = window;
    28     }
    29 
    30     if (w.self != w.top) {
    31         return;
    32     }
    33 
    34     if (/https?:\/\/(www\.)?linux.org.ru/.test(w.location.href)) {
    35 
    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 
    72     }
    73 })(window);
    74