Microformats Bookmarklet

September 25th, 2006

Inspired by John Hicks microformats Safari mock, it was quickly apparent that this (or a version of this) functionality was possible through a bookmarklet.

Bookmarklet

To use the bookmarklet, drag the link below on to your bookmark toolbar:

Microformats

Originally I had intended for the entire logic behind the Microformats overlay to be within the bookmarklet. However, it’s particularly difficult to get all the code on one line, and this more simplistic bookmarklet means upgrades will take automatic effect.

Usage

The bookmarklet has been primarily been designed for Safari, as Firefox has Tails. However, the Microformats Bookmarklet supports Safari, Firefox and IE (both 6 and 7).

Click on the bookmarklet once it has been saved, to bring up an overlay window of all the hCards and hCalendars within the current page.

Example of Microformats bookmarklet running

To save an individual item, click on the item heading and it will automatically (if you’re using Safari) save the .vcf or .ics files and import them in to Address Book or iCal.

If you are saving your vcard or vevent in IE, it will prompt you to save an HTML file - make sure that when you save, you name it appropriately so that you can import it properly.

To close the overlay, click anywhere outside of the window (i.e. in the darkened area).

The Logic

If anyone is interested, here’s roughly what is happening behind the scenes for the bookmarklet to work:

  1. Load external module: jQuery.
  2. Load second external module: microformats.js (the main module).
  3. Apply a new inline CSS styles (because IE doesn't support CSS style sheet injection).
  4. Load the new HTML for the overlay.
  5. Search for vCard and vCalendar classes and capture all the information according to the guidelines laid out by Microformats††.

† Because jQuery saves so much coding time, and it’s a superb library to use.

†† The bookmarklet currently only supports hCards and hCalendars, but should be straight forward to support more formats where appropriate.

Known Issues

A lot of cross site testing took place for this bookmarklet, so I do know of a few places it doesn’t work.

I’ve seen a problem where the overlay simply doesn’t appear. I think this may be due to the site also using Prototype (since I’m using jQuery which may be conflicting). I have only seen this occur on Multipack members page.

Since hCards and hCalendars are defined using classes, a page with a lot of markup can take a number of seconds to parse all the information.

I have tried to parse all the different fields in the hCards and hCalendars, but I may have missed a few, particularly in the hCard. All sites I tested had all the information correctly imported.

If you see any other issues or have any suggestions, or just feedback, please feel free to leave a comment below.

Comments

New comment Jon Hicks said on September 25, 2006:

Lordy! This is amazing! You’ve done a cracking job on this, thanks!

New comment chris messina said on September 25, 2006:

Brilliant! Can’t wait to throw this into a few special projects I’ve got going on… :)

New comment Remy said on September 25, 2006:

There’s a few small issues with the date format in the hCalendars, in particular when the dtstart is just the date without the time.

Should be simple to fix though.

New comment Tomm Eriksen said on September 25, 2006:

I can’t seem to get a title on hcalendar events from upcoming.org, for instance: http://upcoming.org/event/102697/

I’m using Safari on MacOSX 10.4.7.

Great work BTW!

New comment Tom said on September 26, 2006:

Very well done, perhaps it would work well with the highlighting CSS stylesheet, e.g. All Microformats are highlighted and then this dialog is revealed when you select one.

New comment Frances Berriman said on September 26, 2006:

I really like this tool! It looks good, it works well. Thanks :)

New comment Brad Dielman said on September 26, 2006:

Using my work computer (WinXP with MS Outlook as the vCard reader). It works great in Firefox/Win, except when trying to save the hCard. hCalendar events save just fine (Outlook recognizes the iCal format), but for some reason it can’t read the hCard. Any suggestions, maybe I’m missing something (besides a Mac, of course).

New comment Shani elharrar said on September 26, 2006:

it’s good and work’s well.

New comment Krzychu Danek said on September 26, 2006:

This looks really great. I mean it would be great to use it not only as a bookmarklet in ones browser but also as an additional functionality on any site offering data marked as hCard or hCalendar. Have you consider licensing it by any chance (or maybe you already have but I’ve missed something)?

I’ve been marking my clients pages [hCard for contact data] without even bothering to tell them. In 90% of all times it is not worth the trouble of explaining what is that I am actually doing. Still, such a nice feature almost out of the box would be a great addition.

New comment pauldwaite said on September 27, 2006:

Awesome.

I’ve had a go with it in Camino. It works great, except (fairly obviously) downloading vCards. Camino saves them as a file called “download”, meaning you have to manually add ”.vcf” to get Address Book to work with them.

No idea if Camino provides anything similar to Safari’s “data:” mock-protocol.

And now, if I may go off on a tangent…

