eBlogzilla

Friday, March 19, 2010

How To Call Java From XPCOM

It is quite easy to call Java methods directly from the extension. You can find a lot of information how to do that here. But there is no information how to call java from JavaScript XPCOM. java keyword is not available here.

There is one trick that allow you to get reference on java variable from XPCOM:
const Cc = Components.classes;
const Ci = Components.interfaces;

var java = Cc["@mozilla.org/appshell/window-mediator;1"].
             getService(Ci.nsIWindowMediator).
             getMostRecentWindow("navigator:browser").java;
Very simple, but not very obvious.

Thursday, February 18, 2010

How to install search plugin together with your extension

A lot of modern sites has own search system. Such system usually performs search on a site and provide search result to a user in accordance with his request.

If you are working on some extension for such site, you can also create and install search plugin for the site.

There are two search plugin formats supported by Firefox:
MozSearch and OpenSearch. Hope that above links will help you to create necessary search plugin.

Then you need to distribute it with your extension. You should do two basic things:
1. Create searchplugins folder in the root of your extension.
2. Add your search plugin to this folder.

So, inside XPI your searchplugins folder will be located on the same level with chrome.manifest and install.rdf files.

There is also one small trick that can be used in your extension. You can select your installed search plugin as default search provider on the first run of your extension. To do that simply add two lines of code:
  var sb = document.getElementById("searchbar");
  sb.searchService.currentEngine = sb.searchService.getEngineByName("YourEngineName");

Wednesday, February 10, 2010

How to change size of image for toolbarbutton on the fly

Sometimes you need to use for toolbarbutton an image which is returned by some third-party service. And it's size can be much greater than you need:

<toolbarbutton id="my-btn-id" label="My Button" image="http://somesite.com/images/img.png"/>

In this case you need to add one line of code in CSS file:
#my-btn-id .toolbarbutton-icon {
width: 16px;
height: 16px;
}
This simple trick will allow you to get toolbarbutton image with necessary size.

Tuesday, February 2, 2010

Header for richlistbox component

Using of header in listbox XUL control is quite common task. But there is no clear information about ability to use header in richlistbox control. I've found quite obvious but undocumented solution for this problem. The next short example will show how to add header to richlistbox control.
<richlistbox rows="10" flex="1">
<listhead>
<listheader label="My header" flex="1"/>
</listhead>
<richlistitem>
<label value="Item1"/>
</richlistitem>
<richlistitem>
<label value="Item2"/>
</richlistitem>
</richlistbox>

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);