Tutorial
Good tutorials on writing user scripts for operator are available at http://www.kaply.com/weblog/operator-user-scripts/
The only difference with a poshZone script is how you define the semantic scope:
If content is markep up as below:
<div class="poshZone"> <span class="poshZoneDescription">Movie: Cold Mountain</span> <div class="movie"> <span class="movieName">Cold Mountain</span>. Made in <span class="movieYear">2003</span>. Watch it. <p>In the waning days of the American Civil War, a wounded soldier (Law) embarks on a perilous journey back home to Cold Mountain, North Carolina to reunite with his sweetheart (Kidman). Based on the novel by Charles Frazier. </p> </div> </div>
then a user script that is interested in movie nodes that have a movie name should have a sematic scope specification as follows:
var rentalq = {
description: "Add to RentalQ on myjavaserver.com",
shortDescription: "RentalQ",
scope: {
semantic: {
"poshZone" : {
custom: "['movie']['movieName']"
}
}
}
The user script action handler can then retrieve the inforamation as below:
var poshContent = Microformats.poshZone.mapPoshZone(semanticObject.resolvedNode);
var body = "subscriberId=user123&password=password&movieName=" +
poshContent['movie']['movieName'] +
"&movieYear=" +
poshContent["movie"]['movieYear'];
Finally here is the complete action handler script that does a POST. Note that support for authentication is yet to come. For now, could choose to prompt the user for credentials.
doAction: function(semanticObject, semanticObjectType, propertyIndex, event) {
//from http://www.kaply.com/weblog/wp-content/uploads/2007/10/fpu.js
if (semanticObjectType == "poshZone") {
var poshContent = Microformats.poshZone.mapPoshZone(semanticObject.resolvedNode);
var body = "subscriberId=user123&password=password&movieName=" +
poshContent['movie']['movieName'] + "&movieYear=" + poshContent["movie"]['movieYear'];
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var referrerURI = ios.newURI("http://www.myjavaserver.com/servlet/bagheera.movierentals.RentalQHomePage", null, null);
var stringStream = Components.classes["@mozilla.org/io/string-input-stream;1"].
createInstance(Components.interfaces.nsIStringInputStream);
if ("data" in stringStream) // Gecko 1.9 or newer
stringStream.data = body;
else // 1.8 or older
stringStream.setData(body, body.length);
var postData = Components.classes["@mozilla.org/network/mime-input-stream;1"].
createInstance(Components.interfaces.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);
if (event) {
var rentalQurl = "http://www.myjavaserver.com/servlet/bagheera.movierentals.RentalQ";
openUILink(rentalQurl, event, undefined, undefined, undefined, postData, referrerURI);
} else {
getBrowser().webNavigation.loadURI(
"http://www.myjavaserver.com/servlet/bagheera.movierentals.RentalQ", 0, referrerURI, postData, null);
return true;
}
}
}
};
page revision: 3, last edited: 17 Jan 2008 03:08





