2013-05-26 138 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