November 22, 2006

Customizable recent comments widget (using JSON feed)

Translations by other bloggers: Spanish, Italian, French

Blogger announced that they support blog feeds in JSON format also. Soon, Hoctro found some sample code to use these JSON feeds and used it to come up with a recent posts/comments widget of his own. I realized that it is now possible to customize the recent comments widget further to anyone's needs. Earlier, I had given a method to use Blogger's feed widget to display recent comments. Using this method, we can only show very few words of the comment (too few to comprehend) and upto 5 comments only. I have developed a widget that allows the blog author to customize both these. You can see the new widget in my sidebar, showing entire comment mostly and significant portion of the comment in some cases.

With this widget,
1. You can specify how many comments you want to display in your blog (defaults to 10)
2. You can specify the maximum number of letters that should be displayed in each comment (defaults to 100).

Here is how you add this widget to your blog.

Step 1: Goto Template->Edit HTML tab. Find the
</head>
tag in your template and add all the code from this page before it and save the template.

Step 2: Goto Template->Page elements tab and click on "Add a page element" in your sidebar. Choose HTML/Javascript element. Give an appropriate title and copy/paste this code.

<br/><script language="javascript">
var numcomments = 10;
var numchars = 100;
</script>
<script src="http://yourblog.blogspot.com/feeds/comments/default?
alt=json-in-script&callback=commentDisplay"></script>

You need to change the blog URL to yours. You could also change the number of comments/letters per comment to whatever fits your blog. Then, save the changes and move the widget to wherever you want. There, you have a customized recent comments widget!!

For those who are interested in the details, I am displaying the content of each entry in the comment feed instead of the title (after removing all the html tags). I display only the number of characters as specified and also control the number of entries displayed. All this is done in the callback function to which the JSON object is passed.

I wish all my readers in US, a happy Thanksgiving!

99 comments:

Soumyadip said...

Sounds fun. Will give it a try.

Hoc Tro Viet said...

Ramani,

Your "recent comment" widget looks really, really clear! Cheers!

Unknown said...

Hoctro,

It was you who inspired me to use JSON!

Anonymous said...

Hi Ramani. I took your hack a little further, and created a fully customizable widget. You can customize it to display the comment-date and the post-title as well, and install it to your blog with one mouseclick. Take a look here

Unknown said...

It seems not work under "Double-Byte Character" system!!

The language we mostly use is Chinese.

So if u have time, could u re-code the JScript to a more flexible one ^_^ thx..

Btw, if u don't mind u can use my blog comment feeds for "Double-Byte Character" testing..

LOUI$$ said...

Thanks bor ramani,another great improvement of beta!

Swipe said...

Is it just me or this is hack not working in Firefox?

Boy said...

will this work for the "latest posts" too? or is this just for comments?

Unknown said...

Tsung-Kai Chen,

Can you try specifying double the number of characters for now. I don't know how to identify double byte system.

Swipe, it does work in firefox too.

Unknown said...

gentletouchee, Hans has come up with a recent posts widget based on this. check this out.

http://beautifulbeta.blogspot.com/2006/11/customizable-recent-posts-widget.htmltml

delang said...

ramani, you've done great job, most of your hack already implemented to my new blog.please do visit my blog leave a comment.thanks u.
http://delang-d.blogspot.com/

Spicc said...

For some reason it does not work on my site (i completed step1 and 2). Would appreciate if you could look and check what went wrong. Adress: kezdoszolesz.blogspot.com.
Tks in advance

Unknown said...

Spicc,

Looks like the line breaks are not proper. Can you make sure the two lines starting with 'var' are not merged?

Spicc said...

I made an attempt by separating the two var part. However does not work, nothing is displayed. I used even your url to test. Now only two words from your recent comment are displayed. Strange. Can you have a look again? tks

--David said...

I am having the same problem that Spicc is having, I think... I get nothing in the comments area, and have a javascript error on the page... In my HEAD section, I added the code as instructed. However, I get the following error: Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: Element type "script" must be followed by either attribute specifications, ">" or "/>".

If I add a space after the TYPE and before the SRC, it publishes, but the comments are blank... Any ideas?

Unknown said...

David, looks like you are now using Hans' version. can you follow up with him?

TGP said...

Great product! Just one concern...when I delete a spam comment from the post, it still shows up in the "Recent Comments". Any thoughts on how to fix this?

Unknown said...

Trailertrash, i wonder if it goes away after some time because the feed might take some time to update for the deletion. If it doesn't, there's no way to fix it.

Unknown said...

Wow thanks for your widget...

The Imaginary Diva said...

Ramani, is this code only for beta? I have been looking forever for a recent comment widget for standard beta. You know where I can get one?