I think for mass adoption of something like this, the functionality either needs to get implemented natively in the most popular browsers, or needs to get implemented directly on web sites. And for automatic adding to calendar/address book, I guess we’ll definitely need browsers to understand what to do with .vcf etc. files.

New comment Snowflake Seven said on September 27, 2006:

Great tool, thank you for making it.

I don’t know enough about scripting to evaluate the conflict, but I noticed an odd interaction between the bookmarlet and <a href=”http://www.newsvine.com”>Newsvine</a>.

Given that there is currently no microformat data on Newsvine this is not really an issue, but it is an interesting behavior that may cause problems with other sites.

Issue:
The bookmarklet alert window appears and the website is grey-ed out as intended, but the website navigation bar is layered over the top of the bookmarlet alert window.

I’m guessing their is something going on with the <code>z-index</code>.

New comment Thijs van der Vossen said on September 27, 2006:

Excellent work. An explicit close button or link in the top right corner would make this even easier to use.

New comment der-matthias said on September 27, 2006:

Luckily I have this IE 6 at work and can test the bookmarklet ;-) It does not work for me, though.

Otherwise, this looks great, thank you! Well done!

I’ll try it on Firefox later.

New comment Todd Sieling said on September 27, 2006:

Very nice!

New comment Ryan Miglavs said on September 29, 2006:

Holy crap-my-pants, this is amazing! Beautiful work, my friend.

This sort of thing makes me feel much better about adding Microformats to some of my recent sites.

Thank you.

New comment Remy said on October 1, 2006:

I’ve tweaked the interface to also include a close button as suggested and given the background a bit of a gradient.

New comment Michel said on October 2, 2006:

You don’t accpet short date like YYYYMMDD, although it seems to be a valid format.

New comment kazu said on October 6, 2006:

I think this bookmarklet is great, but I found one funny issue: after showing overlay, it seems to add characters of ”#?” to URL in the address bar. I just tested using Safari2, so I don’t know about other browsers.

New comment Remy said on October 7, 2006:

The ”#?” is the href of the links in the overlay. This allows me to add javascript onclick functions without breaking the XHTML of the web page.

Also – the ”#?” doesn’t/shouldn’t change the page in any way what so ever. Let me know if you see different and I’ll try to resolve it.

Thanks.

New comment Remy said on October 8, 2006:

@Michel – the problem wasn’t in the short date format, but with the offset as you said in your email. The fix also have been applied to test for the offset first. Thanks.

New comment Andy Westley said on October 18, 2006:

Any chance you could expand the functionality to extract hreview items? Tails in firefox doesn’t work with MacOSX, but does handle reviews. Not seen Tails in action but your bookmarklet is pretty damn slick.

New comment Wesley Smith said on December 31, 2006:

I’m no guru in this area but it would appear to me that a good combination would be to put together some client side css/java script combo. CSS takes care of the detection and displays the appropriate icon in the lower right or upper right corner. Clicking on the icon would call your script and there you have it. I would build it myself but I’m way too slow in the implementation department.

New comment Henrique Costa Pereira said on January 26, 2007:

