使用文件保存带有 Firefox 插件的网页 -> 另存为弹出窗口 window

Save web pages with Firefox addon using file -> save as pop-up window

首先让我说我是附加组件开发的新手。使用附加 SDK,我正在尝试创建一个简单的 Firefox 附加组件,当按下一个按钮时,它的作用就像按下 Ctrl-S 热键,或者按照文件 -> 保存页面作为弹出保存页面向上 window。我在这里看过类似的问题,但它们似乎围绕着内置的保存功能,而不是利用 "save page as" window。

最终目标是在进行保存调用之前 运行 其他功能。用户将只能正常看到保存页面 window。

我不知道发送热键信号或从加载项中访问文件下拉菜单的方法。

执行此操作的一种方法是调用 另存为 对话框,就像用户单击 "Save Page As..." 菜单项一样 (id="menu_savePage") .您可以通过执行该菜单项的 doCommand() 方法来完成此操作。下面假设传入的event是用户点击按钮的command事件

function launchSaveAsFromButton(event) {

    var window = event.view;

    //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 (window === null || typeof window !== "object") {
        //If you do not already have a window reference, you need to obtain one:
        //  Add a "/" to un-comment the code appropriate for your add-on type.
        //* Add-on SDK:
        var window = require('sdk/window/utils').getMostRecentBrowserWindow();
        //*/
        /* Overlay and bootstrap (from almost any context/scope):
        var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
                             .getService(Components.interfaces.nsIWindowMediator)
                             .getMostRecentWindow("navigator:browser");
        //*/
    }
    if (typeof document === "undefined") {
        //If there is no document defined, get it
        var document = window.content.document;
    }
    if (typeof gBrowser === "undefined") {
        //If there is no gBrowser defined, get it
        var gBrowser = window.gBrowser;
    }

    let menuSavePage = gBrowser.ownerDocument.getElementById("menu_savePage");
    menuSavePage.doCommand();
}

使用 DOM Inspector in combination with the add-on Element Inspector.

可以更轻松地找出 "Save Page As..." 对话框的 ID