Unknown said...

Diva,

Old Blogger hacks are available at blogfresh.blogspot.com. But, I strongly suggest you to switch to New Blogger because things are easier.

Michael Mace said...

Thank you very much. This is great!

Unknown said...

Can you tell me how to align the comments of the script to the left?

bomtemponocanal.blogspot.com

Unknown said...

Sorry to bother again. in the blog it shows someone commented something.

Because my blog is portuguese is there anychance I can change that to disse

SDtektiv said...

I did step one, but when I saved it, it said my code wasn't "well-parsed"... any ideas?

Unknown said...

Can you check if you copied the code properly? This has worked for so many people.

wlh said...

Ramani, thanks for the widget. I think I know why some people may be having problems.

First, for the html edit for the template

use a forward slash, not a backslash between text\javascript

Second, when setting up the page element, you may delete the words following // number of comments to display and // number of characters to display

Set the numbers to what you prefer.

Since I am mostly html illiterate, it took me about an hour to get this thing to work. But it works well when the above changes are made.

Wes

Unknown said...

wlh, good to know you got it working. I will remove those comment text to avoid problems.

Dek Mat said...

hiya ramani... the widget was working fine until today where suddenly it all went blank!

its weird!

can u help?

Unknown said...

Dek, it seems to work for you now. it might have been a temporary problem.

The Imaginary Diva said...

Thanks a lot Ramani for the info.

I am looking to move my blogs to beta soon....

N said...

what's the maximum time it will take to display the recent comments?? :( my widget is not working

Unknown said...

I really don't know.

Matthieu said...

I have the same trouble : I tried to install it (and hans version too...), and no comments appeared.

Could it be possible that something, this weekend, makes the feed really slow ? I have waited for about 10 minutes to see if something was eventually coming, I have tried to had test comments, but nothing worked.

I leave the widget on my page, if you want to have a look a it. thx for any help

Unknown said...

Mathieu and Radvixen,

Looks like both of you had enabled only short version of comment feed. That's why it didn't work. I have fixed the problem in my script hackosphere.js. Matthieu, I checked that it works now in your blog.

Radvixen, you can try adding the hack again and it should work.

Matthieu said...

Thx a lot !

Andrew said...

Hi Ramani.
I speak Spanish, I don't speak English much.
Interesting web.
But How I can to show recent comments in Spanish.
Similar a Hans How.
Bye

சயந்தன் said...

Hi Its sayanthan. I have been using ur script for last 3 weeks. It was fine and working.
but from today 16.02 It shows my old comments.. not new one.
actually its not only for me..
sorry my blog is in my mother language in tamil. so i couldnt show what exatlly happen..
http://yourblog.blogspot.com/feeds/comments/default

to check i added this link into my google reader. it also shows 2 years old comments.

do u know any news..? r they changed anything..?
thanks
sayanthan

Unknown said...

Hi Sayanthan,

Blogger seems to have messed up with everybody's comment feed. Mine is also messed up. Let's wait till they fix the problem.

By the way, I used to blog in tamil as Venkataramani, you might remember me. So, I can understand tamil. I have read your blog too.

Laurie Stark said...

this is awesome, thanks. is there a relatively easy way to leave out reply comments left by the blog owner (me)?

thanks again!

"MamaDubs" Bobbie said...

I am a member of mybloglog.com. I want to take the messages that are left for me there and have them in my sidebar in a recent comment widget. Is this possible?
If so, is it easy? I'm a bit of a beginner.
Thanks,
Bobbie

Unknown said...

Bobbie,

That's not easy. I am not sure if it is possible even.

Krystalline Apostate said...

Ramani:
So is there any news about the JSON feeds? I've used your hack - it only pulls comments from posts back in March of 2006.
It used to work - not any more. Any news whatsoever?

Unknown said...

Krysttalline,

I thought Blogger folks will fix it quickly but they don't seem to. Meanwhile somebody found a workaround. You can try changing the URL given in the post to

http://YOURBLOG.blogspot.com/feeds/comments/default?start-index=2
&alt=json-in-script&callback=commentDisplay

Anonymous said...

Ramani - thanks! That worked like a charm. Obliged

Krystalline Apostate

lavocedelmuro said...

Thank you for your widget, it works fine.
It's possible to change the language the phrases "ON" and "commented"?
In the first version by beatiful beta i made it but now i think whe should modify the code of the script.
Bye

Hello and Color said...

Thank you for such a great job to provide this hack. I installed it in my blog and it works pretty well. Thanks!!!

Gee Why said...

First off, Ramani, thanks for this widget! For my blog (pulpconnection.blogspot.com), my comments feed started working again this morning. I was using the start-index=2 hack which was working with this Hackosphere widget.

