🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #175

Re: Extracting all the HTMLElements from IHTMLDocument2

#175
From: vanmmd
Date: Fri, 28 Sep 2007 11:27
192 lines
7364 bytes
Hi Remy,
Thanks much for the hint. From the beginning, i assumed that the code
fails because it tries to grab links from not-yet-fully-loaded
iframes. Totally agree with you.

As a Java programmer, i am getting familiar with the Borland C
library. Could you please confirm that the following code snippet will
do busy-waiting till the iframe has been fully loaded:
while(!(bp->ReadyState == Shdocvw_tlb::READYSTATE_COMPLETE)) {
	// Busy-waiting
}
// NOW the iframe and webpage are supposed to be fully loaded

Following is the full picture of our code. Regrettably, it's still
stuck with the CNN.COM, we never can grab any link from the iframes.
********************************************************************************************************
// BrowserDocumentComplete: This caller will call service getFrame and
getLinks to extract all the links inside the webpage
void __fastcall TMainForm::BrowserDocumentComplete(TObject *Sender,
      LPDISPATCH pDisp, Variant *URL)
{
  // Van's note:  Inject this bug intentionally. The app fails even
when user click the Web Btn. Why?
  // Why this function is called even when user clicks the Web Btn,
hasn't browse any webpage yet
  /*
  IHTMLDocument2 *dummmy_p1=NULL;
  assert(dummmy_p1!=NULL);
  */

  BrowserReady=true;
  WWWIsLoading=false;
  MainForm->WWWCurrentLink=0;
  WWWResetScanning();
  //
  if (WWWNumLinks>0) {
    FocusLink(Browser, 0);
  }

  WWWFrameCount=FrameCount(Browser);
  WWWCurrentFrame=0;

  if (WWWFrameCount==0) {
      assert(WWWCurrentLink<WWWNumLinks);
      assert(WWWCurrentLink>=0);
      WWWCurrentLinkp= GetLink(Browser, WWWCurrentLink);
  } else {
    // ToDo: Check if all frames are link-less.
    IHTMLDocument2 *thisframe=NULL;
    WWWCurrentFrame=WWWFrameCount-1;    // Van's note: Extract the
links from the top-most frame
    //
    thisframe=GetFrame(Browser, WWWCurrentFrame);

    //
    if(thisframe!=NULL) {
        assert(thisframe!=NULL);
        long linksthisframe=NumLinks(thisframe);

        if (WWWLinkSelected) {
                //assert(WWWCurrentLink<linksthisframe); // failed
with cnn.com
                if (WWWCurrentLink<linksthisframe) {
                        assert(WWWCurrentLink>=0);
                        WWWCurrentLinkp=GetLink(thisframe,
WWWCurrentLink);
                        thisframe->Release();
                } else {
                        // Van's note: Do nothing for now
                }
        }
    } else {
        // Do nothing if thisframe==NULL for now.
        // For debugging purpose. MUST NEVER BRANCH THIS WAY
        IHTMLDocument2 *dummmy_p4=NULL;
        assert(dummmy_p4!=NULL);
    }
  }     // End of if (WWWFrameCount==0)

  WWWLinkSelected=true;
  if (WWWCurrentLinkp!=NULL){
    getlinkborder(WWWCurrentLinkp, WWWCurrentLinkStyleWas);
    setlinkborder(WWWCurrentLinkp, "1.0mm solid red");
  } else {
     // accept that we do nothing if WWWCurrentLinkp == NULL
     // For debugging purpose. MUST NEVER BRANCH THIS WAY
     IHTMLDocument2 *dummmy_p5=NULL;
     assert(dummmy_p5!=NULL);
  }
  if (WWWScanMode == WWWScanModeStr[0]) MainForm->WWWLinkScanTimer-
>Enabled=false;
//--------------------

  // ToDo - scanning mellem frames
  setfields();

}


// Following are the callees
int FrameCount(TCppWebBrowser *bp) {
  IHTMLDocument2 *HTMLDoc = NULL;

  long collSize=0;

  // replaced by this standard code???
  if( (bp->ReadyState == Shdocvw_tlb::READYSTATE_COMPLETE) && (bp-
>Document) &&
        SUCCEEDED(bp->Document->QueryInterface(IID_IHTMLDocument2,
        (LPVOID*)&HTMLDoc)) )
  {
    IHTMLFramesCollection2 *pColl = NULL;

    if(SUCCEEDED(HTMLDoc->get_frames(&pColl))) {
      pColl->get_length(&collSize);
      pColl->Release();
    }
  }
  if (HTMLDoc) HTMLDoc->Release();
  return collSize;
}

