v2exBetterReply

better reply experience for v2ex

Ekde 2016/08/25. Vidu La ĝisdata versio.

// ==UserScript==
// @name        v2exBetterReply
// @namespace   v2ex.com
// @description better reply experience for v2ex
// @include     https://www.v2ex.com/t/*
// @version     1
// @grant       GM_log
// @grant       GM_addStyle
// @run-at      document-end
// @require     https://code.jquery.com/jquery-2.2.4.min.js
// ==/UserScript==

GM_addStyle(".v2exBR-reply-no-target{background-color: #AAAAAA; color: black !important; cursor: pointer; font-weight:bold;}");
GM_addStyle(".v2exBR-cited-comment-view{background-color: white; position: absolute; display: none; max-width: 500px;}");
GM_addStyle(".v2exBR-reply-citation{color: #778087; cursor: pointer;} .v2exBR-reply-citation:hover{color: #4d5256; text-decoration: underline;}");
GM_addStyle(".v2exBR-cited-comment-view .fr{display: none;}");

$(document.body).append($("<div class=\"v2exBR-cited-comment-view cell\" id=\"v2exBR_citation_div\"></div>"));

$(".no").hover(function(){
	$(this).addClass("v2exBR-reply-no-target");
}, function(){
	$(this).removeClass("v2exBR-reply-no-target");
}).click(function(){
	var username = $(this).parent().next().next().children("a").text();
	var commentNo = $(this).text();
	makeCitedReply(username, commentNo);
});

var commentCells = $("div.cell").filter(function(){
	return this.id.startsWith("r");
});
commentCells.find("div.reply_content")
	.each(function(index){
		var content = $(this).html();
		var replacementSpan = "<span class=\"v2exBR-reply-citation\" v2exBR-commentCellId=\"\">";
		content = content.replace(/&gt;&gt;\d+\s/g, replacementSpan + "$&" + "</span>");
		$(this).html(content);
		
		$("span.v2exBR-reply-citation", this).each(function(){
			var replyNo = $(this).text().match(/>>(\d+)/)[1];
			if(replyNo <= commentCells.length){
				$(this).attr("v2exBR-commentCellId", commentCells.get(replyNo - 1).id);
			}
		});
	});


$(".v2exBR-reply-citation").hover(function(){
	var self = this;
	var commentCellId = $(self).attr("v2exBR-commentCellId");
	if(commentCellId == "") return;
	console.log(commentCellId);
	var citationHTML = $("#"+commentCellId).html();

	var divPosTopOffset = window.getComputedStyle(self).getPropertyValue("font-size").match(/(\d+)px/)[1];
	console.log(divPosTopOffset);
	console.log($(self).offset().top + parseFloat(divPosTopOffset) + "px");
	$("#v2exBR_citation_div").html(citationHTML)
		.css({
			top:$(self).offset().top,
			left:$(self).offset().left + $(self).width()
		})
		.fadeIn(100);
}, function(){
	$("#v2exBR_citation_div").fadeOut(100);
});


$(".v2exBR-reply-citation").click(function(){
	var commentCellId = $(this).attr("v2exBR-commentCellId");
	if(commentCellId == "") return;
	$("html, body").animate({
		scrollTop: $("#" + commentCellId).offset().top
	}, 500);
	
});

function makeCitedReply(username, commentNo){
	replyContent = $("#reply_content");
	oldContent = replyContent.val();

	userTag = "@" + username + " ";
	commentTag = ">>" + commentNo + " \n";

	newContent = commentTag + userTag;
	if(oldContent.length > 0){
		if (oldContent != commentTag + userTag) {
			newContent = oldContent + "\n" + commentTag + userTag;
		}
	} else {
		newContent = commentTag + userTag;
	}

	replyContent.focus();
	replyContent.val(newContent);
	moveEnd($("#reply_content"));
}