Hi !

I am trying to extract Html source of the DOM document but I am not able to
save the html source......

Can any body tell me how to achieve this..........


 NS_IMETHODIMP MyComponent::OnStateChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
 {
     USES_CONVERSION;

     if((nsIWebProgressListener::STATE_STOP & aStateFlags) &&
(nsIWebProgressListener::STATE_IS_DOCUMENT & aStateFlags))
     {
         nsCOMPtr<nsIDOMWindow> window;
         nsresult rv= aWebProgress->GetDOMWindow(getter_AddRefs(window));

         nsCOMPtr<nsIDOMDocument> document;
         rv=window->GetDocument(getter_AddRefs(document));
         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc
(do_QueryInterface(document));
         nsString url;

     if(htmlDoc)
     {
         try
         {
            rv=htmlDoc->GetURL(url);
         }catch(...){return 0;}

         char* _url=W2A(url.get());
         char* mapurl=MapPath(_url);

         if(mapurl)
         {
            nsCOMPtr<nsIDOMWindowCollection> frames;
            rv=window->GetFrames(getter_AddRefs(frames));
            uint frameLength=0;
            frames->GetLength(&frameLength);
            uint i=0;
            if(frames && frameLength)
            {
                for(uint i=0;i<frameLength;i++)
                {
                    nsCOMPtr<nsIDOMWindow> frameWindow;


//frames->NamedItem(NS_LITERAL_STRING("messageAreaIframe"),getter_AddRefs(frameWindow));
                    frames->Item(i,getter_AddRefs(frameWindow));

                    if(frameWindow)
                    {
                        nsCOMPtr<nsIDOMDocument> frameDOMDoc;

rv=frameWindow->GetDocument(getter_AddRefs(frameDOMDoc));
                        nsCOMPtr<nsIDOMHTMLDocument> frameDoc
(do_QueryInterface(frameDOMDoc));
                        nsString _url;
                        try
                        {
                            frameDoc->GetURL(_url);
                        }
                        catch(...){    }

                        if(strstr(W2A(_url.get()),"478"))
                        {
                            if(frameDoc)
                            {
                                SaveHTML(frameDoc,mapurl);
                            }
                        }
                    }
                }
          }
         }

     }         //AttachListener(htmlDoc);
     }
     return 0;
 }

///////////////////////////////////////////// following is saveHTML function

NS_IMETHODIMP MyComponent::SaveHTML(nsIDOMHTMLDocument * htmlDoc,char* _url)
 {
     USES_CONVERSION;
    nsCOMPtr<nsIDocToString> docObj(do_CreateInstance("@
mozilla.org/DocToString;1"));

    char* retVal;
    docObj->GetHtmlString(htmlDoc,&retVal);        ///this function called
from java script

    nsCOMPtr<nsILocalFile> directory(do_CreateInstance("@
mozilla.org/file/local;1"));
    PRBool exists =  PR_TRUE;
    nsresult rv =
directory->InitWithNativePath(NS_LITERAL_CSTRING(NS_SAVEPATH));

    rv = directory->Exists(&exists);
    if (!exists)
    {
         directory->Create(1U,0);//1U means Directory
    }
    char dirPath[512]="";
    strcat(dirPath,NS_SAVEPATH);
    strcat(dirPath,_url);

    nsCOMPtr<nsILocalFile> dirToCreate(do_CreateInstance("@
mozilla.org/file/local;1"));

    rv = dirToCreate->InitWithNativePath(NS_LITERAL_CSTRING(dirPath));

    rv = dirToCreate->Exists(&exists);
    if (!exists)
    {
         dirToCreate->Create(1U,0);
    }
    GUID SubID ; WCHAR *pOut ;
    CoCreateGuid (&SubID) ;
    StringFromCLSID(SubID, &pOut) ;

    strcat(dirPath,W2A(pOut));
    strcat(dirPath,".htm");
    FILE* logFile=NULL;
    try
        {
            logFile=fopen( dirPath , "w");
            if(logFile)
            {
                fputs( retVal, logFile );
            }
    }catch(...){}

    if(logFile)
    {
        fclose(logFile);
    }
    logFile = NULL;

    rv=docObj->SaveDocument(htmlDoc,&retVal);

    FILE* log=NULL;
    char* dir="C:\\tmp.txt";
     try
        {
            log=fopen( dir , "w");
            if(log)
            {
                fputs( retVal, log);
                fclose(log);
            }
            log=NULL;
        }
        catch(exception& e){}
     return 0;
 }

////////////////////////////////// Java script code

function DocToString(){}

DocToString.prototype = {
    GetHtmlString: function (root)
     {
        var serializer = Components.classes["@
mozilla.org/xmlextras/xmlserializer;1"].createInstance(
Components.interfaces.nsIDOMSerializer);
        if (!serializer) return Components.results.NS_ERROR_FAILURE;
        return serializer.serializeToString(root);
     },

    QueryInterface: function (iid)
    {
        if (!iid.equals(Components.interfaces.nsIDocToString)
            && !iid.equals(Components.interfaces.nsISupports))
        {
            throw Components.results.NS_ERROR_NO_INTERFACE;
        }
        return this;
    },
      SaveDocument: function (aDocument)
      {

        try
        {
            file = "c:\\Temp2\\test2.htm";
            data = "c:\\Temp2\\";


//netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

          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);
 /*
        persistListener = new PersistProgressListener;
        persist = Components.classes["@
mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
              .createInstance(Components.interfaces.nsIWebBrowserPersist);

        persist.progressListener = persistListener;
        persist.saveDocument(aDocument, localFile, dataPath,null,null,null);
   */
       }
        catch(ex)
       {
         return (ex);
       }
        return "test";
    }

}

 function PersistProgressListener()
 {
   this.init();
 }

 PersistProgressListener.prototype =
 {
   QueryInterface : function(aIID)
   {
     if(aIID.equals(Components.interfaces.nsIWebProgressListener))
       return this;
     throw Components.results.NS_NOINTERFACE;
   },

   init : function()
   {
   },

   destroy : function()
   {
   },

   // nsIWebProgressListener
   onProgressChange : function (aWebProgress, aRequest,
                                aCurSelfProgress, aMaxSelfProgress,
                                aCurTotalProgress, aMaxTotalProgress)
   {
   },

   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
   {
   },

   onLocationChange : function(aWebProgress, aRequest, aLocation)
   {
   },

   onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
   {

   },

  onSecurityChange : function(aWebProgress, aRequest, aState)
   {
   }


 }

 var Module = {
    firstTime: true,

    registerSelf: function (compMgr, fileSpec, location, type) {
        if (this.firstTime) {
            this.firstTime = false;
            throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
        }

        compMgr =
compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        "DocToString",
                                        this.myProgID,
                                        fileSpec,
                                        location,
                                        type);
    },

    getClassObject : function (compMgr, cid, iid) {
        if (!cid.equals(this.myCID))
        throw Components.results.NS_ERROR_NO_INTERFACE
        if (!iid.equals(Components.interfaces.nsIFactory))
        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
        return this.myFactory;
    },

    myCID: Components.ID("{113e2a49-8c04-443c-93c1-99db79608246}"),
    myProgID: "@mozilla.org/DocToString;1",

    myFactory: {
        createInstance: function (outer, iid) {
            if (outer != null)
            throw Components.results.NS_ERROR_NO_AGGREGATION;
            return (new DocToString()).QueryInterface(iid);
        }
    },

    canUnload: function(compMgr) {
        return true;
    }
};
function NSGetModule(compMgr, fileSpec) { return Module; }
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to