// van's note: Failed when deal with IFRAMEs
IHTMLDocument2* GetFrame(TCppWebBrowser *bp, int idx) {
  IHTMLDocument2 *HTMLDoc = NULL, *res=NULL;

  long collSize=0;

  // Busy waiting till the iframe has been fully loaded
  while (!CompletedLoading(bp)) {

  };

  if( (bp->ReadyState == Shdocvw_tlb::READYSTATE_COMPLETE) && (bp-
>Document) &&
        SUCCEEDED(bp->Document->QueryInterface(IID_IHTMLDocument2,
        (LPVOID*)&HTMLDoc)) )
  {
    IHTMLFramesCollection2 *pColl = NULL;

    if(SUCCEEDED(HTMLDoc->get_frames(&pColl))) {
      pColl->get_length(&collSize);

      assert(collSize>0);

      if (collSize>idx) {
        VARIANT one;
        VariantInit(&one);      // v's
        one.vt = VT_UINT;
        one.lVal = idx;

        VARIANT out;
        VariantInit(&out);      // v's
        pColl->item(&one,&out);
        //
        IHTMLWindow2 *win=NULL;
        if (SUCCEEDED(out.pdispVal->QueryInterface( IID_IHTMLWindow2,
(LPVOID*)&win ))) {
          // van's note
          assert(win);   // passed with CNN.com
          // van's note: Can not pass with CNN.com. Why we can not
assign win->get_document(&res)????
          // van's note: For some reasson, this assignment is failed
with some webpage
          if (!SUCCEEDED(win->get_document(&res))) {
            res=NULL;
          }
          win->Release();
        } else {
          res=NULL;
        } // end if (SUCCEEDED(out.pdispVal->....

	VariantClear(&one);       // v's
	VariantClear(&out);       // v's
      } // end if (collSize>idx)

      pColl->Release();
    } // end if(SUCCEEDED(HTMLDoc->get...
  } // end if( (bp->ReadyState == .....

  if (HTMLDoc) HTMLDoc->Release();
  // van's note: Bug 2: This assertion failed with CNN.COM. The
function return null.
  // For debugging purpose. MUST NEVER FAILS. REGRETABBLY, STILL FAILS
  assert(res);
  return res;
}
//
bool CompletedLoading(TCppWebBrowser *bp) {
  return bp->ReadyState==Shdocvw_tlb::READYSTATE_COMPLETE;
}
********************************************************************************************************
Technical Details
Message-ID: <1190978874.563094.112220@k79g2000hse.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!k79g2000hse.googlegroups.com!not-for-mail
References: <46c061ea$1@newsgroups.borland.com> <46c09536@newsgroups.borland.com> <46f7e838$1@newsgroups.borland.com> <46f7f81a@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,26c174002c4041d7
X-Google-Attributes: gidfeeec,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!k79g2000hse.googlegroups.com!not-for-mail
From:  vanmmd <suskovivor@yahoo.com>
Newsgroups: borland.public.cppbuilder.internet.web
Subject: Re: Extracting all the HTMLElements from IHTMLDocument2
Date: Fri, 28 Sep 2007 04:27:54 -0700
Organization: http://groups.google.com
Lines: 193
Message-ID: <1190978874.563094.112220@k79g2000hse.googlegroups.com>
References: <46c061ea$1@newsgroups.borland.com>
   <46c09536@newsgroups.borland.com>
   <46f7e838$1@newsgroups.borland.com>
   <46f7f81a@newsgroups.borland.com>
NNTP-Posting-Host: 130.226.188.2
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1190978875 14593 127.0.0.1 (28 Sep 2007 11:27:55 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Fri, 28 Sep 2007 11:27:55 +0000 (UTC)
In-Reply-To: <46f7f81a@newsgroups.borland.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.1),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: k79g2000hse.googlegroups.com; posting-host=130.226.188.2;
   posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0