Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...