12 June 2008

Blogger Feeling Lucky Widget (Random Post)


from phydeaux3

I wouldn't have thought many people would think something like this would be useful, but a recent post on Techcrunch goes ape over wordpress having a 'random post' widget now. I figured if wordpress has one, New Blogger should as well.

With that in mind, here is the Blogger Feeling Lucky widget. Since we are in the Google family, I thought using the 'feeling lucky' nomenclature was appropriate. It's a silly little widget that adds a link to a random post on your blog. It is available in one-click add a widget to your Layouts template, or for classic templates the raw code is available to add that works as well. Also, a slightly tweaked version of the code also allows FTP Blogger blogs to join in the fun. All the things you need to install are explained on the setup page, although it's pretty simple. Currently I have it installed under the sidebar search box, via the manual code provided for exact placement.

Blogger Feeling Lucky Widget Install Page

Ricardo Santos has made a Portuguese translation available at Widgets para o blogger - Phydeaux3

Notes

For those that may be interested, here's how it works. Nothing really earth shattering going on. In the initial page load, the scripts make a json call to the feed url. Here I'm only interested in getting the total number of posts in the blog, so the call is made to the summary feed (to keep load time minimal) and with only one entry returned. The total number of posts is returned in New Blogger in the openSearch:totalResults field, so take the total number of posts and then use standard means to generate a random number within that range. Then the link is created using that number as it's variable.

Now at this point we don't have a valid url yet, just a number. So when the link is clicked, another quick json call is made, using the random number generated as the start-index parameter to get that entry in the feed only, a quick parse of the results to grab that entries url, then good old document.location (Thanks to Pete changed to window.location and a few other small tweaks to keep the back button working) to switch to it.

Old Blogger did have a similar tweak by Mario Brhemenz, but like a lot of our Old Blogger stuff relied on an outside service (del.icio.us)to work. I don't know that I've seen one for New Blogger before.

I think everything works as it should, but if you find any issues lemme know.


Zemanta Pixie

0 yorum: