eBlogzilla

Friday, October 9, 2009

Firefox add-on for copying containing folder for a downloaded file

I've created small add-on which extends standard context menu in Download Manager. I think that it can be useful for those people which who uses some kinds of file managers like Far, Total Commander and etc.

Usually after downloading of a file I need to open containing folder it my Far manager. But there is no basic way to copy path of the folder which contains a downloaded file. So, I've added one menu item in Download Manager context menu. It allows to do exactly that I need - copy containing folder for a downloaded file.

Hope that it will be useful for someone too. You can download this extension here.

Saturday, September 12, 2009

How to get local IP address

Some time ago I was need to detect local IP address of the machine where my Firefox add-on is installed. I've not found complete example. Finally, I've found the next solution:

var dnsService = Components.classes["@mozilla.org/network/dns-service;1"].
getService(Components.interfaces.nsIDNSService);
var ip = dnsService.resolve(dnsService.myHostName, false).getNextAddrAsString();

Nothing difficult!

Saturday, May 23, 2009

How to add welcome page

Sometimes it will be helpful to open welcome page immediately after first running of an extension. This is quite simple task. But it will allow to make your
extension more user-friendly.

const MY_EXT_PREF_BRANCH = "extensions.myext";
const Cc = Components.classes;
const Ci = Components.interfaces;

var myExt = {
PREF_FIRSTRUN : MY_EXT_PREF_BRANCH + ".firstrun",

PAGE_WELCOME : "http://www.myext.com/welcome",

prefService : Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch),


load : function () {
if (myExt.isFirstRun()) {
setTimeout(myExt.openSuccessPage, 2000);
myExt.prefService.setBoolPref(myExt.PREF_FIRSTRUN, false);
}
}

isFirstRun : function() {
return myExt.getBoolPref(myExt.PREF_FIRSTRUN, true);
},

openSuccessPage : function() {
gBrowser.selectedTab = gBrowser.addTab(myExt.PAGE_WELCOME);
},


getBoolPref: function(prefname, prefdef) {
try {
return myExt.prefService.getBoolPref(prefname);
} catch(e) {
myExt.prefService.setBoolPref(prefname, prefdef);
return myExt.prefService.getBoolPref(prefname);
}
}
}

window.addEventListener("load", myExt.load, false);

Thursday, April 9, 2009

User-friendly uninstall process of an extensions

As a rule you need to perform few steps when user uninstalls your extension.

1. Remove all data that was added by your extension.
2. Open feedback page.

Below you will find some code that performs all these actions:


const MY_EXT_PREF_BRANCH = "extensions.my_ext";
const Cc = Components.classes; 
const Ci = Components.interfaces; 

var my_ext = {
  preferencesService : Cc["@mozilla.org/preferences-service;1"].
          getService(Ci.nsIPrefBranch),
    
  observerService : Cc['@mozilla.org/observer-service;1'].
          getService(Ci.nsIObserverService),

  PLUGIN_UUID : "{a704f080-166f-12de-8c30-0206200c9a66}",

  PAGE_FEEDBACK : "http://mysite.com/feedback",

  uninstallObserver : {
    observe: function(aSubject, aTopic, aData)
    {
       try {
          var item = aSubject.QueryInterface(Ci.nsIUpdateItem);

          if (item.id != my_ext.PLUGIN_UUID) {
            return;
          }

          if (aData == "item-uninstalled") {
            gBrowser.selectedTab = gBrowser.addTab(my_ext.PAGE_FEEDBACK);

            // Remove all properties that was installed by our extension
            my_ext.preferencesService.deleteBranch(MY_EXT_PREF_BRANCH);
          }
        } catch (e) {
      }
    }
  },

  addUninstallObserver : function() {
    my_ext.observerService.addObserver(my_ext.uninstallObserver, "em-action-requested", false);
  }
};

my_ext.addUninstallObserver();

Thursday, January 8, 2009

How to add icon in textbox

Probably, most common place where you can use this trick is a toolbar for some site with search capability. Sure, that it will be nice to have search box on your toolbar like this:




I've found two different ways how to do this. The first way is to use XBL to define new component. It can be quite complicated for those developers who has no previous experience with it. Fortunately, there is another way which is more simple.

  <textbox id="TB-Search"
    minwidth="200" width="200"
    onfocus="tb.searchBoxFocus(this);"
    onblur="tb.searchBoxBlur(this);"
    onkeypress="tb.keyHandler(event);"
  >
    <image id="TB-SearchImage"/>
  </textbox>

CSS definition for search image:

#TB-SearchImage {
  list-style-image: url("http://yoursite.com/favicon.ico");
  padding-right: 2px;
  padding-left: 0px;
  margin: 0px;
  border: 0px;
}

That's it!