【问题标题】:Disabling beforeunload dialogues in Firefox 29 extension在 Firefox 29 扩展中禁用 beforeunload 对话
【发布时间】:2014-06-05 23:24:57
【问题描述】:

我正在尝试禁用 Firefox 扩展程序中的 beforeUnload 对话框,该扩展程序通过将窗口包装在 XPCNativeWrapper 中并使用 addeventlistener 重新定义 window.onbeforeunload 直到 FF28。

示例 JavaScript:

var win = new XPCNativeWrapper(window, "onbeforeunload", "event", "addEventListener()");
var beforeUnload = win.onbeforeunload;
win.onbeforeunload = null;
var newBeforeUnload = function(e) {
    beforeUnload();
    //code to remove dialog
}
win.addEventListener('beforeunload', newBeforeUnload, false);

当 beforeUnload 方法更改页面的位置时,这在 FF29 中停止工作。在 FF29 中,我收到错误“拒绝从脚本访问 'chrome://browser/content/page.html'”。

页面上的JS示例:

window.onbeforeunload = function() {
    parent.frame2.location="page.html";
};

【问题讨论】:

  • 您是否正在尝试编辑插件源代码?

标签: javascript firefox-addon onbeforeunload


【解决方案1】:

因此,我能够使用以下方法解决我的问题:

windowToModify.location.replace("javascript:(" + function() {
    window.oldBeforeUnload = window.onbeforeunload;
    window.onbeforeunload = function(E) {
        window.oldBeforeUnload();
        var evt = e || window.event;
        if (evt && evt.returnValue) {
            delete evt['returnValue'];
        }
    };
    window.onunload = null;
} + ")()");

所以,javascript:(/*some javascript as a string */) 是一个有效的位置,它将在窗口/框架的范围内运行。此脚本设置一个名为 window.oldBeforeUnload 的属性,然后覆盖 onbeforeunload 以禁用卸载前对话框。

这应该适用于 FF 3.6+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2021-11-02
    相关资源
    最近更新 更多