Wanted: ProductName/Version

Were down to one reporter bug that needs to be fixed ASAP. Were not always getting the correct Product. By product I mean productName/productVersion. For example “Firefox/1.0+” or “SeaMonkey/1.8b2”. The below code is what’s currently being used. general.useragent.extra.firefox is not exactly reliable, even less so than I originally thought. Leaving me with not many options. So my challenge is this: build an unbreakable function (or as close to it as possible) that will return the correct product/version for us to use. Should work with SeaMonkey > 1.7 and Firefox 1.0 and later. nsIXULAppInfo can’t be relied on 100% as it’s not frozen.

function getProduct() {
  // only works on Gecko 1.8 and higher
  if (‘nsIChromeRegistrySea’ in Components.interfaces) {
    return ‘SeaMonkey/’+
    Components.classes[‘@mozilla.org/network/io-service;1’]
              .getService(Components.interfaces.nsIIOService)
              .getProtocolHandler(‘http’)
              .QueryInterface(Components.interfaces.nsIHttpProtocolHandler).misc.substring(3);
  }
  // Firefox < 1.0+
  else if (navigator.vendor != ){
    return window.navigator.vendor+‘/’+window.navigator.vendorSub;
  }
  // Firefox 1.0+
  try {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].
                           getService(Components.interfaces.nsIPrefService);
    return prefs.getCharPref("general.useragent.extra.firefox");
  }
  catch(ex) {
    return "Unknown";
  }
}

3 thoughts on “Wanted: ProductName/Version

  1. Here’s a function from Forumzilla that might help. It uses nsIXULAppInfo on Aviary 1.1 apps, the app.id preference on Aviary 1.0 apps, and assumes Seamonkey if neither value is available. It’s designed to distinguish between Seamonkey and Thunderbird but should be easily modifiable to determine if it’s running in Firefox or some other XUL app.

    var gAppName;
    function getAppName() {
        if (gAppName)
            return gAppName;
     
        try {
            // Aviary 1.1 apps store application meta-data in the app info component.
            var appInfo =
              Components
                .classes["@mozilla.org/xre/app-info;1"]
                  .getService(Components.interfaces.nsIXULAppInfo);
            if (appInfo &amp;&amp; appInfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}") {
                gAppName = "Thunderbird";
                debug("determined application to be Thunderbird via nsIXULAppInfo");
                return gAppName;
            }
            debug("could not determine application via nsIXULAppInfo: " + appInfo);
        }
        catch(e) {
            debug(e);
        }
       
        try {
            // Aviary 1.0 apps store application meta-data in a preference.
            var id =
              Components
                .classes["@mozilla.org/preferences-service;1"]
                  .getService(Components.interfaces.nsIPrefService)
                    .getBranch("app.")
                      .getCharPref("id");
            if (id &amp;&amp; id == "{3550f703-e582-4d05-9a08-453d09bdfdc6}") {
                gAppName = "Thunderbird";
                debug("determined application to be Thunderbird via app.id pref");
                return gAppName;
            }
            debug("could not determine application via app.id pref: " + id);
        }
        catch(e) {
            debug(e);
        }
     
        // Non-aviary apps (f.e. Seamonkey) don’t store application meta-data.
        debug("could not determine application; assuming Seamonkey");
        gAppName = "Seamonkey";
        return gAppName;
    }

    *Edit:* robert 5/20/05 3:34 – parse code for readability.

  2. doh! you all make firefox. and you can’t even get your own product’s version number. So much for F/oss.

Leave a Reply

Your email address will not be published. Required fields are marked *