Even though my comment feeds works, the default Hackosphere widget (without the start-index=2 hack) still doesn't work (old comments still show up).

But I guessed and tried using a start-index=1 and now everything works like it used to. The most recent comment shows up with this Hackosphere recent comments widget.

Unknown said...

Gee Why,

After seeing your comment, I rechecked my comment feed and it seems to start with the latest comments. I wonder why the widget wouldn't work without start-index. Did you try restarting the browser?

Gee Why said...

Can't remember if I restarted my browser or not (probably not). I just tested with removing the start-index, and everything works as before.

Thanks again for this great hack.

Simon said...

Fabulous hack. Best out there. thanks.

The_Editrix said...

WONDERFUL hack! THANKS!!!

Annie Chang said...

Hi, Ramani:

Can you expand the var numcomments = 10 to 30 or 50?

10 is not enough to my blog.

>.<

Thanks a lot in advance.

>.^

Annie Chang said...

To Rmani:

I cannot sure that my prior message posted successfully or not.

Please expland the var numcomments = 10 to 30 or 50.

Many thanks for your assistance in advance.

Ciao!

>.^

Unknown said...

Annie,

You can change the number to any number upto 25. If you want more than 25 (that maybe too many), you have to add max-results=x to the feed URL.

Anonymous said...

It just does not work. Ive followed your instructions to the letter, but blogger will keep saying "error", stating that the brackets are not closed properly, EVEN THOUGH THEY ARE. Why did Google have to ruin blogger?

Jeff Wharton said...

Hey, great stuff...

Is there anyway to add some functionality to remove comments from certain authors? i.e. I want to only show comments left by others and not my replies

pommygranate said...

Ramani

Will this work for Haloscan comments?

Unknown said...

No, it won't work with haloscan.

Jedi Dadi said...

This is exactly what I'm looking for but I can't seem to get it to work. I've followed the instructions exactly.
Is it because I have a private group blog? Any ideas on what else I should do?

Unknown said...

Yes, steve, feeds won't work with private blog and so, this won't work too.

philip said...

I'm trying to mess with it now to make it say "answers" instead of "comments".

philip said...

I'm having the same problem with this that many non english users are having. They want to change "commented" to "respondio" or something. My blog is about questions and answers so I call comments "answers". I want it to say "XXXXXX answered" instead of "xxxx commented". Is it possible to change this?

Unknown said...

Philip,

That text is shown by my script right now and it's common for everybody. I don't have support for modifying it.

Tisha! said...

woohoo worked like a charm thank you so much!

Guru said...

Hi again ramani,
It's sad that I cannot post on beautiful beta blog, but I guess that the script is a co-work of you both, so I post my question here :-)
Again, the question is about the blog Mobilmag.dk
The problem: There are some international characters, which the script can't show them, if they are used in the post's title. for example: å æ ø ä, If the tile is: "ærlig mobiltelefoni", It will show it like this: "rlig mobiltelefoni" which does not mean anything!
The solution: I think there could be a solution to this problem. I had a look at the .js (version 2.1) and found out that the script will use the title which is used in the comment feed to show it as the post-title. I looked at the feed and I found out that the characters disappears already in the feed. So I want to ask if there is a way that the script can follow a link to find the real title? and if yes, will it make the web page loading time longer?
Thanks in advance.
Regards,
Mobilguru

Unknown said...

Mobilguru,

Right now, I don't have any plans to upgrade this script. sorry.

akull-naja said...

Thank you!!! Very simple, very clear!

outsounds@gmail.com said...

ok it works :-)

Thanks a lot you for this widget.

I've translated this post in italian for other italian bloggers, in my blog.

greetings from italy

Unknown said...

Outer, thank you. I will add a link to your post.

Tuxicoman said...

I've made a french translation + tuto of your script on my blog : http://tuxicoman.blogspot.com/2007/06/widget-commentaires-rcents.html

Thanks !

Unknown said...

Thank you. I will add a link soon.

Katherine said...

Hi, thanks a million for this widget, it works like a dream!

Choonie, the Guru said...

Hey...thanks a million. It works in my blog. Your instruction is very simple and easy to follow. Thanks. I will look forward for more interesting widgets.

Unknown said...

Wonderful Widget! Simple but useful.
thank you so much

Phuong said...

could you possibly help me to customize the 'Post A Comment' link like yours, please? it looks very nice.

Further more, my comment link at the end of my blog does not aggree to show up.
My blog is http://ezones.blogspot.com

Very much appreciated

Admin said...

thanks for fixed it, keep the good work ramani ... it working at my blog again. thanks

http://whoisbritney.blogspot.com

outsounds@gmail.com said...

