Prevent XUL notificationBox from closing when button is hit

≡放荡痞女 提交于 2019-12-13 15:22:26

问题


I have a problem concerning the notificationBox. I create a notification using

appendNotification( label , value , image , priority , buttons, eventCallback )

and supply a button in the buttons argument. Now, I want to prevent the notificationBox from closing when I hit the button. The XUL Documentation states that this can be done by throwing an error in the eventCallback function:

This callback can be used to prevent the notification box from closing on button click. In the callback function just throw an error. (For example: throw new Error('prevent nb close');)

This does not work for me, however, it works when I add the throw-statement to the callback function of the button itself.

  1. Is this a bug in XUL or an inconsistency with the documentation?
  2. Is there any harm done by adding it to the button's callback function?

回答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:

function testNotificationBoxWithButtons() {
    //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 (typeof window === "undefined") {
        //If there is no window defined, get the most recent.
        var window=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 it
        var gBrowser = window.gBrowser;
    }

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

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

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

        //Do not prevent notification from closing:
    };

    function testNotificationCallback(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);
}


来源:https://stackoverflow.com/questions/35892102/prevent-xul-notificationbox-from-closing-when-button-is-hit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!