如何在本地保存网页,包括图片等

pra*_*ran 5 javascript firefox webpage xpcom firefox-addon

我正在为应用程序构建一个附件。客户需要付费查看一些网页并从中下载一些文件。他们希望通过加载项自动执行此下载过程。因此,他们不必单击“另存为”并等待下载完成,而是可以单击加载项而忽略该过程。问题是,网页正在向浏览器提供一些cookie。因此,最好的方法是File->“另存页面为”。我想通过附加组件来完成。是否有任何使用firefox-javascript的方法?我使用了nsiDownloader。但是它只保存html,而不保存图片等。有人可以指导我解决这个问题吗?

编辑: 嗨,这是完成欺骗的代码,这要归功于sai prasad

var dir =Components.classes["@mozilla.org/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile); 
dir.initWithPath("C:\\filename");
var file = Components.classes["@mozilla.org/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile);  
file.initWithPath("C:\\filename.html");  
var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']  
          .createInstance(Components.interfaces.nsIWebBrowserPersist);  
alert("going to save");
wbp.saveDocument(content.document, file,dir, null, null, null);  
alert("saved");
Run Code Online (Sandbox Code Playgroud)

编辑: 但是,仍然有些网页没有完全保存为“另存为”。那些保存的页面不像原始页面那样呈现,它们看起来像一些html示例。

Sai*_*sad 5

由于您提到File->“ Save Page As”可以按预期工作,因此我尝试遍历源代码(chrome://browser/content/browser.xul),发现了这一点:

https://developer.mozilla.org/en/nsIWebBrowserPersist#saveDocument()

确保仅在完全加载网页(而不是DOMContentLoaded)后才调用此函数!!