Friday, September 12, 2008

One Crazy Ride

The object of this lab was to complete a task that none of use knew how to do; in short, to do the previously impossible. The specific example was writing commands for Ubiquity. You can find the lab here:

http://zenit.senecac.on.ca/wiki/index.php/Learning_Collaborative_Development_Lab_Fall_2008

Instead of writing one of the four suggested commands like everyone else, I dug around to see what the community was looking for. I found it: the URL noun.

They have a prototype for the URL noun (at bottom of this page), but it was missing a couple items. It would've been awesome if it could hook into awesomebar's data resources asynchronously, but async data transfer isn't simple enough for a novice to complete over a week.

Instead, I chose to improve on the regexp by allowing other protocols (instead of just http, https, and ftp), and adding syntax for subfolders. Here's my current work:



var noun_type_url = {
_name: "Valid URL",

suggest: function( text, html ) {
if (!text)
return [ CmdUtils.makeSugg("http://") ];


if(/^[A-Za-z0-9]+:\/\/([A-Za-z0-9_-]+)(\.([A-Za-z0-9_-]+))*(\/([A-Za-z0-9_-~#=]*))*(\/[A-Za-z0-9_]*\.[A-Za-z0-9_]+)?$/.test(text)){
return [ CmdUtils.makeSugg("valid! " + text) ];
}

else if(/^([A-Za-z0-9_-]+)(\.([A-Za-z0-9_-]+))*(\/([A-Za-z0-9_-~#=]*))*(\/[A-Za-z0-9_]*\.[A-Za-z0-9_]+)?$/.test(text)){
return [ CmdUtils.makeSugg("valid! " + "http://" + text) ];
}

return [ CmdUtils.makeSugg("What are you thinking. :V") ];
}
};

CmdUtils.CreateCommand({
name: "url",
takes: {"your shout": noun_type_url},
preview: function( pblock, theShout ) {
pblock.innerHTML = "Will verify: " + theShout.text;
},
execute: function( theShout ) {
var msg = theShout.text;
displayMessage( msg );
}
})



jono from #ubiquity on irc.mozilla.org was very helpful in this learning process. From the first few minutes of chat, I found out that I wasn't working alone on this noun type and that there are coders willing to review my work.

There's just a little more polish I can put into this code before considering my limits reached. Asynchronous data calls seems to deal with the Ubiquity source code, and compiling is a task for next week!

No comments: