Start TB in offline mode? - SOLVED

User Help for Mozilla Thunderbird
Post Reply
Burma
Posts: 32
Joined: June 15th, 2007, 12:35 pm

Start TB in offline mode? - SOLVED

Post by Burma »

The portable version has a way to start TB in offline mode. I couldn't find a way to start the regular non-portable version of TB in offline mode. Does anyone know if there is a way to do this? I don't want to do this by turning off my internet connection or disable server checking of individual TB accounts (I have too many). I was hoping for a way of doing this from the command line. Thanks
Last edited by Burma on July 31st, 2022, 9:23 pm, edited 1 time in total.
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode?

Post by morat »

You can configure offline settings with the "Offline Settings" dialog.

i.e. Menu Bar > Tools > Settings > General > Offline

To configure Thunderbird always to start up offline, use the Config Editor to set the preference offline.startup_state to 3.

Outbox - Online and offline states
http://kb.mozillazine.org/Outbox#Online ... ine_states
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode?

Post by morat »

You can use the command-line-startup observer to handle a command-line flag at startup using the autoconfig files. (advanced users only)

e.g.

ThunderbirdPortable.exe --cowabunga
thunderbird.exe --cowabunga

* <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");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  Services.console.logStringMessage("test command-line-startup observer");
  var commandLine = aSubject;
  if (commandLine.handleFlag("cowabunga", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.console.logStringMessage("test STATE_INITIAL_LAUNCH without param");
      var win = Services.wm.getMostRecentWindow("mail:3pane"); // null
      if (win.MailOfflineMgr.isOnline()) win.MailOfflineMgr.toggleOfflineStatus();
    }
  }
}, "command-line-startup", false);
I get an error because the main window isn't ready yet. I don't remember how to listen for the opening of the main window.

Edit:
morat wrote:I don't remember how to listen for the opening of the main window.
I guess you can use the nsIWindowWatcher.registerNotification method.

Reference
http://searchfox.org/comm-esr102/search ... tification
http://searchfox.org/mozilla-esr102/sou ... atcher.idl
Last edited by morat on July 31st, 2022, 7:03 pm, edited 1 time in total.
Burma
Posts: 32
Joined: June 15th, 2007, 12:35 pm

Re: Start TB in offline mode?

Post by Burma »

I can't seem to get autoconfig.js working at all in TB 102.1.0 (64-bit). Could you possibly provide a Hello World type of sample to test that autoconfig.js and mozilla.cfg are functional? Thanks
morat wrote:You can use the command-line-startup observer to handle a command-line flag at startup using the autoconfig files. (advanced users only)

e.g.

ThunderbirdPortable.exe --cowabunga
thunderbird.exe --cowabunga

* <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");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  Services.console.logStringMessage("test command-line-startup observer");
  var commandLine = aSubject;
  if (commandLine.handleFlag("cowabunga", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.console.logStringMessage("test STATE_INITIAL_LAUNCH without param");
      var win = Services.wm.getMostRecentWindow("mail:3pane"); // null
      if (win.MailOfflineMgr.isOnline()) win.MailOfflineMgr.toggleOfflineStatus();
    }
  }
}, "command-line-startup", false);
I get an error because the main window isn't ready yet. I don't remember how to listen for the opening of the main window.
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode?

Post by morat »

Hello World Example:

* <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");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.addEventListener("load", function (event) {
    var doc = event.target;
    var url = doc.location.href;
    if (url == "chrome://messenger/content/messenger.xhtml") {
      chromeWindow.setTimeout(function () {
        try {
          chromeWindow.alert("Hello, world!");
        } catch (e) {
          Components.utils.reportError(e);
        }
      }, 3000);
    }
  }, false);
}, "chrome-document-global-created", false);
Installation directory
http://kb.mozillazine.org/Installation_directory

Thunderbird Portable 32-bit:

* C:\ThunderbirdPortable\App\Thunderbird\defaults\pref\autoconfig.js
* C:\ThunderbirdPortable\App\Thunderbird\mozilla.cfg