Hey, take a look in my website and see what it happens! (http://www.revolucao.etc.br/) I implement microformats anywhere, but when I click in the bookmaklet, the overlay window apears in the footer of my site without any style! Do you know what it is?

New comment Henrique Costa Pereira said on January 26, 2007:

Hey, I discover why this script does not work in Adactio and why was not working in my website. Was the mime type: application/xhtml+xml. I change that in my site and the problem is gone… Make an update in your post about that!

Your script and idea is very cool! Congratulations!

New comment Garold said on February 20, 2007:

Hi! Very nice content. Interesting how can you do all that? I’m trying to build my site, but have no success for now((.

New comment Richard said on March 3, 2007:

Great Code.

One small issue – if you run this more than one time it add the script links to the DOM. It might be better to enscapsulate all the business inside one function and in the bookmarklet test for the function. If the function exists run it, but if not load the script into the DOM.

I am actually using this as the basis of a screen scrapper – I need something that displays part of a page on a particular site, reformmated as text.

cheers

New comment Richard said on March 3, 2007:

update: I added the ids for the script and CSS to the remove function

New comment Jacob Wyke said on April 18, 2007:

Just found this and am blown away by it…it’s such a great tool. Thanks!

New comment Marnen Laibow-Koser said on April 22, 2007:

Hey, this is just awesome. I’m just getting into using microformats, and this is an excellent tool.

One feature request: the bookmarklet doesn’t seem to parse <span class=”uid”> into a UID: field. Can you add that?

New comment Mark McLaren said on May 2, 2007:

Hi,

I thought I would have a bash at making this work in IE. I have managed to get something working (although not yet perfect). My instance just supports hCards at the moment (not hCalendar). What I did:

  • I updated jQuery to the latest version
  • Changed it to use real graphic files rather than the data URI scheme
  • Modified the way that stylesheets are dynamically added for IE
  • Altered the way that vCards are saved for IE

http://mark.mclaren.googlepages.com/hcard.html

New comment Remy Sharp said on May 2, 2007:

@Mark – awesome work. I actually thought it wouldn’t be possible because of the way I created the vcard on the fly – but I love the ‘decodeURIComponent’ component in IE.

Really, really great work.

There is a bug where there are more than one Microformat on the page – the saved MF chooses the last MF rather than the selected MF (give it a test on http://microformats.org).

I’ll drop you an email offline and if we can nail that bug, I’ll update the latest version of the Microformat bookmarklet and add a credit to you for the IE conversion.

New comment wesley said on May 9, 2007:

What’s the possibility of getting this to work with greasemonkey?

New comment Jared Quinn said on May 14, 2007:

Fantastic booklet, however I am having an issue with multiple addresses (postal and pref) in the way they are displayed in the final outputted vcard (they seem to be merged).

My data is at http://test.collectionsaustralia.net/org/102

New comment Jamie Knight said on June 7, 2007:

hiya,

Nice Work, i have seen this around before, nice to see it in use and at work!

^licks^

Jamieknight & Lion

New comment Marnen Laibow-Koser said on June 18, 2007:

Just so you know, this appears to be broken on the Safari 3 Mac beta (522.11).

New comment Remy Sharp said on June 26, 2007:

@Marnen - thanks for the catch - I've upgraded to Safari 3 and released the bug fix now.

New comment Marnen Laibow-Koser said on June 30, 2007:

Yes, it looks like it's fixed. Thanks!

New comment Sergei Yakovlev said on August 2, 2007:

Great work, Remy!

My only gripe is that it doesn't work properly with OS X Address Book and Unicode.

The problem is that Address Book currently works only with UTF-16-BE encoded vCards. Specifying ;CHARSET=UTF-8 or ENCODING=UTF-8 doesn't work.

Given that most webpages are in UTF-8, an UTF-8 to UTF-16-BE conversion is needed. It could be performed in Javascript or, perhaps, there is already a nice web service for doing that (perhaps this one: http://www.iconv.com/iconv.htm). Also, you might want to use jQuery's $.browser.safari to only perform this conversion for Safari users.

Heck, maybe I should do this myself and give you a patch. Please contact me.

Cheers!

New comment Simon Bruce said on October 10, 2007:

Hi,

This code is fantastic. The only problem I can see with it is when it is ignoring the abbr tag (in some places) such as

<abbr title="United Kingdom">UK</abbr>

It is placing UK in the country field rather than United Kingdom. Thanks for a very informative piece of code.

New comment David Hall said on December 22, 2007:

Thanks for this, I'm a late comer to microformats, but it's working nicely in iCab v4ß - but not in Demeter...

New comment Lukas said on January 24, 2008:

Hmm flickr does not seem to parse right with this bookmarklet ..

http://www.flickr.com/photos/till/2198002194/

New comment CD said on February 15, 2008:

One comment: to close the overlay via 'esc' key would be kewl!

New comment Emir said on February 20, 2008:

Thanks for the wonderful bookmarklet.

I'm having an issue where the exported .vcf does not export fist and last names properly. I'm no sure whether it's my markup or the bookmarklet. I'm suspecting it's the latter since it works fine with Tails for Firefox, and my example is taken directly from the hCard creator.

Here's my test site. In exported .vcf both "Barrrettt, Fred" get put as a family-name and given-name is left blank. Expected: "Fred" should be in given-name and "Barrrett" should be in family-name.

Any clue? (name deliberately misspelled)

New comment Nicolas Hoizey said on March 7, 2008:

Hi,

This is wonderful!

Is there any chance to see additionnal microformats supported any time?

Thanks anyway.

New comment Steven said on March 14, 2008:

The vcards don't seem to handle line breaks correctly, see:
http://www.beckdecorso.com/index.html

Also, I found this awesome bookmarklet while looking for a greasemonkey script to insert microformats into pages - then i could download them! Kind of like data detectors in os x mail.

New comment Remy Sharp said on March 14, 2008:

@Emir - thanks for pointing that bug out. I've fixed that bug now, and tested it on your site.

@Steven - also thanks - I've stripped line breaks from single line fields, i.e. from name, etc. but not from address.

New comment Michael said on June 11, 2008:

Great tool. But somehow clicking on a link in Safari does not add it to the Address Book. It only gets saved as Downloadedfile.vcf

Any hint how to change this?

Thanks, Michael

Post your own comment
  • This comment form supports limited Markdown entry.
  • Please wrap code examples in <pre><code>
  • Please note that your e-mail will not be displayed on this page. We will never pass on your details and your information will be kept private.
Back to top