As far as I know, 'save as..' feature from Firefox browser also uses this function, so I tried to implement this function in my code.
All went well except the saved css and javascript files. They contain weird numbers. 'Save as..' from Firefox does not do this. Did I do something wrong?
What I did:
webBrowserPersist.saveDocument(content.document, file, dir, null, null, null);
Did I miss something? Am I supposed to change MIME or encoding flags? (I don't understand these 4th and 5th parameters by the way)
Thanks in advance.
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebBrowserPersist#saveDocument%28%29
The fourthargument is for putting encoding flags. So from the page above (we look for flags that have the word encode in it), we could pass to it like:
persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
persist.saveDocument(gBrowser.contentDocument, localFile, dataPath, persist.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES, null, null);
For fifth its formating flags. We can use multiple by joing them with bitwise or operator so like:
persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
persist.saveDocument(gBrowser.contentDocument, localFile, dataPath, null, persist.PERSIST_FLAGS_FORCE_ALLOW_COOKIES | persist.PERSIST_FLAGS_BYPASS_CACHE, null);
stuff like that
and last argument is for how you want to wrap it. i guess there are some wrapping flags. maybe try passing a number.
Working example:
var file = "c:\\test2.htm";
var data = "c:\\test2_data";
localFile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
localFile.initWithPath(file)
dataPath = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile)
dataPath.initWithPath(data)
persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
.createInstance(Components.interfaces.nsIWebBrowserPersist);
persist.saveDocument(gBrowser.contentDocument, localFile, dataPath, null, null, null);
Stolen from: https://github.com/aptana/Jaxer/blob/f7994fc75a768c9873f094e29868c22e88b46b50/server/src/mozilla/embedding/qa/jstests/nsIWebBrowserPersistTest2.txt#L77
By the way this is what the save us function uses from the menu:
/*
function saveDocument(aDocument, aSkipPrompt)
{
if (!aDocument)
throw "Must have a document when calling saveDocument";
// We want to use cached data because the document is currently visible.
var ifreq =
aDocument.defaultView
.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var contentDisposition = null;
try {
contentDisposition =
ifreq.getInterface(Components.interfaces.nsIDOMWindowUtils)
.getDocumentMetadata("content-disposition");
} catch (ex) {
// Failure to get a content-disposition is ok
}
var cacheKey = null;
try {
cacheKey =
ifreq.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIWebPageDescriptor);
} catch (ex) {
// We might not find it in the cache. Oh, well.
}
internalSave(aDocument.location.href, aDocument, null, contentDisposition,
aDocument.contentType, false, null, null,
aDocument.referrer ? makeURI(aDocument.referrer) : null,
aDocument, aSkipPrompt, cacheKey);
}
*/
saveDocument(gBrowser.contentDocument, true)
this is the function that the save menu item uses.
for this problem you must user PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION property:
var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
createInstance(Components.interfaces.nsIWebBrowserPersist);
var localPath = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
localPath.initWithPath("\home\user\test");
var localFile = localPath.clone();
localFile.append("myfile.html");
persist.persistFlags=persist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
persist.saveDocument(contentDoc, localFile, localPath, 0 , null, null);