Comment Author Photos
Alright, Vivek asked me about adding comment author pics for blogger beta, so I went looking and found that Singpolyma had already done this for pre-beta Blogger. After looking at it a little more it looked like it would work for the beta too.
Here's what you wanna do... First of all go to Template -> Edit HTML, check the box for Expanding the widget template, and find the commenting section:
<dl id='comments-block'>
<b:loop values='data:post.comments' var='comment'>
<dt class='comment-author' expr:id='"comment-" + data:comment.id'>
<div class='commentphoto' style='float:right;'/>
<a expr:name='"comment-" + data:comment.id'/>
<b:if cond='data:comment.authorUrl'>
<a expr:href='data:comment.authorUrl' rel='nofollow'><data:comment.author/></a>
<b:else/>
<data:comment.author/>
</b:if>
<data:commentPostedByMsg/>
</dt>
<dd class='comment-body'>
<b:if cond='data:comment.isDeleted'>
<span class='deleted-comment'><data:comment.body/></span>
<b:else/>
<p><data:comment.body/></p>
</b:if>
</dd>
<dd class='comment-footer'>
<span class='comment-timestamp'>
<a expr:href='"#comment-" + data:comment.id' title='comment permalink'>
<data:comment.timestamp/>
</a>
<b:include data='comment' name='commentDeleteIcon'/>
</span>
</dd>
<div style='clear:both;'/>
</b:loop>
</dl>
The 2 lines in bold are what you add. You can change float:right to float:left if you want the pic to be on the left instead of the right
Now scroll back up and add the following before the </header> tag:
<script src='http://jscripts.ning.com/get.php?xn_auth=no&id=830605' type='text/javascript'/>
<script type='text/javascript'>
//<![CDATA[
addLoadEvent(function(){showCommentPhotos('commentphoto','comment-author', 1);});
//]]>
</script>
Optionally, you could replace the external javascript file
http://jscripts.ning.com/get.php?xn_auth=no&id=830605
with
http://yggomelprup.googlepages.com/blogger_comment_photos.js
where I've tweaked a bug where the default (noimage) image wouldn't show up in some cases, and I've limited the size of the pics to 60x75. I'm also using a different default image.
To read more about this head over to Singpolyma - Technical Blog
Update of the Update :P
Here's what I updated in the external javascript file (the parts in bold are what I added or changed):
/* Script by Stephen Paul Weber a.k.a. Singpolyma (http://singpolyma-tech.blogspot.com/)
Based on work by Johan Sundstrom (http://ecmanaut.blogspot.com/2005/10/blogger-hack-inline-comment-faces.html)
Updated by PurpleMoggy (http://purplemoggy.blogspot.com/)
*/
if( document.all && !document.getElementsByTagName )
document.getElementsByTagName = function( nodeName )
{
if( nodeName == '*' ) return document.all;
var result = [], rightName = new RegExp( nodeName, 'i' ), i;
for( i=0; i<document.all.length; i++ )
if( rightName.test( document.all[i].nodeName ) )
result.push( document.all[i] );
return result;
};
document.getElementsByClassName = function( className, nodeName )
{
var result = [], tag = nodeName||'*', node, seek, i;
var rightClass = new RegExp( '(^| )'+ className +'( |$)' );
seek = document.getElementsByTagName( tag );
for( i=0; i<seek.length; i++ )
if( rightClass.test( (node = seek[i]).className ) )
result.push( seek[i] );
return result;
};
function writeScript(src) {
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.src = src;
document.body.appendChild(thescript);
}//end function writeScript
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}//end if
}//end function addLoadEvent
BloggerProfiles = {};
BloggerProfiles.callbacks = {};
BloggerProfiles.noimage = 'http://img139.imageshack.us/img139/1011/defaultavatarad7.png';
BloggerProfiles.imageWidth = 60;
BloggerProfiles.imageHeight = 75;
BloggerProfiles.callbacks.profile = function(data) {
if(data.photo) {
node = document.createElement('img');
node.alt = 'Photo';
if(!data.photo.url)
data.photo.url = BloggerProfiles.noimage;
node.src = data.photo.url;
if(data.photo.width)
node.width = data.photo.width;
if(data.photo.height)
node.height = data.photo.height;
if (node.width > BloggerProfiles.imageWidth) {
node.height = (BloggerProfiles.imageWidth * node.height) / node.width;
node.width = BloggerProfiles.imageWidth;
}
if (node.height > BloggerProfiles.imageHeight) {
node.width = (BloggerProfiles.imageHeight * node.width) / node.height;
node.height = BloggerProfiles.imageHeight;
}
thecomments = document.getElementsByClassName('commentphoto-'+escape(data.url));
for(var i=0; i<thecomments.length; i++)
thecomments[i].appendChild(node.cloneNode(false));
if(data.blogs) {
for(var i=0; i<data.blogs.length; i++) {
if(data.blogs[i].url == BloggerProfiles.blogurl) {
thecomments = document.getElementsByClassName('commentblock-'+escape(data.url));
for(var i2=0; i2<thecomments.length; i2++) {
thecomments[i2].style.color = BloggerProfiles.color;
thecomments[i2].style.backgroundColor = BloggerProfiles.bgcolor;
}//end for thecomments
}//end if blogs.url == blogurl
}//end for blogs
}//end if data.blogs
}//end if data.photo
else {
node = document.createElement('img');
node.alt = 'Photo';
node.src = BloggerProfiles.noimage;
if (node.width > BloggerProfiles.imageWidth) {
node.height = (BloggerProfiles.imageWidth * node.height) / node.width;
node.width = BloggerProfiles.imageWidth;
}
if (node.height > BloggerProfiles.imageHeight) {
node.width = (BloggerProfiles.imageHeight * node.width) / node.height;
node.height = BloggerProfiles.imageHeight;
}
thecomments = document.getElementsByClassName('commentphoto-'+escape(data.url));
for(var i=0; i<thecomments.length; i++)
thecomments[i].appendChild(node.cloneNode(false));
if(data.blogs) {
for(var i=0; i<data.blogs.length; i++) {
if(data.blogs[i].url == BloggerProfiles.blogurl) {
thecomments = document.getElementsByClassName('commentblock-'+escape(data.url));
for(var i2=0; i2<thecomments.length; i2++) {
thecomments[i2].style.color = BloggerProfiles.color;
thecomments[i2].style.backgroundColor = BloggerProfiles.bgcolor;
}//end for thecomments
}//end if blogs.url == blogurl
}//end for blogs
}//end if data.blogs
}//end else
}//end callback
function showCommentPhotos(photoclass,linkclass,linknum,fullclass,blogurl,color,bgcolor) {
var comments, comlinks, i, node, re, by, blocks;
BloggerProfiles.blogurl = blogurl;
BloggerProfiles.color = color;
BloggerProfiles.bgcolor = bgcolor;
re = new RegExp( '^http://www.blogger.com/profile/\\d+', 'i' );
comments = document.getElementsByClassName(photoclass);
comlinks = document.getElementsByClassName(linkclass);
if(fullclass)
blocks = document.getElementsByClassName(fullclass);
else
blocks = [];
var profilesobj = {};
var profileurls = [];
for(i=0; i<comments.length; i++) {
by = comlinks[i].getElementsByTagName('a').item(linknum);
if(by && re.test(by.href)) {
comments[i].className += ' commentphoto-'+escape(by.href);
if(blocks && blocks[i])
blocks[i].className += ' commentblock-'+escape(by.href);
} else {
comments[i].className += ' commentphoto-nophoto'+i;
}//end if-else
if( !by || !re.test( by.href ) ) {BloggerProfiles.callbacks.profile({'url':'nophoto'+i,'photo':{'url':BloggerProfiles.noimage}});continue;}
if(!profilesobj[by.href])
profileurls.push(by.href);
profilesobj[by.href] = true;
}//end for
for(i=0; i<profileurls.length; i++)
writeScript('http://singpolymaplay.ning.com/bloggerProfile.php?xn_auth=no&url='+encodeURIComponent(profileurls[i]));
}//end function showCommentPhotos
There's nothing in this file you need to customize anymore. If you'd like to customize the default picture, or customize the max height & width then you can just do something like the following (in place of the 2nd codebox above):
<script src='http://yggomelprup.googlepages.com/blogger_comment_photos.js' type='text/javascript'/>
<script type='text/javascript'>
//<![CDATA[
BloggerProfiles.noimage = 'http://img304.imageshack.us/img304/3518/rect18st2.png';
BloggerProfiles.imageWidth = 10;
BloggerProfiles.imageHeight = 20;
addLoadEvent(function(){showCommentPhotos('commentphoto','comment-author', 1);});
//]]>
</script>
The 3 lines in bold are optional. If you don't want to limit the width & height then you can just set those 2 lines to some ridiculously high number, like 1000. The BloggerProfiles.noimage line will work with both Singpolyma's external javascript file as well as mine. The BloggerProfiles.imageWidth & BloggerProfiles.imageHeight lines will only work with mine.
PurpleMoggy's Blog


