Example #1
0
/*-----------------------------------------------------------------------------
  Recursively count the number of DOM elements on the page
-----------------------------------------------------------------------------*/
DWORD CPagetestBase::CountDOMElements(CComQIPtr<IHTMLDocument2> &doc)
{
  DWORD count = 0;

  if( doc )
  {
    // count the number of elements on the current document
		CComPtr<IHTMLElementCollection> coll;
		if( SUCCEEDED(doc->get_all(&coll)) && coll )
		{
			long nodes = 0;
			if( SUCCEEDED(coll->get_length(&nodes)) )
        count += nodes;
      coll.Release();
    }

    // walk any/all iFrames
		CComQIPtr<IOleContainer> ole(doc);
		if(ole)
		{
			CComPtr<IEnumUnknown> objects;
			if( SUCCEEDED(ole->EnumObjects(OLECONTF_EMBEDDINGS, &objects)) && objects )
			{
				IUnknown* pUnk;
				ULONG uFetched;
				while( S_OK == objects->Next(1, &pUnk, &uFetched) )
				{
					CComQIPtr<IWebBrowser2> browser(pUnk);
					pUnk->Release();
					if (browser)
					{
						CComPtr<IDispatch> disp;
						if( SUCCEEDED(browser->get_Document(&disp)) && disp )
						{
							CComQIPtr<IHTMLDocument2> frameDoc(disp);
							if (frameDoc)
								count += CountDOMElements(frameDoc);
						}
					}
				}
			}
		}			
  }

  return count;
}
Example #2
0
/*-----------------------------------------------------------------------------
  Recursively count the number of DOM elements on the page
-----------------------------------------------------------------------------*/
DWORD CPagetestBase::CountDOMElements(CComQIPtr<IHTMLDocument2> &doc)
{
  DWORD count = 0;

  if( doc )
  {
    // count the number of elements on the current document
		CComPtr<IHTMLElementCollection> coll;
		if( SUCCEEDED(doc->get_all(&coll)) && coll )
		{
			long nodes = 0;
			if( SUCCEEDED(coll->get_length(&nodes)) )
        count += nodes;
      coll.Release();
    }

    // Recursively walk any iFrames
    IHTMLFramesCollection2 * frames = NULL;
    if (doc->get_frames(&frames) && frames) {
      long num_frames = 0;
      if (SUCCEEDED(frames->get_length(&num_frames))) {
        for (long i = 0; i < num_frames; i++) {
          _variant_t index = i;
          _variant_t varFrame;
          if (SUCCEEDED(frames->item(&index, &varFrame))) {
            CComQIPtr<IHTMLWindow2> window(varFrame);
            if (window) {
              CComQIPtr<IHTMLDocument2> frameDoc;
              frameDoc = HtmlWindowToHtmlDocument(window);
              if (frameDoc)
                count += CountDOMElements(frameDoc);
            }
          }
        }
      }
      frames->Release();
    }
  }

  return count;
}