在按下按钮时阻止XUL通知框关闭

时间:2016-03-09 12:51:08

标签: javascript xul

我对notificationBox有疑问。我create a notification使用

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

并在buttons参数中提供一个按钮。 现在,我想在按下按钮时阻止notificationBox关闭。 XUL Documentation表示可以通过在eventCallback函数中抛出错误来完成此操作:

  

此回调可用于防止通知框在按钮单击时关闭。在回调函数中只是抛出一个错误。 (例如:throw new Error('prevent nb close');

这对我不起作用,但是,当我将throw - 语句添加到按钮本身的回调函数时,它可以正常工作。

  1. 这是XUL中的错误还是与文档不一致?
  2. 通过将其添加到按钮的回调功能是否有任何伤害?

1 个答案:

答案 0 :(得分:3)

在我看来,这是文档中的错误,而不是代码中的错误。但是,在按钮回调中抛出错误以防止关闭并不是实现该目标的最佳方法。

  • 查看source code,代码和the documentation之间明显存在多个关于按钮如何在notification上工作的差异。
  • 有一种特殊编码方法可以阻止通知在按钮回调中关闭(从回调中返回true)。
  • 为了实现正常功能而抛出错误通常是一种糟糕的编程习惯。这样做也会导致每次按下按钮时控制台中都会显示错误。在正常操作下故意在控制台中显示错误是不好的。它还可能导致您的插件未经审核批准。
  • 由于它已被记录(不是可操作的),如果你想在按下一个按钮时关闭而在按下另一个按钮时没有关闭,你必须存储一个全局变量,最后一个按钮回调被调用,然后选择基于如果您想在执行notificationBox回调时阻止关闭,请填写该信息。这将是设计这些通知按钮操作的一种不恰当的复杂方式。

鉴于这一切,我会说故意抛出错误以防止关闭并不是“正确”的方法。虽然,为了防止关闭而抛出错误不会对通知框的操作造成任何损害,但它确实在控制台中显示错误,这是不好的。

阻止通知在通知按钮回调中关闭的正确方法是从回调中返回True值。

虽然以前记录不明确的方式可能是他们打算让它运行的方式,但实际上并不是这样。给定

  • 更新文档比更改代码更容易。
  • 代码的工作方式优于文档化的方法。
  • 文档中还有其他不准确之处会阻止人们使用可能正常工作的功能(弹出窗口/菜单按钮)。

因此,我更新了the documentation以反映源代码中的实际内容,并通过一些修改将此答案的代码复制到那里。

以下是我用来测试的一些代码:

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);
}