Chris Risner . Com

Google Plus One Enabled!

Posted on: 6/2/2011 12:58:00 AM by

While going through some of the recent news, I noticed that Google had officially released their Plus One capabilities.  Plus One seems to be getting a lot of press as being “competition” with Facebook.  As far as I can tell, the idea is that since I can hit the Plus One button on a site or a search result and then my friends will see it as more relevant in search results, that competes with sharing a link in Facebook.  I don’t share links on Facebook that often though so maybe I just don’t see it yet.  At any rate, I felt that I should immediately  comply with Google’s implied wishes and add Plus One to my site and the Veritas source code (now on GIT, more about that later).  Now, whenever you view an entry on the site, you can see the Plus One button near the bottom of the entry.  Feel free to click it as much as possible so that other people will know as well!   As far as implementation goes, it is VERY SIMPLE.  You can view instructions on implementing it at the Google Webmaster site.  You need to put two tags in your html.  In the head, or just before the body close, you need to put the javascript include:

<script type="text/javascript" src="http://apis.google.com/js/plusone.js"></script>

From there, all you have to do is insert the following where you want the “Plus One” button to appear:

<g:plusone></g:plusone>

There are a few parameters you can read about for use with the plusone tag.  Since I wanted to show the Plus One on pages where I’m showing multiple entries, I need to specify what URL I’m referring to for each instance of the Plus One.  This is done easily enough by adding a HREF attribute to the plus one tag.  So now the tag from above becomes something like this:

<g:plusone href="http://chrisrisner.com/myUniqueUrl"></g:plusone>

That’s about all there is to it.  Plus One Away!

Categories: Google, Javascript, Veritas, Web
Bookmark and Share
First Article

How to Quickly Show a Gravatar

Posted on: 3/1/2010 11:34:00 PM by

Gravatars While most of what these people add to the internet is absolute garbage, many people like other people to know when they’ve said something.  One of the ways people are doing this is through the use of Gravatars.  Gravatars are small images that (can) uniquely identify you as a commenter or contributor on a website, typically a blog.  For example, if you go and look at the comments on this site, you’ll see that any comment I’ve made has a small image of morlock next to it.  Any other site that I comment on that uses gravatars will show the exact same image.  Due to the way it’s set up, where a site will set an image’s source to be a gravatar.com url, I could change my image on all of these sites whenever I want to.

Recently while commenting on a session for Mix, a conference I’m attending in a couple weeks, I noticed that as soon as you entered your email address, it would show your gravatar.  Figuring that this would be a nice little addition to my site, I added it a few days ago.  Since many people do use Gravatars, I thought I’d demonstrate an easy way to add this to your web site.  The sample I’m showing specifically applies to ASP .Net MVC, but it could be taken and easily altered for other applications. 

Our example will consist of three parts:  the view, the javascript, and the controller method.

The View is very simple and just contains a field that we can put our email address in and an image tag that can have it’s source set later:

   1:  <dl>
   2:       <dt><label for="EmailAddress">Email Address:</label></dt>
   3:       <dd>
   4:            <%=Html.TextBox("Email")%>
   5:       </dd>
   6:  </dl>    
   7:  <br />
   8:  <img class="newgravatar" title="Gravatar"/>

Our javascript is a bit more complex and shows an example of using ajax to go back to the server to get the path to our email address’ gravatar:

   1:  window._emailRegex = /^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*$/i;
   2:   
   3:  $(document).ready(function() {
   4:      //Gravatar stuff
   5:      $('input[name=Email]').blur(function() {
   6:          var email = $(this).val();
   7:          if (email.indexOf("@") > 0 && window._emailRegex.test(email)) {
   8:              $.post('/Home/GetGravatarPath', { value: email }, function(gravatarPath) {
   9:                  $('img.newgravatar').attr('src', gravatarPath);
  10:              });
  11:          }
  12:      });
  13:  });

You’ll notice here that we’re also checking up against a regex to make sure the email address entered is valid.  Once we’re sure it is, we’ll post back to a method on our Home controller.  The result of that method call is then set to the source attribute of the image with newgravatar as it’s class.

Lastly, we have our controller method:

   1:  public string GetGravatarPath(string value)
   2:  {
   3:       return Gravatar.GetGravatarURL(value);
   4:  }

and the class that method calls to actually get the path:

   1:  public class Gravatar
   2:  {
   3:       public static string GetGravatarURL(string emailAddress)
   4:       {
   5:            string imageUrl = "http://www.gravatar.com/avatar.php?";
   6:            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
   7:            UTF8Encoding encoder = new UTF8Encoding();
   8:            byte[] hashedBytes = md5.ComputeHash(encoder.GetBytes(emailAddress));
   9:   
  10:            StringBuilder sb = new StringBuilder(hashedBytes.Length * 2);
  11:            for (int i = 0; i < hashedBytes.Length; i++)
  12:            {
  13:                 sb.Append(hashedBytes[i].ToString("X2").ToLower());
  14:            }
  15:   
  16:            imageUrl += "gravatar_id=" + sb.ToString();
  17:            imageUrl += "&size=40";
  18:            return imageUrl;
  19:       }
  20:  }
This class contains the “important” part where we actually have to build our path to the gravatar.  As you can see, we can create the path without actually ever having to go to gravatar.com.  Instead we just use the agreed upon algorithm and return that url.  That url is then used as the image source above in our javascript.  After all this, we have an “ajaxy” way of displaying our commenter's gravatars that looks pretty slick and was only 47% stolen from the Mix site.  Enjoy!

Source code: download

Categories: .Net, Javascript, MVC, Veritas, Web
Bookmark and Share
First Article






Categories