saveDocument() function from NSIWebBrowserPersist

我怕爱的太早我们不能终老 提交于 2019-12-04 20:18:36

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!