Start extension from command line switch
-
- Posts: 1
- Joined: January 9th, 2006, 12:22 pm
Start extension from command line switch
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 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>
-
- Posts: 582
- Joined: July 12th, 2004, 7:20 pm
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?
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?
-
- Posts: 18
- Joined: December 27th, 2005, 9:20 am
Starting firefox -myapp from the command line
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.
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.
-
- Posts: 18
- Joined: December 27th, 2005, 9:20 am
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;
}
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;
}
-
- Posts: 0
- Joined: December 31st, 1969, 5:00 pm
A much simpler way is to launch it through a pseudo-URL:
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).
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).
-
- Posts: 18
- Joined: December 27th, 2005, 9:20 am
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.
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.
-
- Posts: 0
- Joined: December 31st, 1969, 5:00 pm
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).
-
- Posts: 1
- Joined: January 15th, 2013, 10:41 am
Re: Start extension from command line switch
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.
- James
- Moderator
- Posts: 28006
- Joined: June 18th, 2003, 3:07 pm
- Location: Made in Canada
Re: Start extension from command line switch
Holy Old Thread Batman!. Locking this ancient thread.