最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to save a webpage locally including pictures,etc - Stack Overflow

programmeradmin0浏览0评论

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 pletion, 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/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile); 
dir.initWithPath("C:\\filename");
var file = Components.classes["@mozilla/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile);  
file.initWithPath("C:\\filename.html");  
var wbp = Components.classes['@mozilla/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.

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 pletion, 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/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile); 
dir.initWithPath("C:\\filename");
var file = Components.classes["@mozilla/file/local;1"]  
       .createInstance(Components.interfaces.nsILocalFile);  
file.initWithPath("C:\\filename.html");  
var wbp = Components.classes['@mozilla/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.

Share Improve this question edited Jul 1, 2012 at 14:01 prabhakaran asked Jun 30, 2012 at 6:07 prabhakaranprabhakaran 5,27417 gold badges75 silver badges109 bronze badges 1
  • I'm not really 100% on this but take a look at CURL – Sturm Commented Jun 30, 2012 at 7:38
Add a ment  | 

1 Answer 1

Reset to default 5

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/en/nsIWebBrowserPersist#saveDocument()

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论