something is going wrong with recent comments widget. in my blog they don't appear. how to fix it ?

Thank You

outsounds@gmail.com said...

sorry I haven't read the post on fixing the new code to change in head section. THX

greetings

Erin said...

It seems Blogger might have recently broken their Feeds widget, at least as it is used for the comments feed.

But...I thought I had seen something here; I was glad to find this and I am using it now. I appreciate it because it saved my Recent Comments widget. Thanks!

Oh, and you can add me to your Neo-Powered Templates list if you want. I have been very happy with it.

Unknown said...

Erin,

Yes, I read about that in Phydeaux3's blog. I will add your blog to the neo list.

fabdo said...

Hallo Ramani,

I've done what u suggest to add this widget to my blog. It works, except for the fact that the comments show no active link, only the authors do.
Do you know why and can you help me on this?!

Ciao
Donatella

William said...

Hey Ramani,

Thanks for the widget. It works great.

You can check it out on my site :
Econoverse.blogspot.com

Goryo said...

cool! works perfectly! 5 star to this blog!

Anonymous said...

Ramani-
Absolutely love the widget, thanks!

Two questions,
how do you change the number of comments/letters per comment

and can you share with me how to properly display all comments on their own page, such as shown on your comment area...

Feel free to drop by and post an
ad in my FREE Advertising Blog:

http://freeadblogger.blogspot.com

Kanan said...

Ramani, awesome stuff! really glad that you have shared the code of the script as well. It's working great! Thanks much.

J said...

I am grateful to find your site! You just saved my frustration!!! =) Thank You so much for sharing with us the code!

My Fiancee is Hot © said...

Just wanted to say thx loads for the tip. I added it on my blog and it works perfectly. Thx again

Unknown said...

I'm very sorry but my comment is to ask you for help.
It seems not to work on my template ...

Probably (For sure) it's because I'm dumb, but so far I'm unable to find out where is my error...

I've indeed added your code just
under

/body tag
and before
/head tag

Then I've addel the code you give
in your post
swapping dipofilopersiflex for
yourblog ...

In my blog, I can see the
" Comentaris recents " new element, but none of my own comments ara added to my blog.

Would you mind have a loojk at it and tell me what you think ?
Thnaks a lot

http://dipofilopersiflex.blogspot.com

silverine said...

Thank you!! Since I get comments on my archives too, this tool is very useful :)

Unknown said...

nice!! tanx for the widget:)

Terry said...

Ramani - Thank you for this website, your instructions are very clear with great explanations.

I am new to blogging, XML, and widgets, and would like to know if there is any way to use a "recent comments" widget that does not requite a feed. My problem is I have a private family blog on blogger.com that is accessible only to invited readers, so it will not allow me to add a feed URL. Currently I worked around this by converting the newer Layout template back to the "Classic" HTML template and used HTML code to simply display all recent comments for the current page in the sidebar, but this has limitations in that it is not customizable and does not show them in absolute most recent order.

Any help you can provide or point me to is greatly appreciated. I have searched all over blogger.com help and Google Groups and can find nothing on how to do this for private blogs.

cejan said...

is there anyway i can show the date the comment was posted too?

Cindy Jones Lantier said...

Love your "Recent Comments" hack. I've just installed it on my test blog and it worked so well, I moved it over to my regular blog, too.

Thanks for all your hard work!

San said...

Hi Ramani,

Thanks for your widgets and 'hacks'. I have been using some of them (you are as you asked in my blogroll under "friends" because I don't know you but I consider you a friend). Now, JFYI your customizable 'recent comments' widget worked until today. I am not sure what Blogger may have done but since today not only shows nothing but does not show any other widget that I had before yours.

So I had to take it out. I will try with Hans' option now, but I wanted you to know in case you need to tweak it based on some change Blogger may have done. It was working perfectly and now it doesnt matter what I do it just cuts the whole navbar not showing anything after your widget (your widget included).

JFYI... thanks in advance if you find a solution... San :)

Ray said...

Thank You...
I will Try it

Phillip Harvey said...

This hacked works like a charm, IFF you have a public blog and your comments feed is turned on.

Ramani, perhaps you should edit the main post to mention these two requirements: (1) that Reader Permissions must be set to "Anybody" in order for the recent comments widget to be able to receive the comments feed, and (2) the comments feed must be turned on. I just spent about two hours troubleshooting before I found that all I had to do was click two buttons.

Thank you for the excellent hack. I love this widget and it works great.

Unknown said...

I'd really like to add both this one and the tag cloud to my blog, but it seems that I can't access anything on googlepages.com from within China. Are the code examples posted anywhere else?

Thanks.

masimet said...

well done Ramani, it really make my blog more nice...