Metapost: Current Comments

By popular request, we finally have a comment widget on the side that will only post the most current comment per "active thread", instead of having a half dozen XYZ commented on the Twilight thread of the week. I owe much gratitude to Husband, who basically devoted his entire day to this and who does not, technically, know Javascript despite being able to code me up a widget on short notice. Husband is awesome.

Quirks of this script include:
  • The widget may display between 1 and 25 "current comments". The deciding factor here is the 25 comments contained in the RSS blog feed page that the widget is loading.
  • The widget may display less than 25 comments. The deciding factor here is comment density: if the last 25 comments on the blog were on a Twilight post, only 1 comment will show. 
  • The widget displays a piece of the web address title, and not the actual blogger title. This is a quirk of the RSS blog feed page and cannot be changed no matter how much it bugs me. 
  • The widget guts were recycled from a snipped posted by the ReviewOfWeb folks, credited in the widget, who are quite clearly also awesome.

For those of you who use Blogger -- and I think this widget will only work with Blogger feeds, but I could be wrong -- here is the final code. Feel free to reuse.

<script style="text/javascript">
  // Global array variable to control for post uniqueness.
  arr = new Array(25);

  // Array function. Returns true when post already exists.
  function ArrayContains(arr, title)
    for (index = 0; index < arr.length; index++)
      if (arr[index] == title)
        return true;
    return false;

  // Comment function.
  function showrecentcomments(json)
    // Set to 25 to process full feed.
    var numcomments = 25;

    // Increment through each feed item.
    for (var i = 0; i < numcomments; i++)
      var entry = json.feed.entry[i];
      var alturl;
      if (i == json.feed.entry.length) break;
      for (var k = 0; k <; k++)
        if ([k].rel == 'alternate')
          alturl =[k].href;

      // Get post url to check for uniqueness.
      checkurl = alturl.split("?");
      checkurl = checkurl[0];

      // If post has already been processed, skip to next in loop.
      if (ArrayContains(arr, checkurl))

      // Else continue processing.
        var linktext = checkurl.split("/");
        linktext = linktext[5];
        // Limit substring to keep on one line.
        linktext = linktext.substr(0,30).link(checkurl);
        if ("content" in entry)
          var comment = entry.content.$t;
        if ("summary" in entry)
          var comment = entry.summary.$t;
        else var comment = "";
        var re = /<\S[^>]*>/g;
        comment = comment.replace(re, "");
        document.write('<b>' +[0].name.$t + '</b> commented on <br/>');

        // Add checkurl to global array now that the post has been processed.
        arr[i] = checkurl;
<script src=""></script>
<font color="999999"><a href="" style="color: rgb(153,153,153)">Widget </a>by <a href="" style="color: rgb(153,153,153)">ReviewOfWeb</a></font>


J.D. Montague said...

Hooray for Hubby!!

Ana Mardoll said...

Seriously. I was like Columbo today: "Just one more thing, please?"

chris the cynic said...

Do the current comments seem to be jammed for anyone else? I think it hasn't updated in 20 hours or so.

It currently reads:

Randy Owens commented on

Ana Mardoll commented on

hapax commented on

chris the cynic commented on

That's not accurate anymore, it was 20 hours ago.

I'm worried that it might have something to do with me, I noticed it starting to be weird around the time Ana fixed a post I borked a post in the Narnia thread. I thought that it sorted itself out when it stopped saying that was the most recent post, but now it seems to be jammed again.

Ana Mardoll said...

Yes, it's borked, but I don't think it's your fault. The Current Comments widget looks at the site comments feed which is the BLOGGER feed. The DISQUS comments are supposed to be consumed by the Blogger site, so that if -- theoretically -- I turned off Disqus, we wouldn't lose old comments. For some reason, the last time Blogger nommed the Disqus comments was 10/5. I have a ticket out with Disqus, but I suspect the culprit is Blogger. *sad face*

Post a Comment