Custom Alerts in Firefox Extensions

News Feed

I was putting together the Text Formating Toolbar and I found a need to have a prompt window that had three buttons and a text box, and an option check box.

I had two options write my own alert box, but since I needed three times, plus I was keeping in mind this will not be the last time I need to do this. that would have meant writing a lot of scripting to accept various options an strings for the title, button title and check box. A lot of work, it would have taken a days work at least. The other option was to work out how I could use the window used by nsIPromptService. After a lot of trying I worked this out and that is what I want to document.

window.openDialog("chrome://global/content/commonDialog.xul","",
"chrome, dialog, modal",args);

That was the easy part, it was setting up how the arguments are passed to the window that had me stumped for a while, till I worked that out too. The answer was nsIDialogParamBlock.

So first we call the service.

var args = Components.classes["@mozilla.org/embedcomp/dialogparam;1"]
.getService(Components.interfaces.nsIDialogParamBlock);

Now we set all our arguments, you only have to use the ones you want to I have listed all for completeness sake.

args.SetInt(0, number); // The button that is selected by default. 0 = OK, 1 = Cancel 2 = Custom Button1 and 3 = Custom Button2
args.SetString(0, 'some string'); // Info text about the dialog input boxs

args.SetInt(1, number); // For the check box. 0 = not ticked, 1 = ticked
args.SetString(1, 'some string');// The label of the check box

args.SetInt(2, number); // The number of buttons you want to show, 1-4 are the valid range. If you set to 1 only OK shows, if 2 OK and Cancel, if 3 Ok Cancel and 1 extra...
args.SetString(2, 'some string');//

args.SetInt(3, number); // The text feilds to show. 1 is the normal text feild. 2 is the normal plus 1 password feild
args.SetString(3, 'some string');// Bold info text, it displays above all other lable and text boxes in the window.

args.SetInt(4, number); // This alters the above. If set to one, and SetInt(3, number) is set to 2 you get 2 password feilds. If SetInt(3, number) is set to one, you get one password feild.
args.SetString(4, 'some string');// The login box label, this box is also normaly used for just about anything where we want a text responce.

args.SetInt(5, number); // set the focused button. 1 = accept, 2 = cancel, 3 = extra1, 4 = extra2
args.SetString(5, 'some string');// Label for the first password feild

args.SetInt(6, number); // if set to any value other than 0 the buttons, execpt for the cancel one, is disabled for 2 seconds
args.SetString(6, 'some string');// Text in the login box, again this text input can be used for anything else

args.SetInt(7, number); // Nothin, SetInt(6) is the highest.
args.SetString(7, 'some string');// The text in the first password input box.

args.SetString(8, 'some string');// Text string if we want something other than OK on the OK button

args.SetString(9, 'some string');// Text for the Cancle button if we don't want Cancel

args.SetString(10, 'some string');// Text for the first custom button

args.SetString(11, 'some string');// Text for the second custom button

args.SetString(12, 'some string');// The dialog window title

Now after the window has closed we can get the same arguments useing args.GetInt() and args.GetString(). For example args.GetInt(0) will be the button that was pressed. 0 = OK, 1 = Cancel 2 = Custom Button1 and 3 = Custom Button2. And args.GetString(6) would be the value typed in the text box. You should be able to work the rest about from the above.

I hope this helps someone, since it took me a long time to work it all out. I ended up needing to make my own alert window because I needed features that were not in the Firefox default one, but knowing all of what I found out from this helped the design of my own.