Saturday, June 16, 2007

Changing the PATH from a Firefox extension



Since (almost) everything is ready (even my new cool XUL sidebar for recommendation list), I must deal with the install process, don't I?
Everything's ok, all the things are in their place (thanks to the .xpi packaging) except for the BDB XML dlls that have to be visible to the jvm in the browser. We have two options: either we copy the dlls to Program Files/Mozilla, either we set the PATH environment variable to extension directory, where Firefox unzips the .xpi.
Yep, I chose the second...
It's easy to execute a batch file from a firefox extension, you just use a

Components.classes["@mozilla.org/process/util;1"] .createInstance(Components.interfaces.nsIProcess)


initialize it with your script and then run it! (actually, if it wasn't for the extension developer's extension code, I wouldn't have known that now...)

But... how should we write the script? Since I couldn't find out how to execute a script at the moment of the extension install (that is, only once in the lifetime of the extension), I had to run it every time I open the browser so the script is a little more complicated: it must check the PATH for the existence of the directory path I want to add, and, if it's not there, put it. I found out from Merlyn that I can write a batch file in Javascript and, with a little help from Microsoft Tech Net I managed to produce this file:

//command line arguments
var args = WScript.Arguments
var dir = args(0);
//get the env vars
var objShell = WScript.CreateObject("WScript.Shell");
var colUsrEnvVars = objShell.Environment("User");

//TODO: test if path exists, else, create it
var path = colUsrEnvVars("PATH");

//TODO: should use regular expressions
var pathToSearch = ";" + path + ";";
var dirToSearch = ";" + dir + ";";
var index = pathToSearch.indexOf(dirToSearch);

if (index < 0) {
WScript.echo("update...");
var newValue = path + ";" + dir;
colUsrEnvVars("PATH") = newValue;
}

called from this bat:

set DIR_NAME=%1
set SCRIPT_PATH=%2
cscript //nologo "%SCRIPT_PATH%" "%DIR_NAME%"

with DIR_NAME and SCRIPT_PATH passed from the firefox code to match the firefox extension installation paths and the path to the js script.

All fine, until I got the wonderful error:

Execution of the Windows Script Host failed. (Not enough storage is available to complete this operation. )

but thanks God, other people got it too, before myself.

Now it sets the PATH but I still have some issues: the update takes oooooh so long (I kind of stare at the bloody console waiting for it to finish) and, at the first run (when PATH gets actually set), the jvm does not 'see' the PATH, it still fails (maybe it loads before I get to set the path)... A browser restart does it, though, so, until I have some spare time, I guess it's ok in the current state...

P.S: ignore all the "we"'s in this post: I'm actually alone in this, but I got used to the "we" from writing the paper associated to this work.

1 comment:

Unknown said...

Your nerd score has just reached level 85 :D