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

标签 javascript firefox save firefox-addon firefox-addon-sdk

首先我要说的是,我是附加开发的新手。使用附加 SDK,我尝试创建一个简单的 Firefox 附加组件,当按下按钮时,其作用类似于按 Ctrl-S 热键,或按照文件 -> 保存页面来弹出保存页面上窗。我在这里看过类似的问题,但它们似乎是围绕内置的保存功能,而不是利用“页面另存为”窗口。

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

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

最佳答案

一种方法是调用“另存为”对话框,就像用户单击“页面另存为...”菜单项 (id="menu_savePage")。您可以通过执行该菜单项的 doCommand() 方法来完成此操作。下面假设传入的事件是用户单击的按钮的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 可以更轻松地查找“页面另存为...”对话框的 ID与附加组件 Element Inspector 结合使用.

关于javascript - 使用 Firefox 插件保存网页,使用文件 -> 另存为弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593566/

相关文章:

javascript - zoom.scaleExtent() 在 d3.js 中做了什么?

javascript - Firefox 中的 setTimeOut 问题

ios - iOS文件创建可访问所有应用程序

tensorflow - 保存 Tensorflow 图和变量,同时排除 Adam 优化器变量

javascript - 在 PHP 中调用自定义 JS 警报

javascript - Redux - 如何记录应用程序状态?

html - CSS 样式适用于 Chrome 但不适用于 Firefox

javascript - 大纲在 Firefox 上无法正常工作

forms - 如何告诉 Play Framework 2 和 Ebean 保存空字段?

javascript - 从谷歌地图和搜索中检索坐标