Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

Firefox addon - can I listen for prompts handled by nsIPromptService?


Firefox exposes this service:

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIPromptService

Great! It is possible to make firefox display all kind of prompts, alerts, confirm boxes etc.

But can I register event listener anywhere? Basically I want to know when any alert, confirm, basic auth prompt, or even <select> options list appears on the screen. Is it possible?


Solution

  • As you already found out, there is a DOMWillOpenModalDialog event. This event might be sufficient for your needs, but you should keep some things in mind:

    • You need to actually add an event listener to all windows that you're interested in and that could open a modal dialog.
    • The event will be fired not only for nsIPromptService windows, but also for tab-modal (pseudo windows) and all other modal dialogs, such as Filepicker windows, sub-windows of the main preferences window, custom add-on provided windows.

    There are other possible solutions, though:

    • Override nsIPromptService with your own implementation. You would then just keep a reference to the original implementation around that you got prior and pass the calls along after inspecting them or whatever.
    • You can overlay the actual modal prompt windows like any other XUL window, at least on Firefox Desktop, e.g. chrome://global/content/commonDialog.xul and/or chrome://global/content/selectDialog.xul to customize the window even more.