Skip to content Skip to sidebar Skip to footer

Prevent Xul Notificationbox From Closing When Button Is Hit

I have a problem concerning the notificationBox. I create a notification using appendNotification( label , value , image , priority , buttons, eventCallback ) and supply a button

Solution 1:

In my opinion, this is an error in the documentation not a bug in the code. However, throwing an error in your button callback to prevent closure is not the best way to accomplish that goal.

  • Looking at the source code, there were clearly multiple discrepancies between the code and the documentation regarding how buttons work on a notification.
  • There is a specifically coded method of preventing the notification closing from within the button callback (return true from the callback).
  • Throwing an error in order to accomplish a normal functionality is usually a bad programming practice. Doing so also results in an error showing in the console every time your button is pressed. Having errors intentionally showing in the console under normal operation is bad. It also can result in your add-on not being approved in review.
  • As it was documented (not as operational), if you wanted to close when one button was pressed and not close when another was pressed, you would have to store in a global variable which button callback was last called and then choose based on that information if you wanted to prevent closure when your notificationBox callback was executed. That would be an inappropriately complex way to design operation of these notification buttons.

Given all that, I would say that intentionally throwing an error in order to prevent closure is not the "correct" way to do it. While, trowing an error to prevent closure doesn't cause any harm to the operation of the notification box, it does show the error in the console, which is bad.

The correct way to prevent the notification from closing from within the notification button callback is to return a True value from the callback.

While it is possible that the previously inaccurately documented way of doing this the way they intended to have it operate, it is not the way it actually works. Given

  • It is easier to update the documentation than it is to make changes to the code.
  • The code works in a way that is better than the documented method.
  • There were other inaccuracies in the documentation that would have prevented people from using functionality which was supposedly working (popups/menu buttons).

I have, therefore, updated the documentation to reflect what is actually in the source code and copied, with some modification, the code from this answer to an example there.

Here is some code I used to test this:

functiontestNotificationBoxWithButtons() {
    //Create some common variables if they do not exist.//  This should work from any Firefox context.//  Depending on the context in which the function is being run,//  this could be simplified.if (typeofwindow === "undefined") {
        //If there is no window defined, get the most recent.varwindow=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                             .getService(Components.interfaces.nsIWindowMediator)
                             .getMostRecentWindow("navigator:browser");
    }
    if (typeof gBrowser === "undefined") {
        //If there is no gBrowser defined, get itvar gBrowser = window.gBrowser;
    }

    functiontestNotificationButton1Callback(theNotification, buttonInfo, eventTarget) {
        window.alert("Button 1 pressed");

        //Prevent notification from closing://throw new Error('prevent nb close');returntrue;
    };

    functiontestNotificationButton2Callback(theNotification, buttonInfo, eventTarget) {
        window.alert("Button 2 pressed");

        //Do not prevent notification from closing:
    };

    functiontestNotificationCallback(reason) {
        window.alert("Reason is: " + reason);

        //Supposedly prevent notification from closing://throw new Error('prevent nb close');// Does not work.
    };


    let notifyBox = gBrowser.getNotificationBox();

    let buttons = [];

    let button1 = {
        isDefault: false,
        accessKey: "1",
        label: "Button 1",
        callback: testNotificationButton1Callback,
        type: "", // If a popup, then must be: "menu-button" or "menu".popup: null
    };

    buttons.push(button1);

    let button2 = {
        isDefault: true,
        accessKey: "2",
        label: "Button 2",
        callback: testNotificationButton2Callback,
        type: "", // If a popup, then must be: "menu-button" or "menu".popup: null
    };

    buttons.push(button2);

    //appendNotification( label , value , image (URL) , priority , buttons, eventCallback )
    notifyBox.appendNotification("My Notification text", "Test notification unique ID",
                                 "chrome://browser/content/aboutRobots-icon.png",
                                 notifyBox.PRIORITY_INFO_HIGH, buttons,
                                 testNotificationCallback);
}

Post a Comment for "Prevent Xul Notificationbox From Closing When Button Is Hit"