在按下按钮时阻止XUL通知框关闭 [英] Prevent XUL notificationBox from closing when button is hit

查看:83
本文介绍了在按下按钮时阻止XUL通知框关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到有关 notificationBox 的问题。我使用

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

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

并在按钮参数中提供一个按钮。
现在,我想在按下按钮时阻止notificationBox关闭。 XUL文档指出这可以通过在 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:


这个回调可以是用于防止通知框关闭按钮单击。在回调函数中只是抛出一个错误。 (例如:抛出新错误('阻止nb关闭');

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

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


  1. 这是XUL中的错误还是与文档不一致?

  2. 通过将其添加到按钮的回调是否有任何伤害功能?


推荐答案

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

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.


  • 查看源代码,显然存在多个差异有关按钮如何工作的代码和文档 通知

  • 有一种特殊编码方法可以阻止通知在按钮回调中关闭(从回调中返回 true )。

  • 为了实现正常功能而抛出错误通常是一种糟糕的编程习惯。这样做也会导致每次按下按钮时控制台中都会显示错误。在正常操作下故意在控制台中显示错误是不好的。它还可能导致您的插件未经审核批准。

  • 因为它已被记录(不是可操作的),如果您想在按下一个按钮时关闭而不是在关闭时关闭另一个被按下了,你必须存储一个全局变量,最后一次调用按钮回调,然后根据该信息选择,如果你想在 notificationBox 回调时阻止关闭执行。这将是设计这些通知按钮的操作的一种不恰当的复杂方式。

  • 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.

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

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


  • 更新文档比更改代码更容易。

  • 代码的工作方式比记录的方法更好。

  • 文档中还有其他不准确之处会阻止人们使用可能正常工作的功能(弹出窗口/菜单按钮) )。

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

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

这篇关于在按下按钮时阻止XUL通知框关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