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

I am building an add-on for an application. The clients are paying to view some webpages and download s

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)!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741820745a4367944.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信