zhihu-reply-tip

显示被引用的用户的最近一条评论

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name       zhihu-reply-tip
// @namespace  https://github.com/QingYun/
// @version    0.1
// @description  显示被引用的用户的最近一条评论
 // @match      http://www.zhihu.com/*
// @copyright  2012+, QingYun
// ==/UserScript==

var HIDE_STYLE = "display: none";

function hasClass(node, className) {
	return node && node.classList && node.classList.contains(className);
}

function createReplyTipNode() {
    var a = document.createElement("a");
    a.setAttribute("href", "#");
    a.setAttribute("class", "like zm-comment-op-link");
    
    var i = document.createElement("i");
    i.setAttribute("class", "zg-icon zg-icon-comment-reply");
    
    a.appendChild(i);
    a.appendChild(document.createTextNode("查看 TA 的最近一条评论"));
    
    return a;
}

function createQuoteBlockNode(content) {
	var bq = document.createElement("blockquote");
    bq.setAttribute("style", HIDE_STYLE);
    
    var p = document.createElement("p");
    p.appendChild(document.createTextNode(content));
    
    bq.appendChild(p);
    
    return bq;
}

function latestReply(commentItems, userLookingFor, endIndex) {
    for (var i = endIndex - 1; i >= 0 ; i--) {
        var curUser = commentItems[i].querySelector(".zg-link").textContent;
        if (curUser ==  userLookingFor) {
            return commentItems[i].querySelector(".zm-comment-content").textContent;
        }
    }
}

function addOneReplyTip(commentItems, targetIndex) {
    var curItem = commentItems[targetIndex];
    if (curItem.querySelector(".zm-comment-hd .like") !== null) 
        return ;
    
    var headNode = curItem.querySelector(".zm-comment-hd");
    if (headNode.childNodes.length > 3) {
        var referredUser;
        if (headNode.firstChild.nodeValue == "\n 匿名用户")
            referredUser = headNode.querySelectorAll(".zg-link")[0];
        else
            referredUser = headNode.querySelectorAll(".zg-link")[1];
        
        if (referredUser) {
            var referredReply = latestReply(commentItems, referredUser.textContent, targetIndex);
            if (!referredReply)
                return ;
            
            var q = null;
            var replyTip = createReplyTipNode();
            replyTip.addEventListener("click", function() {
                if (q === null) {
                    q = createQuoteBlockNode(referredReply);
                    var content = curItem.querySelector(".zm-comment-content");
                    content.parentNode.insertBefore(q, content);
                }
                
                if (q.getAttribute("style") == HIDE_STYLE)
                    q.removeAttribute("style");
                else
                    q.setAttribute("style", HIDE_STYLE);
            });
            
            curItem.querySelector(".zm-comment-hd").appendChild(replyTip);
        }
    }
}

function addReplyTips(commentList) {
    var commentItems = commentList.querySelectorAll(".zm-item-comment");
    for (var i = 0; i < commentItems.length; i++) {
        addOneReplyTip(commentItems, i);
    }
}

(function(){
    var addReplyTipTimer;
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type == "childList") {
                var firstAddedNode = mutation.addedNodes.length && mutation.addedNodes[0];
                var firstRemovedNode = mutation.removedNodes.length && mutation.removedNodes[0];
                
                if (hasClass(firstAddedNode, "zm-comment-box") && hasClass(firstRemovedNode, "zm-comment-box"))
                    addReplyTips(mutation.addedNodes[0].querySelector(".zm-comment-list"));
                else if (hasClass(firstAddedNode, "zm-item-comment")) {
                	if (addReplyTipTimer) clearTimeout(addReplyTipTimer);
                    addReplyTipTimer = setTimeout(function() {
                    	addReplyTips(mutation.target);
                    }, 100);
                }
            }
        });
    });
    
    observer.observe(document.body, {
        childList: true
        , subtree: true
        , attributes: false
        , characterData: false
    });
})();