HOWTO: Build your Own RDFa Bookmarklet

You can build your own bookmarklet. Here's how the simple CC bookmarklet is built. There are two steps: first, the javascript file that encompasses your functionality, and second, the bookmarklet that invokes your Javascript file.

Goal: a bookmarklet which, when clicked, looks for CC metadata. If it doesn't find it, it just says so. If it does find it, it asks the user if she wants to see the CC license in question. If so, it redirects the page to the CC license.

The Javascript File

Start with the template. Note the following section of the code:

RDFA.CALLBACK_DONE_PARSING = function() { // // Your Code Goes Here // }

That's the main callback function that the RDFa parsing code will call when it's done parsing.

We want to find the cc:license property that applies to the current URL. The first thing we need to do is represent the cc:license property name:

var cc = new RDFA.Namespace('cc',''); var cc_license = new RDFA.CURIE(cc,'license');

Then, we just ask for the list of triples that pertain to the current URL (the empty string), and the property defined above:

var triple = RDFA.getTriples('',cc_license);

If there are no such triples, triple is null. If there are, triples is an array of triple data structures. Thus, if triple is null, we alert the user that there is no CC license. If it's non-null, we ask the user if she wants to see the license. If so, we set the browser's location to the object of the triple, which is the CC license.

if (triple) { if (confirm('This document is licensed under a CC license. Want to see it?')) { document.location = triple[0].object; } } else { alert ('no cc license'); }

That's it. The resulting file is then cc.js [2006-05-22].
(Note that cc.js checks for more than cc:license, it also checks for xhtml:license.)

The Bookmarklet

The role of the bookmarklet is simply to add a SCRIPT element to the head of the document that will load up your Javascript file.

Start with the bookmarklet template and save it as an HTML file.

Find the javascript piece that reads:


Replace <<<YOUR_JS_URL>>> with the URL of the Javascript file you created earlier.

Load up the HTML page you just created in a browser, drag the link to your bookmarks bar, and you're done.