Thunderbird Portable 64-bit:

* C:\ThunderbirdPortable\App\Thunderbird64\defaults\pref\autoconfig.js
* C:\ThunderbirdPortable\App\Thunderbird64\mozilla.cfg

Thunderbird 32-bit:

* C:\Program Files\Mozilla Thunderbird\defaults\pref\autoconfig.js
* C:\Program Files\Mozilla Thunderbird\mozilla.cfg

Thunderbird 64-bit:

* C:\Program Files (x86)\Mozilla Thunderbird\defaults\pref\autoconfig.js
* C:\Program Files (x86)\Mozilla Thunderbird\mozilla.cfg

The autoconfig.js file should be in the same folder as the channel-prefs.js file.

The mozilla.cfg file should be in the same folder as the thunderbird.exe executable.
Burma
Posts: 32
Joined: June 15th, 2007, 12:35 pm

Re: Start TB in offline mode?

Post by Burma »

That worked, thanks morat. It will give me a starting point to work out the command line observer above.
morat wrote:Hello World Example:

* <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");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.addEventListener("load", function (event) {
    var doc = event.target;
    var url = doc.location.href;
    if (url == "chrome://messenger/content/messenger.xhtml") {
      chromeWindow.setTimeout(function () {
        try {
          chromeWindow.alert("Hello, world!");
        } catch (e) {
          Components.utils.reportError(e);
        }
      }, 3000);
    }
  }, false);
}, "chrome-document-global-created", false);
Installation directory
http://kb.mozillazine.org/Installation_directory

Thunderbird Portable 32-bit:

* C:\ThunderbirdPortable\App\Thunderbird\defaults\pref\autoconfig.js
* C:\ThunderbirdPortable\App\Thunderbird\mozilla.cfg

Thunderbird Portable 64-bit:

* C:\ThunderbirdPortable\App\Thunderbird64\defaults\pref\autoconfig.js
* C:\ThunderbirdPortable\App\Thunderbird64\mozilla.cfg

Thunderbird 32-bit:

* C:\Program Files\Mozilla Thunderbird\defaults\pref\autoconfig.js
* C:\Program Files\Mozilla Thunderbird\mozilla.cfg

Thunderbird 64-bit:

* C:\Program Files (x86)\Mozilla Thunderbird\defaults\pref\autoconfig.js
* C:\Program Files (x86)\Mozilla Thunderbird\mozilla.cfg

The autoconfig.js file should be in the same folder as the channel-prefs.js file.

The mozilla.cfg file should be in the same folder as the thunderbird.exe executable.
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode?

Post by morat »

morat wrote:I don't remember how to listen for the opening of the main window.
I guess you can use the nsIWindowWatcher.registerNotification method.

Reference
http://searchfox.org/comm-esr102/search ... tification
http://searchfox.org/mozilla-esr102/sou ... atcher.idl
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode?

Post by morat »

Cowabunga Example:

ThunderbirdPortable.exe --cowabunga
thunderbird.exe --cowabunga

* <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");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var commandLine = aSubject;
  if (commandLine.handleFlag("cowabunga", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.ww.registerNotification(function onOpen(aSubject, aTopic, aData) {
        if (aTopic == "domwindowopened") {
          var chromeWindow = aSubject;
          chromeWindow.addEventListener("load", function (event) {
            var doc = event.target;
            var url = doc.location.href;
            if (url == "chrome://messenger/content/messenger.xhtml") {
              Services.ww.unregisterNotification(onOpen);
              chromeWindow.setTimeout(function () {
                try {
                  chromeWindow.alert("Cowabunga!");
                } catch (e) {
                  Components.utils.reportError(e);
                }
              }, 3000);
            }
          }, {once: true});
        }
      });
    }
  }
}, "command-line-startup", false);
You could listen for the "DOMContentLoaded" event. That event is fired before the "load" event.
Burma
Posts: 32
Joined: June 15th, 2007, 12:35 pm

Re: Start TB in offline mode?

