【问题标题】:Save web pages with Firefox addon using file -> save as pop-up window使用文件保存带有 Firefox 插件的网页 -> 另存为弹出窗口
【发布时间】:2015-07-24 03:18:34
【问题描述】:

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

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

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

【问题讨论】:

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


【解决方案1】:

执行此操作的一种方法是调用 另存为 对话框,就像用户单击“将页面另存为...”菜单项 (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 与附加组件Element Inspector 结合使用,可以更轻松地查找“将页面另存为...”对话框的 ID。

【讨论】:

  • 组件似乎不适合我。我收到以下错误:JPM 未定义消息:ReferenceError:Components 在此上下文中不可用。组件提供的功能可能在 SDK 模块中可用:developer.mozilla.org/en-US/Add-ons/SDK 但是,如果您仍需要导入组件,您可以使用 chrome 模块的属性作为组件属性的快捷方式。然后它给出了快捷方式和 require 语句,但是添加它们并不能解决错误。
  • @zlaecikdos:您没有声明您正在使用附加 SDK(我已编辑问题以包含该标签,并明确表示您正在使用它)。该代码现在包括获取窗口,如果由于某种原因尚未从 event.view 定义它,它将与 SDK 一起使用。
  • 现在可以使用了。感谢您的帮助,以及我造成的任何困惑。我真的对插件开发一无所知,所以我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
相关资源
最近更新 更多