v2exBetterReply

better reply experience for v2ex

目前為 2016-08-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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"));
}