Skip to main content
2 of 2
v1.1c
brasofilo
  • 1k
  • 8
  • 22

I've made a couple of fixes to make this work again. While I don't fork and ask for a merge, here it is a copy of my script for convenience, the modifications are indicated with comments. The matches of sites has been revised as well.

// ==UserScript==
// @name           stackoverflow-comment-reply
// @namespace      stackapps
// @description    Add "reply" links to comments
// @version        1.1c
// @match          *://*.askubuntu.com/*
// @match          *://*.mathoverflow.net/*
// @match          *://*.serverfault.com/*
// @match          *://*.stackapps.com/*
// @match          *://*.stackexchange.com/*
// @match          *://*.stackoverflow.com/*
// @match          *://*.superuser.com/*
// @match          *://area51.stackexchange.com/proposals/*
// @author         Benjamin Dumke-von der Ehe
// ==/UserScript==

/* MOD: don't run if not SE and user not logged in */
if (!window.StackExchange || window.StackExchange.options.user.isAnonymous )
    return;

// Thanks to Shog9 for this idea for making the script work in both
// Chrome and Firefox:
// http://meta.stackoverflow.com/questions/46562
function with_jquery(f) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.textContent = "(" + f.toString() + ")(jQuery)";
    document.body.appendChild(script);
};

with_jquery(function($) {

    $(document).ajaxComplete(function(){
        setTimeout(function () {
            $(".comments").each(put_reply_links);
        }, 100);
    });

    // event handler for a click on the reply links
    function reply() {
        /* MOD: changed the following 2 lines to detect the new Add Comment link */
        var commentlinkId = $(this).closest(".comments").next().attr("id");
        var commentlink = $('#'+commentlinkId + ' a.js-add-link')[0].click();
        var username = goodify($(this).prev().text().replace(/♦/, ""))
        var formid = commentlinkId.replace(/^.*-(\d+)$/, "add-comment-$1");
        var ta = $("#" + formid + " textarea")[0];
        var start = ta.selectionStart;
        var end = ta.selectionEnd;
        var shift = username.length + 3;
        ta.value = "@" + username + ": " + ta.value;
        ta.focus();
        ta.selectionStart = start + shift;
        ta.selectionEnd = end + shift;
    };

    function put_reply_links() {
        $(this).find(".comment:not(:has(.reply-link)) .comment-user").each(function () {
            /* MOD: // changed click by on(click) */
            $("<span class='reply-link' style='cursor:pointer;opacity:0.3' title='reply'> &crarr;</span>").on('click',reply).insertAfter(this); 
        });
    }

    $(".comments").each(put_reply_links);

    function goodify(s) {
        return s.replace(/ +/g, "");
    }   
});
brasofilo
  • 1k
  • 8
  • 22