2013-05-26 116 views
1

这是我使用addon-builder [builder.addons.mozilla.org/]开发的第一个Firefox扩展。通过Firefox扩展下载到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); 

你能只给我从那里我应该得到最简便的下载功能是一个开始。

回答

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