用Firefox插件保存网页,使用file ->保存为弹出窗口

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

本文关键字:保存 窗口 插件 Firefox 保存网页 file 使用      更新时间:2023-09-26

让我先说一下,我是一个外挂开发新手。使用附加组件SDK,我试图创建一个简单的Firefox附加组件,当按下按钮时,就像按Ctrl-S热键一样,或者按照文件->保存页面来获得保存页面弹出窗口。我在这里看过类似的问题,但他们似乎是围绕内置的保存功能,而不是利用"保存页面为"窗口。

最终目标是在save调用之前运行其他函数。用户将只看到正常的保存页面窗口。

我不知道如何发送热键信号,或从附加组件内访问文件下拉菜单。

这样做的一种方法是调用Save As对话框,就好像用户点击了"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检查器和附加元素检查器,可以更容易地找到"Save Page As…"对话框的ID