MozillaZine

Is it possible to access AddOns from outside via cmdline?

User Help for Mozilla Firefox
pxstein
 
Posts: 29
Joined: August 8th, 2019, 2:13 am

Post Posted April 3rd, 2020, 12:11 am

Is it in general possible to access installed AddOns from outside Firefox from (WinOS) command line?

Or is this never possible?

If it is possible:
Can someone give me an example AddOn?

Thank you
Peter

morat
 
Posts: 3933
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 3rd, 2020, 7:06 am

What do you mean by access an addon?

You can open an addon page using the command line.

firefox.exe "moz-extension://<internal uuid>/example.html"

You cannot create a command line handler with web extensions, like you could with legacy extensions.

Command Line Handler
http://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICommandLine
http://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICommandLineHandler
http://developer.mozilla.org/en/Chrome/Command_Line

I never tried creating a command line handler using the autoconfig files.

Maybe you could use the "command-line-startup" observer notification.

Reference
http://searchfox.org/mozilla-release/search?q=handleFlag&case=true

pintassilgo
 
Posts: 187
Joined: August 30th, 2013, 3:50 pm

Post Posted April 3rd, 2020, 12:21 pm

pxstein wrote:Can someone give me an example AddOn?

QuickFox Notes, text editor with tabs.

You can open the text editor without opening Firefox itself with the parameter -qfn, for instance:

C:\Mozilla\Firefox\firefox.exe -qfn

Tested in Firefox 75.

morat
 
Posts: 3933
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 4th, 2020, 7:58 am

@pintassilgo

Nice example.

I get a "ChromeUtils is not defined" error when trying to create a command line handler using the autoconfig files.

Any suggestions?

* <install directory>\defaults\pref\autoconfig.js

Code: Select all
pref("general.config.sandbox_enabled", false);
pref("general.config.filename", "mozilla.cfg");
pref("general.config.obscure_value", 0);

* <install directory>\mozilla.cfg

Code: Select all
// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.setTimeout(function () {
    try {
      Services.console.logStringMessage("observer, browser-delayed-startup-finished");
    } catch (e) {
      Components.utils.reportError(e); // [check] Show Content Messages
    }
  }, 10);
}, "browser-delayed-startup-finished", false);

var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
var categoryManager = Components.classes["@mozilla.org/categorymanager;1"].
  getService(Components.interfaces.nsICategoryManager);

function TweaksCommandLineHandler() {}

TweaksCommandLineHandler.prototype = {
  classID: Components.ID("{12345678-1234-1234-1234-123456789012}"),
  classDescription: "Tweaks Command Line Handler",
  category: "command-line-handler",
  entry: "m-tweaks",
  contractID: "@mozilla.org/commandlinehandler/general-startup;1?type=tweaks",
  _xpcom_categories: [{category: this.category, entry: this.entry}],
  factor: XPCOMUtils._getFactory(TweaksCommandLineHandler),
  QueryInterface: ChromeUtils.generateQI([Components.interfaces.nsICommandLineHandler]),
  register() {
    if (registrar.isCIDRegistered(this.classID)) {
      return;
    }
    registrar.registerFactory(this.classID, this.classDescription, this.contractID, this.factory);
    categoryManager.addCategoryEntry(this.category, this.entry, this.contractID, false, true);
  },
  unregister() {
    categoryManager.deleteCategoryEntry(this.category, this.entry, this.contractID, false);
    registrar.unregisterFactory(this.classID, this.factory);
  },
  handle: function (commandLine) {
    try {
      var param = commandLine.handleFlagWithParam("example", false);
      if (param) {
        if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
          Services.console.logStringMessage("handler, STATE_INITIAL_LAUNCH, param = " + param);
        }
        if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
          Services.console.logStringMessage("handler, STATE_REMOTE_AUTO, param = " + param);
        }
        commandLine.preventDefault = true;
      }
    } catch (e) {
      Components.utils.reportError(e); // error with flag without param
    }
    if (commandLine.handleFlag("example", false)) {
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
        Services.console.logStringMessage("handler, STATE_INITIAL_LAUNCH without param");
      }
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
        Services.console.logStringMessage("handler, STATE_REMOTE_AUTO without param");
      }
      commandLine.preventDefault = true;
    }
  },
  helpInfo: "",
};

TweaksCommandLineHandler.prototype.register();

Deploying Firefox in an enterprise environment
http://developer.mozilla.org/Firefox/Enterprise_deployment_before_60

Observer Notifications
http://developer.mozilla.org/docs/Mozilla/Tech/XPCOM/Observer_Notifications

pintassilgo
 
