Friday, September 05, 2008

My First Ubiquity command

It redirects the current tab to the root of the current domain.  - I find this helpful when there isn't a home link on a website.


/// http://blog.stevenlevithan.com/archives/parseuri
/*
 parseUri 1.2.1
 (c) 2007 Steven Levithan 
 MIT License
*/
function parseUri (str) {
 var o   = parseUri.options,
  m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
  uri = {},
  i   = 14;

 while (i--) uri[o.key[i]] = m[i] || "";

 uri[o.q.name] = {};
 uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  if ($1) uri[o.q.name][$1] = $2;
 });

 return uri;
};

parseUri.options = {
 strictMode: false,
 key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
 q:   {
  name:   "queryKey",
  parser: /(?:^|&)([^&=]*)=?([^&]*)/g
 },
 parser: {
  strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
 }
};


function cmd_domain_home() {
// heavily ripped from Utils.openUrlInBrowser

 var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);

  var browserWindow = windowManager.getMostRecentWindow("navigator:browser");

  var browser = browserWindow.getBrowser();

  var uriParts = parseUri(browser.mCurrentBrowser.currentURI.spec);

  browserWindow.loadURI(uriParts.protocol + "://" + uriParts.host + "/", null, null, false);

}

0 comments:

Post a Comment

I get a lot of comment spam :( - moderation may take a while.