18

I just tried to link some comment from the election page only to find out there is no link on the timestamp.

Is this by design? If so: can this be reconsidered. Sometimes I want to link to comments in chat which is not easy to do now.

To make my question a bit less dupe. The comments once linked by doing heavy manual labor (looking up the id in the DOM) also don't onebox in chat.

8

3 Answers 3

6

Not yet thoroughly tested. I confirm that it supports the case of loading more comment, and it should also support posting/deleting comment.

I have made some updates since my first revision to fix some bugs.

This is currently Revision 3

// ==UserScript==
// @name           Timestamp in Election comment
// @namespace      nhahtdh
// @include        http://stackoverflow.com/election*
// @grant          metadata
// @author         nhahtdh

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 ($) {
    function addCommentLink(ref) {
        $('.comment', ref).each(function () {
            $('.comment-date > .relativetime-clean', this).wrapAll($('<a href="' + '#' + this.id + '"></a>'));
        });
    }

    addCommentLink(document);

    $(document).ajaxComplete(function (e, xhr, settings) {
        if (/\/comments/.test(settings.url)) {
            addCommentLink(document);
        }
    });
});
4

You can construct a URL that will point to a comment.

All you need is to take a look at the id of the actual comment -

http://stackoverflow.com/election/4?#comment-21205321
-------------------------------------------^

It's not too comfortable, and a little hacky... but it works never-the-less!

8
  • I know I can look it up in the DOM (that's the part about not be easy). I should have added that in my question. Sorry that I suck at this :(
    – PeeHaa
    Feb 25, 2013 at 22:52
  • 2
    I smell a userscript brewing ;)
    – Lix
    Feb 25, 2013 at 22:53
  • @PeeHaa - Nothing feels better than finding someone else has already done your work ;)
    – Lix
    Feb 25, 2013 at 23:00
  • Why are you referring to me as pee. That's very offensive! Especially bad when you are running for mod! Flagged! ;)
    – PeeHaa
    Feb 25, 2013 at 23:03
  • @PeeHaa - sorry for that.. I'm used to using the minimum 3 character @ replies...
    – Lix
    Feb 25, 2013 at 23:04
  • Also it seems that Manishearth's script doesn't work any more...
    – Lix
    Feb 25, 2013 at 23:05
  • 2
    That's ok. I forgive you :-) Would be nice if this would be "fixed" though at some point imho (the functionality I mean although a userscript could be a temporary solution).
    – PeeHaa
    Feb 25, 2013 at 23:09
  • 2
    @Lix It works, but only on the visible comments. Doesn't work on comments that show up after you click "add / show n more comments".
    – yannis
    Feb 25, 2013 at 23:24
2

Put your hands in the air like you just don't care (for such crass abstractions as jQuery)

Edit: slight improvement to expando link callback to prevent multiple calls and wait until all additional comments have been loaded

// ==UserScript==
// @name Election comment linkifier
// @description Linkify comment timestamps in SO moderator election
// @version 1.0
// @author DaveRandom
// @namespace https://github.com/DaveRandom
// @match http://stackoverflow.com/election/*
// ==/UserScript==

(function() {

  function addLinksToUnprocessedPosts() {
    var i, l, linkSpan, linkText, anchorElement,
        comments = document.querySelectorAll('tr[id^=comment-]:not(.linkmaker-processed)');

    pending = false;

    for (i = 0, l = comments.length; i < l; i++) {
      linkSpan = comments[i].querySelector('span.comment-date span');
      linkText = linkSpan.firstChild;
      linkSpan.removeChild(linkText);

      anchorElement = document.createElement('a');
      anchorElement.href = '#' + comments[i].id;
      anchorElement.appendChild(linkText);

      linkSpan.appendChild(anchorElement);

      comments[i].className += ' linkmaker-processed';
    }
  }
  addLinksToUnprocessedPosts();

  var i, l, pending = false,
      expandoLinks = document.querySelectorAll('.comments-link');
  for (i = 0, l = expandoLinks.length; i < l; i++) {
    expandoLinks[i].parentNode.querySelector('tbody').addEventListener('DOMNodeInserted', function(e) {
      if (e.srcElement.nodeType === 1 && e.srcElement.tagName.toLowerCase() === 'tr' && !pending) {
        pending = true;
        setTimeout(addLinksToUnprocessedPosts, 0);
      }
    });
  }

}());

Tested in a real browser (Google Chrome), but I suppose if you must you may sully my fine craftsmanship by using it in that other one...

5
  • 1
    Good guy Dave strikes again!
    – PeeHaa
    Feb 26, 2013 at 0:09
  • DOMNodeInserted event is deprecated, at least according to MDN
    – nhahtdh
    Feb 26, 2013 at 0:15
  • 2
    @nhahtdh Indeed it is, and for very good reasons. However the main problem is with bubbling - not too much of a problem because the node(s) I attached it to have very few child nodes and they don't really change apart from when you click the link. The alternative (mutation observers) is, however, a lot more of a pita to work with. I agreed it's not an ideal mechanism but the other option - without resorting to jQuery - was to poll the DOM for completion which is even more horrible (IMO). Side note: the way in which the SE code deals with adding the extra posts to the DOM is very inefficient. Feb 26, 2013 at 0:24
  • @DavidX.Random: +1 for a great comment. I don't code web-related stuffs much, so this is great insight.
    – nhahtdh
    Feb 26, 2013 at 0:28
  • 2
    @nhahtdh In a couple of years (hopefully) MutationObserver will be the way to do things like this but at the moment it is only supported in Chrome and Firefox, and in Chrome it uses a non-standard name (Webkit prefix). Mutation events are very inefficient on the DOM (especially in Firefox) which is why they have been deprecated but for real-world applications where cross browser support is required, i.e. not little overlays like this, unfortunately you are stuck with them at the moment. I started work on an observer abstraction with fallback to events a while ago, but it doesn't work yet. Feb 26, 2013 at 0:36

Not the answer you're looking for? Browse other questions tagged .