Posts: 187
Joined: August 30th, 2013, 3:50 pm

Post Posted April 4th, 2020, 1:08 pm

I get a "ChromeUtils is not defined" error when trying to create a command line handler using the autoconfig files.

Any suggestions?

Code: Select all
Cu.importGlobalProperties(['ChromeUtils']);


And you made a typo there:
factor: XPCOMUtils._getFactory(TweaksCommandLineHandler),

factory: XPCOMUtils._getFactory(TweaksCommandLineHandler),

morat
 
Posts: 3933
Joined: February 3rd, 2009, 6:29 pm

Post Posted April 4th, 2020, 8:16 pm

@pintassilgo

Thank you kindly. I got it working.

* <install directory>\mozilla.cfg

Code: Select all
// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

Components.utils.importGlobalProperties(["ChromeUtils"]);

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.setTimeout(function () {
    try {
      Services.console.logStringMessage("observer, browser-delayed-startup-finished");
    } catch (e) {
      Components.utils.reportError(e); // [check] Show Content Messages
    }
  }, 10);
}, "browser-delayed-startup-finished", false);

// firefox.exe --abc            // not tested
// firefox.exe --abcdata "test" // not tested

// FirefoxPortable.exe --abc            // observer succeeds at startup, handle fails at startup, handle succeeds with warning
// FirefoxPortable.exe --abcdata "test" // observer succeeds at startup, handle fails at startup, handle succeeds with warning

// Warning: unrecognized command line flag -profile
// view-source:resource:///modules/BrowserContentHandler.jsm

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var commandLine = aSubject;
  try {
    var param = commandLine.handleFlagWithParam("abcdata", false);
    if (param) {
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
        Services.console.logStringMessage("observer, STATE_INITIAL_LAUNCH, param = " + param);
      }
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
        Services.console.logStringMessage("observer, STATE_REMOTE_AUTO, param = " + param);
      }
    }
  } catch (e) {
    Components.utils.reportError(e); // error with flag without param
  }
  if (commandLine.handleFlag("abc", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.console.logStringMessage("observer, STATE_INITIAL_LAUNCH without param");
    }
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
      Services.console.logStringMessage("observer, STATE_REMOTE_AUTO without param");
    }
  }
}, "command-line-startup", false);

var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
var categoryManager = Components.classes["@mozilla.org/categorymanager;1"].
  getService(Components.interfaces.nsICategoryManager);

function TweaksCommandLineHandler() {}

TweaksCommandLineHandler.prototype = {
  classID: Components.ID("{12345678-1234-1234-1234-123456789012}"),
  classDescription: "Tweaks Command Line Handler",
  category: "command-line-handler",
  entry: "m-tweaks",
  contractID: "@mozilla.org/commandlinehandler/general-startup;1?type=tweaks",
  _xpcom_categories: [{category: this.category, entry: this.entry}],
  factory: XPCOMUtils._getFactory(TweaksCommandLineHandler),
  QueryInterface: (XPCOMUtils.generateQI || ChromeUtils.generateQI)([Components.interfaces.nsICommandLineHandler]),
  register() {
    if (registrar.isCIDRegistered(this.classID)) {
      return;
    }
    registrar.registerFactory(this.classID, this.classDescription, this.contractID, this.factory);
    categoryManager.addCategoryEntry(this.category, this.entry, this.contractID, false, true);
  },
  unregister() {
    categoryManager.deleteCategoryEntry(this.category, this.entry, this.contractID, false);
    registrar.unregisterFactory(this.classID, this.factory);
  },
  handle: function (commandLine) {
    try {
      var param = commandLine.handleFlagWithParam("abcdata", false);
      if (param) {
        if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
          Services.console.logStringMessage("handle, STATE_INITIAL_LAUNCH, param = " + param);
        }
        if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
          Services.console.logStringMessage("handle, STATE_REMOTE_AUTO, param = " + param);
        }
        commandLine.preventDefault = true;
      }
    } catch (e) {
      Components.utils.reportError(e); // error with flag without param
    }
    if (commandLine.handleFlag("abc", false)) {
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
        Services.console.logStringMessage("handle, STATE_INITIAL_LAUNCH without param");
      }
      if (commandLine.state == Components.interfaces.nsICommandLine.STATE_REMOTE_AUTO) {
        Services.console.logStringMessage("handle, STATE_REMOTE_AUTO without param");
      }
      commandLine.preventDefault = true;
    }
  },
  helpInfo: "  --abc                Test without param" + "\n" +
            "  --abcdata <string>   Test with    param" + "\n",
};

TweaksCommandLineHandler.prototype.register();

Return to Firefox Support


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 13 guests