Poshzone.Js
Here is the script that plugs poshZones as a new dataformat into operator. You can download it from the files section at the bottom of this page.
function poshZone(node) {
if (node) {
Microformats.parser.newMicroformat(this, node, "poshZone");
}
}
poshZone.prototype.toString = function() {
if(this.resolvedNode){
return Microformats.poshZone.mapPoshZone(this.resolvedNode)['poshZoneDescription'];
}
return "could not resolve tostring for poshZone";
}
var poshZone_definition = {
mfVersion: 0.8,
mfObject: poshZone,
className: "poshZone",
properties: {
"content": {
virtual: true,
virtualGetter: function(node, parentNode){
return Microformats.poshZone.mapPoshZone(node);
}
}
},//properties
getSpans : function (node){
return this.getChildren(node, 'span');
},
getDivs : function (node){
return this.getChildren(node, 'div');
},
getChildren : function (node, name){
var children = node.childNodes;
var rtnVal = new Array();
for(var i=0; i < children.length ; i++){
if(new String(children[i].tagName).toLowerCase() == name) rtnVal.push(children[i]);
}
return rtnVal;
},
isListLike : function(nodeset){
var classes = new Array();
for(var i=0; i < nodeset.length; i++){
if(classes[nodeset[i].getAttribute('class')]) return true;
classes[nodeset[i].getAttribute('class')] = true;
}
return false;
},
mapPoshZone : function(node){
var rtnMap = new Array();
var spans = this.getSpans(node);
for(var i=0; i < spans.length; i++)
rtnMap[spans[i].getAttribute('class')] = spans[i].innerHTML;
var subDivs = this.getDivs(node);
var useList = this.isListLike(subDivs);
for(var i=0; i < subDivs.length; i++){
if(useList)
rtnMap.push(this.mapPoshZone(subDivs[i]));
else
rtnMap[subDivs[i].getAttribute('class')] = this.mapPoshZone(subDivs[i]);
}
return rtnMap;
}
}//poshZone_definition
Microformats.add("poshZone", poshZone_definition);
//shows the serialized poshZone content node in an alert
var poshAction = {
description: "Posh action debug",
shortDescription: "PoshDbg",
scope: {
semantic: {
"poshZone" : {
custom : "['poshZoneDescription']"
}
}
},
getActionName: function(semanticObject, semanticObjectType, propertyIndex) {
if (semanticObjectType == "poshZone") {
try {
if( !eval("Microformats.poshZone.mapPoshZone(semanticObject.resolvedNode)"
+this.scope.semantic[semanticObjectType].custom) )
return;
}catch (ex) {
return;
}
return "";
}
},
doAction: function(semanticObject, semanticObjectType) {
function serialize(map){
var s = "";
for( key in map ){
s += " , "; s += key; s += ' = ';
if( map[key] instanceof Array){
s += '{';
s += serialize(map[key]);
s +='}';
}else{
s += map[key];
}
}//for
return s;
}//serialize
if (semanticObjectType == "poshZone") {
var poshContent = semanticObject.content;
alert(serialize(poshContent));
}
}//do action
};
SemanticActions.add("poshAction", poshAction);
Missing Features
- I would like to add support for specifying compound scope. Something like
scope: {
semantic: {
"poshZone" : {
custom: "['movie']['movieName'] & ['movie']['movieYear']"
}
}
- Return ISO formatted dates to user scripts.
page revision: 4, last edited: 17 Jan 2008 03:44