我有关于 notificationBox 的问题.我 create a notification使用
appendNotification( label , value , image , priority , buttons, eventCallback )
并在 buttons
参数中提供一个按钮。
现在,我想阻止 notificationBox 在我按下按钮时关闭。 XUL Documentation声明这可以通过在 eventCallback
函数中抛出错误来完成:
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');
)
这对我不起作用,但是,当我将 throw
语句添加到按钮本身的回调函数时它会起作用。
- 这是 XUL 中的错误还是与文档不一致?
- 将它添加到按钮的回调函数中有什么坏处吗?
最佳答案
在我看来,这是文档中的错误,而不是代码中的错误。但是,在按钮回调中抛出错误以防止关闭并不是实现该目标的最佳方式。
- 查看 source code , 代码与 the documentation 之间显然存在多个差异关于按钮如何在 notification 上工作.
- 有一种专门编码的方法可以防止按钮回调中的通知关闭(从回调中返回
true
)。 - 为了完成正常功能而抛出错误通常是一种糟糕的编程习惯。这样做还会导致每次按下按钮时在控制台中显示错误。在正常操作下故意在控制台中显示错误是不好的。它还可能导致您的附加组件未通过审核。
- 如文档所述(不是操作性的),如果你想在按下一个按钮时关闭而不是在按下另一个按钮时关闭,你必须将最后调用哪个按钮回调的全局变量存储起来,然后选择基于如果您想在执行
notificationBox
回调时阻止关闭,请根据该信息。这将是设计这些通知按钮操作的一种不恰当的复杂方式。
考虑到所有这些,我会说故意抛出错误以防止关闭并不是“正确”的做法。虽然抛出错误以防止关闭不会对通知框的操作造成任何损害,但它确实会在控制台中显示错误,这很糟糕。
防止通知从通知按钮回调中关闭的正确方法是从回调中返回一个 True
值。
虽然以前记录不准确的方式可能按照他们打算让它运行的方式执行此操作,但这并不是它实际工作的方式。给定
- 更新文档比更改代码更容易。
- 代码的工作方式比记录的方法更好。
- 文档中还有其他不准确之处,可能会阻止人们使用本应有效的功能(弹出窗口/菜单按钮)。
因此,我更新了 the documentation以反射(reflect)源代码中的实际内容,并通过一些修改将此答案中的代码复制到此处的示例。
这是我用来测试的一些代码:
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);
}
关于javascript - 防止 XUL notificationBox 在点击按钮时关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35892102/