agent/browser/ie/pagetest/PagetestReporting.cpp
/*-----------------------------------------------------------------------------
	Extract all of the links from the provided document
-----------------------------------------------------------------------------*/
Extract all of the links from the provided document
Extract all of the links from the provided document
void CPagetestReporting::GetLinks(CComQIPtr<IHTMLDocument2> &doc, CAtlList<CStringA> &urls)
{
	if( doc )
	{
		CComPtr<IHTMLElementCollection> coll;
		if( SUCCEEDED(doc->get_links(&coll)) && coll )
		{
			long count = 0;
			if( SUCCEEDED(coll->get_length(&count)) )
			{
				for( long i = 0; i < count; i++ )
				{
					_variant_t index = i;
					CComPtr<IDispatch> item;
					if( SUCCEEDED(coll->item(index, index, &item)) && item )
					{
						CStringA linkUrl;
						CComQIPtr<IHTMLLinkElement> link = item;
						if( link )
						{
							_bstr_t bs_href;
							if( SUCCEEDED(link->get_href(bs_href.GetAddress())) )
							{
								CStringA href = bs_href;
								linkUrl = href;
							}
						}
						else
						{
							CComQIPtr<IHTMLAnchorElement> anchor = item;
							if( anchor )
							{
								_bstr_t bs_href;
								if( SUCCEEDED(anchor->get_href(bs_href.GetAddress())) )
								{
									CStringA href = bs_href;
									linkUrl = href;
								}
							}
						}
						if( !linkUrl.IsEmpty() && linkUrl.Left(4) == "http")
						{
							int index = linkUrl.Find("#");
							if( index > -1 )
								linkUrl = linkUrl.Left(index);
							bool found = false;
							POSITION pos = urls.GetHeadPosition();
							while( pos && !found )
								if( urls.GetNext(pos) == linkUrl )
									found = true;
							if( !found )
								urls.AddTail(linkUrl);
						}
					}
				}
			}
		}
	}
}
void CPagetestReporting :: GetLinks ( CComQIPtr < IHTMLDocument2 > & doc , CAtlList < CStringA > & urls ) { if ( doc ) { CComPtr < IHTMLElementCollection > coll ; if ( SUCCEEDED ( doc -> get_links ( & coll ) ) && coll ) { long count = 0 ; if ( SUCCEEDED ( coll -> get_length ( & count ) ) ) { for ( long i = 0 ; i < count ; i ++ ) { _variant_t index = i ; CComPtr < IDispatch > item ; if ( SUCCEEDED ( coll -> item ( index , index , & item ) ) && item ) { CStringA linkUrl ; CComQIPtr < IHTMLLinkElement > link = item ; if ( link ) { _bstr_t bs_href ; if ( SUCCEEDED ( link -> get_href ( bs_href . GetAddress ( ) ) ) ) { CStringA href = bs_href ; linkUrl = href ; } } else { CComQIPtr < IHTMLAnchorElement > anchor = item ; if ( anchor ) { _bstr_t bs_href ; if ( SUCCEEDED ( anchor -> get_href ( bs_href . GetAddress ( ) ) ) ) { CStringA href = bs_href ; linkUrl = href ; } } } if ( ! linkUrl . IsEmpty ( ) && linkUrl . Left ( 4 ) == " " ) { int index = linkUrl . Find ( " " ) ; if ( index > -1 ) linkUrl = linkUrl . Left ( index ) ; bool found = false ; POSITION pos = urls . GetHeadPosition ( ) ; while ( pos && ! found ) if ( urls . GetNext ( pos ) == linkUrl ) found = true ; if ( ! found ) urls . AddTail ( linkUrl ) ; } } } } } } }
Extract all of the links from the provided document
Extract all of the links from the provided document
// walk through them all
 // get the href out
 // get the href out
 // only record http urls
 // strip off any bookmarks from the url
 // make sure we didn't already record this url
