Start extension from command line switch

Talk about add-ons and extension development.
Locked
rsg00usa
Posts: 1
Joined: January 9th, 2006, 12:22 pm

Start extension from command line switch

Post by rsg00usa »

Does anyone know how to start a FF extension from the command line? With extension "helloworld" installed run firefox from the command line like: firefox.exe -helloworld

I tried adding the following code in the helloworldOverlay.xul file. It didn't work:

<script>
function helloworldInit()
{
window.removeEventListener("load", gremlinInit, true);
var cmdLine = Components.classes["@mozilla.org/toolkit/command-line;1"].createInstance(Components.interfaces.nsICommandLine);
var found = cmdLine.findFlag("jsconsole", false);
if (found >= 0) helloworld();
}

window.addEventListener("load", helloworldInit, true);

</script>
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

You can run extensions standalone using XulRunner, not Firefox. Be aware that XulRunner doesn't support everything, though.
Bloodeye
Posts: 582
Joined: July 12th, 2004, 7:20 pm

Post by Bloodeye »

Command Line Args:
http://kb.mozillazine.org/Command_line_arguments

You can open a chrome window from the command line. Not exactly sure what your trying to do? If your helloworldOverlay is a browser overlay, then any code in your helloworldInit() function would execute when loaded?
phreed
Posts: 18
Joined: December 27th, 2005, 9:20 am

Starting firefox -myapp from the command line

Post by phreed »

I presume you are talking about Firefox 1.5 (earlier versions were different).
The way you do this is to create a 'component' as part of your package.
<extension>
install.rdf
chrome.manifest
chrome/<your app>.jar
components/<your app command line component>.js

This component is installed each time you start firefox.
A good (i.e. short) example of such a component is that of jsconsole.
(/usr/lib/firefox/components/jsconsole-clhandler.js on Debian)

At a minimum you will need to change:
clh_contractID
clh_CID
clh_category

You will also want to adapt the 'jsConsoleHandler.handle = function clh_handle(cmdLine)'.
I use the '@mozilla.org/appshell/appShellService;1' component to open my application.
phreed
Posts: 18
Joined: December 27th, 2005, 9:20 am

Post by phreed »

Here is the code...
There are some places where 'myapp' must not contain any 'weird' characters.
(Weird characters include '-' I spent the better part of the day tracking that one down.)
==========================

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:sw=4:sr:sta:et:sts: */

/*
* -myapp commandline handler; starts up My App.
*/

const nsIAppShellService = Components.interfaces.nsIAppShellService;
const nsISupports = Components.interfaces.nsISupports;
const nsICategoryManager = Components.interfaces.nsICategoryManager;
const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
const nsICommandLine = Components.interfaces.nsICommandLine;
const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
const nsIFactory = Components.interfaces.nsIFactory;
const nsIModule = Components.interfaces.nsIModule;
const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher;


var consoleService = Components
.classes['@mozilla.org/consoleservice;1']
.getService( Components.interfaces.nsIConsoleService );

function recordMessage( message ) {
consoleService.logStringMessage("myapp: " + message + "\n");
}


/*
* Classes
*/

const myAppHandler = {
/* nsISupports */
QueryInterface : function clh_QI(iid) {
if (iid.equals(nsICommandLineHandler) ||
iid.equals(nsIFactory) ||
iid.equals(nsISupports))
return this;

throw Components.results.NS_ERROR_NO_INTERFACE;
},

/* nsICommandLineHandler */

handle : function clh_handle(cmdLine) {
var args = new Object();
args.urlopt = false;
try {
var uristr = cmdLine.handleFlagWithParam("myapp", false);
if (uristr) {
args.urlopt = true;
args.url = uristr;
}
}
catch (e) {
recordMessage( "myAppHandler::handle arg has no param" );
}

if (args.urlopt || cmdLine.handleFlag("myapp", false)) {
var appShellClass = Components.classes[ "@mozilla.org/appshell/appShellService;1" ];
var appShellService = appShellClass.getService(nsIAppShellService);
var hiddenWin = appShellService.hiddenDOMWindow;
/*
A XULWindow man be better than a DOMWindow
var hiddenWin = appShellService.hiddenWindow;
*/

recordMessage( "myAppHandler::handle open with argument: " + args.url );
hiddenWin.openDialog("chrome://myapp/content/myapp.xul",
"_blank",
"chrome,menubar,toolbar,status,resizable,dialog=no",
args);
cmdLine.preventDefault = true;
}
},

helpInfo : " -myapp Open the My App.\n",

/* nsIFactory */

createInstance : function clh_CI(outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;

return this.QueryInterface(iid);
},

lockFactory : function clh_lock(lock) {
/* no-op */
}
};

