如何在本地保存网页,包括图片等 [英] How to save a webpage locally including pictures,etc

查看:171
本文介绍了如何在本地保存网页,包括图片等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个应用程序构建一个附加组件。客户正在付费查看一些网页,并从中下载一些文件。他们希望通过附件自动执行下载过程。因此,不要选择将页面另存为并等待下载完成,而是可以单击该附件并忘记该过程。问题是,网页提供了一些cookie到浏览器。所以最好的办法是文件 - >保存页面为。我想通过附加组件来完成。有没有任何Firefox的JavaScript的方式呢?我使用了nsiDownloader。但它只保存html,而不是图片等。任何人都可以引导我在这个问题上?

编辑:
这是代码, 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(要保存);
wbp.saveDocument(content.document,file,dir,null,null,null);
alert(saved);

编辑:
但仍有部分网页无法保存完全如将页面另存为。这些保存的页面并不像原始页面那样呈现,它们看起来像一些html示例。 保存页面为正在按预期工作,我试着翻阅源代码(chrome://browser/content/browser.xul),发现这一点:

< a href =https://developer.mozilla.org/en/nsIWebBrowserPersist#saveDocument%28%29 =nofollow> https://developer.mozilla.org/en/nsIWebBrowserPersist#saveDocument()



确保只有在网页完全加载(而不是DOMContentLoaded)后才能调用这个函数!!


I am building an add-on for an application. The clients are paying to view some webpages and download some files out of it. They want to automate this downloading process by add-on. So instead of selecting "Save Page as" and waiting for the download's completion, they can click the add-on and forget the process. The problem is, the webpage is providing some cookies to the browser. So the best way is File-> "Save Page As" . I want to do it through the add-on. Is there any firefox-javascript way for this?. I used nsiDownloader. But it saves only html, not the pictures,etc. Can anybody guide me in this issue?

EDIT: Hi, This is the code which did the trick, thanks to 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");

EDIT: But, still some webpages are not saved exactly as "Save Page as". Those saved pages are not rendered like original pages, they are look like some html example.

解决方案

Since you mention that File->"Save Page As" is working as expected, I tried looking through the source code (chrome://browser/content/browser.xul) and found this:

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

Make sure that you shall call this function only after the webpage is completely loaded (not DOMContentLoaded)!!

这篇关于如何在本地保存网页,包括图片等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