Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 624851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:14:43+00:00 2026-05-13T19:14:43+00:00

Thanks to everyone in advance – I need to load a preference before any

  • 0

Thanks to everyone in advance –

I need to load a preference before any windows are loaded at startup. Below is some /component code I have been working with. The SetPreference method seems to fail when it is called (nothing executes afterwords either) – I am assuming because the resources that it needs are not available at the time of execution…or I am doing something wrong. Any suggestions with this code or another approach to setting a preference at startup?

Thanks again,

Sam

For some reason the code formatting for SO is not working properly – here is a link to the code as well – http://samingrassia.com/_FILES/startup.js

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

const Cc = Components.classes;
const Ci = Components.interfaces;

const ObserverService = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);

function MyStartupService() {};

MyStartupService.prototype = {
  observe : function(aSubject, aTopic, aData) {
    switch (aTopic) {
      case 'xpcom-startup':
        this.SetPreference("my.extension.is_running", "false");
        break;
      case 'app-startup':
        this.SetPreference("my.extension.is_running", "false");
        ObserverService.addObserver(this, 'final-ui-startup', false);
        break;
      case 'final-ui-startup':

        //make sure is_running is set to false
        this.SetPreference("my.extension.is_running", "false");

        ObserverService.removeObserver(this, 'final-ui-startup');
        const WindowWatcher = Cc['@mozilla.org/embedcomp/window-watcher;1'].getService(Ci.nsIWindowWatcher);
        WindowWatcher.registerNotification(this);
        break;
      case 'domwindowopened':
        this.initWindow(aSubject);
        break;
    }
  },
  SetPreference : function(Token, Value) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    str.data = Value;
    prefs.setComplexValue(Token, Components.interfaces.nsISupportsString, str);

    //save preferences
    var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
    prefService.savePrefFile(null);  
  },
  initWindow : function(aWindow) {
    if (aWindow != '[object ChromeWindow]') return;
    aWindow.addEventListener('load', function() {
      aWindow.removeEventListener('load', arguments.callee, false);
      aWindow.document.title = 'domwindowopened!';
      // for browser windows
      var root = aWindow.document.documentElement;
      root.setAttribute('title', aWindow.document.title);
      root.setAttribute('titlemodifier', aWindow.document.title);
    }, false);
  },
  classDescription : 'My Startup Service',
  contractID : '@mystartupservice.com/startup;1',
  classID : Components.ID('{770825e7-b39c-4654-94bc-008e5d6d57b7}'),
  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver]),
  _xpcom_categories : [{ category : 'app-startup', service : true }]
};

function NSGetModule(aCompMgr, aFileSpec) {
  return XPCOMUtils.generateModule([MyStartupService]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:14:44+00:00Added an answer on May 13, 2026 at 7:14 pm

    To answer your real question, which is

    I have code that loads on every window load and I need to make sure that only gets executed once every time firefox starts up.

    ..you should just use a module, in the load handler that you wish to execute once, check a flag on the object exported from (i.e. “living in”) the module, then after running the code you need, set the flag.

    Since the module is shared across all windows, the flag will remain set until you close Firefox.

    As for your intermediate problem, I’d suggest wrapping the code inside observe() in a try { ... } catch(e) {dump(e)} (you’ll need to set a pref and run Firefox in a special way in order to see the output) and check the error returned.

    I guess xpcom-startup and app-startup is too early to mess with preferences (I think you need a profile for that), note that you don’t register to get xpcom-startup notification anyway. You probably want to register for profile-after-change instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks to everyone in advance. I've been doing some research on error handling and
Thanks to everyone in advance, I am trying to access any context parameters in
Hello everyone and thanks in advance for any ideas, suggestions or answers. First, the
First at all, thanks everyone. What do I need to get work crystal report
Thanks to everyone in advance - So I went ahead and implemented this code:
(More package/component install fun for me -- thanks everyone for all your help to
That's my first post here. Thanks in advance everyone that collaborates with me. I'm
Hi everyone and thanks in advance for your help. This is the situation: I'm
Thanks to everyone in advance - I have yet to find an easy way
Thanks to everyone in advance, I have overridden HTTPServletResponse that collects headers and also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.