Loading...

34 comments:
Hi purple,
I've installed your hack, but the comment and profile image are too close. How can I put a space between them?
@protesto
some css something like the following should work:
.commentphoto img {
margin: 2px;
}
Thank for the solution, I will add it to my fully modded templates soon.
How do you make your comments stand out? I noticed in your blog that when you comment, it's highlighted a different color in your template?
thechicas@gmail.com
@Welcome = أﻫﻼ ﻭﺴﻬﻼ
This post from Hackosphere should get you started
That explains how to make the comment-body look different
I just took it a little further and applied the same type of thing to the comment-author, comment-body, and comment-footer sections
I implemented this in my blog. Works Gr8! Thanks. I too had a similar request. Why don't you make a tutorial on styling the comments section. It will help CSS dummies like me. You might want to include stuff like enclosing comments in CSS box/blocks with the profile image also in it.
BTW I also implemented the theme switching hack. Thanks
Hi purple :
I've install the hack several months ago, and I found it interesting that we 2 just correct almost the same bug...^_^
Btw, nice Blog!!
Dear purple:
I forgot to mention one thing, that is your javascript code could be more
intuitive and fewer code size!
take a look at http://phpfi.com/184679
Hey there!
Thanks so much for doing this! I would have eventually, but I'm just so stuck for time right now....
A few questions/points:
1) The bold in your modified version of my script isn't showing up for me. I'd like to know exactly what you changed and why so that I can work it back into the original script file :)
2) Did you add the stuff for limiting the image size? I had that originally, but people were complaining that they wanted their image the same size and dimensions as on their profile page. Perhaps it should be optional?
3) You don't have to host the file yourself to change the default 'no image' image. All you have to do is this:
<script src='http://jscripts.ning.com/get.php?xn_auth=no&amp;id=830605' type='text/javascript'/>
<script type='text/javascript'>
//>![CDATA[
BloggerProfiles.noimage = "URL to image";
addLoadEvent(function(){showCommentPhotos('commentphoto','comment-author', 1);});
//]]<
>/script<
In fact, it is stored in a global object variable very on purpose to make customisation possible :)
Can you post your hack?
Thank your for your response. I checked Hacksphere's post about author comment highlighting but I'm looking for something different. Hackosphere's hack changes the color of the text of the author's comment. On your blog, the whole comment section of the author is highlighted by a different color. How did you do that? I would like to implement into my blog the way you have it.
@Singpolyma
First I changed default image, I didn't realize you could change that outside the script, thanks
Yes, I added the part for limiting the image size. That part probably could be better (not hardcoded into it), but I was really just doing it for me
The error I noticed was that if the comment author had a blogger profile, but it was either not public or they didn't have a photo then the default image is never displayed. I finally figured out it's the function that gets called from your PHP file. The first part checks for data.photo which isn't there in these cases, so the script just ends at that point. I just added the else part so the default image does show up.
@Tsung-Kai Chen
Well, yours is definately shorter. I still think mine's more intuitive cuz it's fully written out. But yeah, shorter here's probably better.
@Welcome = أﻫﻼ ﻭﺴﻬﻼ
His only becomes red because he sets some CSS so that it becomes red, you could just as easily set the background color and border with CSS (leaving it setup the same way). I did see your other question too about the peek-a-boo, I've just been really busy lately. Maybe I'll make another post when I get a chance just to show the couple updates I made to other people's hacks.
I tried to create my own javascript so I could change the BloggerProfiles.noimage but it wouldn't work. Can you help me? I wanna change the default image you created to this one: http://bp1.blogger.com/_gtm-tgQS2BM/RYDlNyo0uMI/AAAAAAAAAKg/bLFKNkbdSW0/s1600-h/noimage.png
I updated the post. See above for info on how to customize the default pic, as well as the default max width & height
I used your updated hack and it still doesn't work for me. I replaced the second javacript for the first and it didn't work at all. All I wanna do is replace the BloggerProfiles.noimage with the one you have to this one:
http://bp1.blogger.com/_gtm-tgQS2BM/RYDlNyo0uMI/AAAAAAAAAKg/bLFKNkbdSW0/s1600-h/noimage.png
Beta Blogger admits php, you can use a rotator random php to vary the image of bloggers with no image profile, whenever it loads the page
Rotator Image php
This is a great little hack. I noticed a slight problem with the example code which was preventing it from working for me (at least in Firefox). In your 2nd text field, where you are talking about the JS code that goes in the header, there should be a closing script tag </script> after the very first line.
@Kato
I noticed that when linking to an external javascript file with:
<script src='file.js'></script>
blogger automatically changes that to:
<script src='file.js'/>
which is probably where I copied and pasted it from, and is still valid XML.
I'm using Firefox and it seems to be working fine (for the most part). I have noticed that sometimes some of the little avatars don't show up or sometimes they'll double or triple up next to each name. I talked to Singpolyma (the original author of this hack) and he said he'd noticed this too recently, but wasn't sure why it was happening.
Ah, well, ignore my first comment then. The script wasn't working for me the other day until I added that ending tag, but now it appears that if you do that it simply removes it (though it appears in the resulting HTML). They've had some downtime the past couple of days, maybe they modified some of their script handling code or I hit a temporary glitch.
I have little problems with this hack! Could you help a little?
binäärijärjestelmä
@Tux
The first thing I noticed in your page's source was that what you copied and pasted from my codebox (which is on multiple lines) is all on one line on your blog, which should have still worked except I was missing a semi-colon (which I've now corrected above).
In the future, if you can, try to keep whatever is on a new line, on that new line (because obviously I'm terrible when it comes to forgetting semi-colons every once in a while).
Hopefully that was the only problem *fingers crossed*
Here again!
Shall I post the gode to you so you can check it directly?
Hello
I have a problem with this hack...
Dumb Q: Does this hack allow comment author profile photos to be seen in the comments section of the post page?
Well, if the answer to that is "yes", then it is not working on my blog.
In fact, i can't see it working on your blog, or Singpolyma's blog, either!
Do you suppose it is something to do with my browser? I'm using Mac OSX, Firefox v1.0. Also tried it in Safari v1.25.
I can see profile pics on the comments-posting page, but not the post (entry) page.
Any help would be greatly appreciated.
Best wishes
@iMuslim
I actually disabled this here because of a few issues I was having with it.
This makes the profile pics show up on the permalink page. Blogger already shows the pics by default on the page where you type in your comment (www2.blogger.com/comment...)
I just checked your blog with Firefox 2 and it seems to be working.
Weird thing happened: after i read your comment i went back to my blog, and it was working! I didn't change anything!
Anyway, i've decided not to bother with it now... it doesn't suit my template. hehe
Thanks for your help!
ola
Good work!!!
Greetings,
Hello Purple:
Hi... from Portugal :-)
Everything was conveniently installed but...
which is the reason doesn't appear in the comments the author's photo?
The address of tests blog (new Blogger) is:
http://viriato2.blogspot.com
Can you help me?
Greetings,
Great work!!! Greetings!
Might I reccomend the new and improved http://singpolyma.net/2007/05/new-comment-photo-hack-blogger/ ?
Very interesting, thank you.
If interested, can visit Louisiana home owner insurance blog.
Thank you!
Hello Purple Moggy:
I´ve tried to install this utility of comment author photos, and I do exactly what you specify, but I can´t see the photos. Can you check my blog and help me please? I will be very thankful with you...
www.alicantevivo.org
rubodewig@gmail.com
Thank you!!
Post a Comment