const clh_contractID = "@mozilla.org/commandlinehandler/general-startup;1?type=myapp";
const clh_CID = Components.ID("{2991c315-b871-42cd-b33f-bfee4fcbf682}");
const clh_category = "c-myapp";

const myAppHandlerModule = {
/* nsISupports */

QueryInterface : function mod_QI(iid) {
if (iid.equals(nsIModule) ||
iid.equals(nsISupports))
return this;

throw Components.results.NS_ERROR_NO_INTERFACE;
},

/* nsIModule */

getClassObject : function mod_gch(compMgr, cid, iid) {
if (cid.equals(clh_CID))
return myAppHandler.QueryInterface(iid);

throw Components.results.NS_ERROR_NOT_REGISTERED;
},

registerSelf : function mod_regself(compMgr, fileSpec, location, type) {
compMgr.QueryInterface(nsIComponentRegistrar);

compMgr.registerFactoryLocation(clh_CID,
"myAppHandler",
clh_contractID,
fileSpec,
location,
type);

var catMan = Components.classes["@mozilla.org/categorymanager;1"]
.getService(nsICategoryManager);
catMan.addCategoryEntry("command-line-handler",
clh_category,
clh_contractID, true, true);
},

unregisterSelf : function mod_unreg(compMgr, location, type) {
compMgr.QueryInterface(nsIComponentRegistrar);

compMgr.unregisterFactoryLocation(clh_CID, location);

var catMan = Components.classes["@mozilla.org/categorymanager;1"]
.getService(nsICategoryManager);
catMan.deleteCategoryEntry("command-line-handler", clh_category);
},

canUnload : function (compMgr) {
return true;
}
};

/* module initialisation */
function NSGetModule(comMgr, fileSpec) {
return myAppHandlerModule;
}
old zeniko
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by old zeniko »

A much simpler way is to launch it through a pseudo-URL:

Code: Select all

firefox -url myext:parameters

Then look for a respective URL in window.arguments while the main window is loading (i.e. overlay browser.xul) and close that window before it is actually shown. It's somewhat a hack, but much simpler to achieve (see my Mass Installer extension for an example).
phreed
Posts: 18
Joined: December 27th, 2005, 9:20 am

Post by phreed »

I thought the -url option was removed with firefox v1.0.6!
Something to do with a joint vulnerability with flash.

I had been using a technique similar to the -url approach you present but using -chrome.

firefox -chrome "chrome://myapp/content/myapp.xul?name=value"

see ...
http://forums.mozillazine.org/viewtopic.php?t=359751
...for the code.
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

see my Mass Installer extension for an example

Could you point me to the code in Mass Installer?
ericjung
Posts: 846
Joined: August 4th, 2003, 9:32 am

Post by ericjung »

oops
Last edited by ericjung on January 14th, 2006, 9:02 am, edited 1 time in total.
old zeniko
Posts: 0
Joined: December 31st, 1969, 5:00 pm

Post by old zeniko »

grimholtz wrote:
see my Mass Installer extension for an example

Could you point me to the code in Mass Installer?

If you extract <a href="view-source:jar:jar:http://www.haslo.ch/zeniko/software/massinstaller.xpi!/chrome/massinstaller.jar!/content/massinstaller/massinstaller.js">massinstaller.js</a>, you'll see the following code at the bottom of the file:

Code: Select all

if (window.arguments && /^file:\/+massinstaller:(.+)/.test(window.arguments[0]))
{ // filter arguments for automatic installation
   MassInstaller.autoInstall(RegExp.$1);
   window.arguments.splice(0, 1);
}

This is executed directly when the corresponding XUL file is overlayed (not in the onLoad event) and simply extracts the first argument encountered and processes and removes it, if it starts with "file:massinstaller:" (the file: prefix is necessary as a work-around for the above mentioned limitation introduced in Firefox 1.0.6).
bluelagoon
Posts: 1
Joined: January 15th, 2013, 10:41 am

Re: Start extension from command line switch

Post by bluelagoon »

rsg00usa wrote:Does anyone know how to start a FF extension from the command line? With extension "helloworld" installed run firefox from the command line like: firefox.exe -helloworld

I tried adding the following code in the helloworldOverlay.xul file. It didn't work:

<script>
function helloworldInit()
{
window.removeEventListener("load", gremlinInit, true);
var cmdLine = Components.classes["@mozilla.org/toolkit/command-line;1"].createInstance(Components.interfaces.nsICommandLine);
var found = cmdLine.findFlag("jsconsole", false);
if (found >= 0) helloworld();
}

window.addEventListener("load", helloworldInit, true);

</script>

I believe you can run extensions standalone using XulRunner, not Firefox.
User avatar
James
Moderator
Posts: 27999
Joined: June 18th, 2003, 3:07 pm
Location: Made in Canada

Re: Start extension from command line switch

Post by James »

Holy Old Thread Batman!. Locking this ancient thread.
Locked