0

When my add-on installs it needs to prompt the user to get a username or something like that. After that it stores it and shouldn't ask again. Where would I place this prompt? install.rdf? browser.xul?

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126

2 Answers2

1

There is no explicit mechanism to run code when the extension installs - you should simply do it when your extension runs for the first time. The easiest approach would be checking whether the user name is already set up. If it is not - show the prompt.

It is not recommended to show a modal dialog, those are extremely annoying to users, especially when they suddenly appear during Firefox start-up. You should instead open your page in a tab. A slight complication: Firefox might be restoring a previous session when it starts up. If you open your page too early the session restore mechanism might replace it. So you should wait for the sessionstore-windows-restored notification, something like this should work:

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

var observer = {
  observe: function(subject, topic, data)
  {
    Services.obs.removeObserver("sessionstore-windows-restored", this);

    var browser = window.getBrowser();
    browser.loadOneTab("chrome://...", {inBackground: false});
  },
  QueryInterface: XPCOMUtils.generateQI([
    Components.interfaces.nsIObserver,
    Components.interfaces.nsISupportsWeakReference
  ])
};
Services.obs.addObserver("sessionstore-windows-restored", observer, true);

A final complication is that your code is probably running from a browser window overlay - meaning that there will be multiple instances of your code if the session restored contains more than one window. You probably want the code above to run only once however rather than opening your first-run page in every browser window. So you will have to coordinate somehow, maybe via preferences. A slightly more complicated but better solution would be having a JavaScript code module in your extension - code modules are only loaded once so you wouldn't have a coordination issue there.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
0

Try using an addonlistener https://developer.mozilla.org/en/Addons/Add-on_Manager/AddonListener#onInstalling%28%29

Or by using the preferences: https://stackoverflow.com/a/958944/1360985

Community
  • 1
  • 1
Yansky
  • 4,580
  • 8
  • 29
  • 24
  • As it happens, your extension (and with it - the nice addon listener you wrote) is rather unlikely to be running when your extension is being installed ;) – Wladimir Palant Nov 19 '14 at 20:16