【问题标题】:Add download to Firefox via Firefox extension通过 Firefox 扩展添加下载到 Firefox
【发布时间】:2013-05-26 06:27:12
【问题描述】:

这是我正在使用 addon-builder [builder.addons.mozilla.org/] 开发的第一个 Firefox 扩展。

我的问题很简单,但是尝试了很多东西,很多天,我都无法得到结果。

我想知道:如何将文件下载添加到 Firefox 下载器??

我有一个类似的网址:http://example.com/file.zip 和一个类似的文件位置:D:\myFolder。 我想通过我的 Firefox 扩展添加这个下载。

我搜索过的东西是:

https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWebBrowserPersist#saveURI%28%29

https://developer.mozilla.org/en-US/docs/Code_snippets/Downloading_Files

Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");

const WebBrowserPersist = Components.Constructor("@mozilla.org/embedding/browser/nsWebBrowserPersist;1",
                                                 "nsIWebBrowserPersist");

var persist = WebBrowserPersist();

var targetFile = Services.dirsvc.get("Desk", Ci.nsIFile);
targetFile.append("file.bin");

// Obtain the privacy context of the browser window that the URL
// we are downloading comes from. If, and only if, the URL is not
// related to a window, null should be used instead.
var privacy = PrivateBrowsingUtils.privacyContextFromWindow(urlSourceWindow);

persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
                     | persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;

persist.saveURI(uriToSave, null, null, null, "", targetFile, privacy);

你能从我应该获得最简单的下载功能的地方开始吗?

【问题讨论】:

    标签: firefox firefox-addon download


    【解决方案1】:
    Components.utils.import("resource://gre/modules/Services.jsm");
    var {downloads}=Services;
    downloads.addDownload(/*parameters*/);  //see documentation for parameters.
    

    addDownload 的文档:nsIDownloadManager#addDownload()

    Services.jsm 提供的各种服务的文档和目录:Services.jsm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      相关资源
      最近更新 更多