So let’s talk about what Firefox 3 will hopefully offer for developers. The goal is that extension authors will have an API to access microformatted data directly without worrying about parsing. In addition, there will be a model for adding additional microformats, as well as adding semantic actions (more about that in a minute). Here’s what the API looks like right now.
Any microformat can be instantiated from a DOM Node:
var hcard = new hCard(DOMNode);
Once the microformat has been instantiated, members can be accessed directly, like hcard.fn. If the property is defined as an array in the microformat specification, it is an array in the newly created object. A pointer to the original DOM node is also stored in this object if you want to do anything to the microformat node on the glass.
Microformat objects can be queried from a document:
var hCards = Microformats.get(document, "hCard");
I need a better name for this one I think. Given a document and a microformat type, this API returns an array of objects that represent each microformat in the document. I’m not sure if this should go through all the child frames and return every microformat in every sub document. I have to think about that.
If you have DOM Nodes, you might need some information about them. The scenario here is what if you have a DOM Node and you don’t know if it is a microformat or not? What APIs could help you?
Microformats.isMicroformat(DOMNode)
This function tells you if the specific DOM Node you passed in is a microformat. It returns a boolean.
Microformats.getParentMicroformatNode(DOMNode)
This function gets the first microformat that is a parent of this node. If you pass in a microformat, it still gets the parent. This is important for finding nested microformats.
Microformats.getMicroformatNamesFromNode(DOMNode)
This function returns an array of the microformat types that this node represent.
So here's how you might use those three functions, for instance when a context menu is displayed on a given node:
if (Microformat.isMicroformatNode(node)) {
mfNode = node;
} else {
mfNode = Microformats.getParentMicroformatNode(node);
}
while (mfNode) {
var mfNames = Microformats.getMicroformatNamesFromNode(mfNode);
/* enumerate through names and do something */
mfNode = Microformats.getParentMicroformatNode(mfNode);
}
I mentioned semantic actions earlier. I'm still working on how I can create an action model that is easily extensible. In an earlier post, I suggested that actions be implemented like this:
semanticActions.actions.youtube_search_tags = {
description: "Find videos on YouTube",
icon: "http://youtube.com/favicon.ico",
scope: {
semantic: {
"tag" : "tag"
}
},
doAction: function(semanticObject, semanticObjectType) {
if (semanticObject.tag) {
return("http://youtube.com/results?search_query=" + encodeURIComponent(semanticObject.tag));
}
}
};
I'm now trying to decide if I should use a function method, something like:
semanticActions.addAction("youtube_search_tags", youtube_object);
There will be much more on that later.
Anyway, I'm primarily looking for feedback from people. Are these APIs sufficient? Did I miss something obvious? Thanks for the input.