Post by Burma »

This is what I've hacked together from morat's code. I'm not sure of all of what it does but it works. It's certainly not elegant and can use some cleaning up:

Code: Select all

// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var commandLine = aSubject;
  if (commandLine.handleFlag("offlinemode", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.ww.registerNotification(function onOpen(aSubject, aTopic, aData) {
        if (aTopic == "domwindowopened") {
          var chromeWindow = aSubject;
          chromeWindow.addEventListener("load", function (event) {
            var doc = event.target;
            var url = doc.location.href;
            if (url == "chrome://messenger/content/messenger.xhtml") {
              Services.ww.unregisterNotification(onOpen);
              chromeWindow.setTimeout(function () {
                try {
                  // chromeWindow.alert("offlinemode!");
                  if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
                        Services.console.logStringMessage("test STATE_INITIAL_LAUNCH without param");
                        var win = Services.wm.getMostRecentWindow("mail:3pane"); // null
                        if (win.MailOfflineMgr.isOnline()) win.MailOfflineMgr.toggleOfflineStatus();
                      }                  
                } catch (e) {
                  Components.utils.reportError(e);
                }
              }, 3000);
            }
          }, {once: true});
        }
      });
    }
  }
}, "command-line-startup", false);
To launch TB offline with this configuration you would go:

"C:\Program Files\Mozilla Thunderbird\thunderbird.exe" --offlinemode

Thanks again morat.
morat
Posts: 6426
Joined: February 3rd, 2009, 6:29 pm

Re: Start TB in offline mode? - SOLVED

Post by morat »

You're welcome.

P.S.

You don't need to use the nsIWindowMediator.getMostRecentWindow method. The onOpen observer gets the most recent window.

Code: Select all

// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var commandLine = aSubject;
  if (commandLine.handleFlag("offlinemode", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.ww.registerNotification(function onOpen(aSubject, aTopic, aData) {
        if (aTopic == "domwindowopened") {
          var chromeWindow = aSubject;
          chromeWindow.addEventListener("load", function (event) {
            var doc = event.target;
            var url = doc.location.href;
            if (url == "chrome://messenger/content/messenger.xhtml") {
              Services.ww.unregisterNotification(onOpen);
              chromeWindow.setTimeout(function () {
                try {
                  if (chromeWindow.MailOfflineMgr.isOnline()) {
                    chromeWindow.MailOfflineMgr.toggleOfflineStatus();
                  }
                } catch (e) {
                  Components.utils.reportError(e);
                }
              }, 3000);
            }
          }, {once: true});
        }
      });
    }
  }
}, "command-line-startup", false);
Burma
Posts: 32
Joined: June 15th, 2007, 12:35 pm

Re: Start TB in offline mode? - SOLVED

Post by Burma »

:mrgreen: :mrgreen: :mrgreen:
morat wrote:You're welcome.

P.S.

You don't need to use the nsIWindowMediator.getMostRecentWindow method. The onOpen observer gets the most recent window.

Code: Select all

// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var commandLine = aSubject;
  if (commandLine.handleFlag("offlinemode", false)) {
    if (commandLine.state == Components.interfaces.nsICommandLine.STATE_INITIAL_LAUNCH) {
      Services.ww.registerNotification(function onOpen(aSubject, aTopic, aData) {
        if (aTopic == "domwindowopened") {
          var chromeWindow = aSubject;
          chromeWindow.addEventListener("load", function (event) {
            var doc = event.target;
            var url = doc.location.href;
            if (url == "chrome://messenger/content/messenger.xhtml") {
              Services.ww.unregisterNotification(onOpen);
              chromeWindow.setTimeout(function () {
                try {
                  if (chromeWindow.MailOfflineMgr.isOnline()) {
                    chromeWindow.MailOfflineMgr.toggleOfflineStatus();
                  }
                } catch (e) {
                  Components.utils.reportError(e);
                }
              }, 3000);
            }
          }, {once: true});
        }
      });
    }
  }
}, "command-line-startup", false);
Post Reply