Esempio n. 1
1
void CNetRadioDlg::OnBnClickedTest()
{
	CString strValue;
	IHTMLDocument2* pDoc = (IHTMLDocument2*)m_Browser.GetHtmlDocument();
	HRESULT hr = S_FALSE;
	IHTMLElementCollection* pAllElem = NULL;
	IHTMLElementCollection* pAllScript = NULL;
	IHTMLElement* pElem = NULL;

	hr = pDoc->get_scripts(&pAllScript);
	if(SUCCEEDED(hr) && pAllScript != NULL)
	{
		IDispatchPtr pElemDisp = NULL;
		_variant_t varID(1);
		_variant_t varIdx(0);
		hr = pAllScript->item(varID, varIdx, &pElemDisp);
		if(SUCCEEDED(hr) && pElemDisp != NULL)
		{
			hr = pElemDisp->QueryInterface(IID_IHTMLElement,(void**)&pElem);
			if(SUCCEEDED(hr) && pElem != NULL)
			{
				BSTR bstrTagName;
				hr = pElem->get_outerHTML(&bstrTagName);
			
				hr = pElem->get_outerHTML(&bstrTagName);
				if(SUCCEEDED(hr))
				{
					CString strTagName(bstrTagName);
					SysFreeString(bstrTagName);
					MessageBox(strTagName);
				}
			}
		}
	}

	hr = pDoc->get_all(&pAllElem);

	if(SUCCEEDED(hr) && pAllElem != NULL)
	{
		IDispatchPtr pElemDisp = NULL;
		_variant_t varID(_T("body"));
		_variant_t varIdx(0);
		hr = pAllElem->item(varID, varIdx, &pElemDisp);
		if(SUCCEEDED(hr) && pElemDisp != NULL)
		{
			MessageBox(_T("OK"));
		}
	}

}
void CIncrementSystemBFDlg::ProcessElementCollection(IHTMLElementCollection* pElemColl, CString id)
{	
	IDispatch* pElemDisp = NULL;
	IHTMLElement* pElem = NULL;
	VARIANT vID;
	VARIANT vIdx;

	VariantInit(&vID);
	vID.vt=VT_BSTR;
	vID.bstrVal=_bstr_t(id);

	VariantInit(&vIdx);
			  
	vIdx.vt=VT_I4;
	vIdx.lVal=0;

	HRESULT hr = pElemColl->item( vID, vIdx, &pElemDisp );
	if ( SUCCEEDED(hr) && pElemDisp != 0x0)
	{
		hr = pElemDisp->QueryInterface( IID_IHTMLElement, (void**)&pElem );
		if ( SUCCEEDED(hr) )
		{
			// Obtained element with ID of "myID".
			BSTR bsHtml;
			pElem->get_outerHTML(&bsHtml);
			CLogFile::WriteLog(bsHtml);

			ConnectButton1( pElem );
			pElem->Release();	
		}
		pElemDisp->Release();
	}
	pElemColl->Release();
}
Esempio n. 3
0
bool CMyInternetExplorer::FindElement (bool bExactMatch, bool bWildcardMatch, bool bCaseSensitive, bool bClick,
									   bool bTagName, bool bOuterHTML, bool bInnerHTML, bool bTooltip,
									   LPCTSTR sTagNameOrg, LPCTSTR sOuterHTMLOrg, LPCTSTR sInnerHTMLOrg, LPCTSTR sTooltipOrg, 
									   CString & sRetTagName, CString & sRetOuterHTML, CString & sRetInnerHTML, CString & sRetTooltip)
{
	ASSERT (m_pWebBrowser != NULL);
	if (m_pWebBrowser == NULL)
		return false;
	
	HRESULT hr;
	IDispatch* pHtmlDocDispatch = NULL;
	IHTMLDocument2 * pHtmlDoc = NULL;
	bool bSearch = true;

	// Retrieve the document object.
	hr = m_pWebBrowser->get_Document (&pHtmlDocDispatch);
	if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
	{
		hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2,  (void**)&pHtmlDoc);
		if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
		{
			IHTMLElementCollection* pColl = NULL;
			hr = pHtmlDoc->get_all (&pColl);

			if (SUCCEEDED (hr) && (pColl != NULL))
			{
				CString sTagName (sTagNameOrg), sOuterHTML (sOuterHTMLOrg), sInnerHTML (sInnerHTMLOrg), sTooltip (sTooltipOrg);
				if (!bCaseSensitive)
				{
					sTagName.MakeLower ();
					sOuterHTML.MakeLower ();
					sInnerHTML.MakeLower ();
					sTooltip.MakeLower ();
				}

				// Obtained the Anchor Collection...
				long nLength = 0;
				pColl->get_length (&nLength);
				
				for (int i = 0; i < nLength && bSearch; i++)
				{
					COleVariant vIdx ((long)i, VT_I4);
					
					IDispatch* pElemDispatch = NULL;
					IHTMLElement * pElem = NULL;
					
					hr = pColl->item (vIdx, vIdx, &pElemDispatch);
					
					if (SUCCEEDED (hr) && (pElemDispatch != NULL))
					{
						hr = pElemDispatch->QueryInterface(IID_IHTMLElement, (void**)&pElem);
						
						if (SUCCEEDED (hr) && (pElem != NULL))
						{
							BSTR bstrTagName, bstrOuterHTML, bstrInnerHTML, bstrTooltip;
							CString sTempTagName, sTempOuterHTML, sTempInnerHTML, sTempTooltip;
							if (!FAILED (pElem->get_tagName (&bstrTagName)))
							{
								sTempTagName = bstrTagName;
								SysFreeString (bstrTagName);
							}
							if (!FAILED (pElem->get_outerHTML (&bstrOuterHTML)))
							{
								sTempOuterHTML = bstrOuterHTML;
								SysFreeString (bstrOuterHTML);
							}
							if (!FAILED (pElem->get_innerHTML (&bstrInnerHTML)))
							{
								sTempInnerHTML = bstrInnerHTML;
								SysFreeString (bstrInnerHTML);
							}
							if (!FAILED (pElem->get_title (&bstrTooltip)))
							{
								sTempTooltip = bstrTooltip;
								SysFreeString (bstrTooltip);
							}
							
							sRetTagName = sTempTagName;
							sRetOuterHTML = sTempOuterHTML;
							sRetInnerHTML = sTempInnerHTML;
							sRetTooltip = sTempTooltip;

							if (!bCaseSensitive)
							{
								sTempTagName.MakeLower ();
								sTempOuterHTML.MakeLower ();
								sTempInnerHTML.MakeLower ();
								sTempTooltip.MakeLower ();
							}

							// Do the comparison here!
							bool bMatches = true;
							if (bMatches && bTagName)
							{
								if (bExactMatch)
								{
									if (sTempTagName != sTagName)
										bMatches = false;
								}
								else if (bWildcardMatch)
								{
									if (bCaseSensitive)
									{
										if (!StringHelper::WildcardCompareNoCase (sTagName, sTempTagName))
											bMatches = false;
									}
									else
									{
										if (!StringHelper::WildcardCompare (sTagName, sTempTagName))
											bMatches = false;
									}
								}
								// Just locate the text inside!
								else
								{
									if (sTempTagName.Find (sTagName) == -1)
										bMatches = false;
								}
							}
							if (bMatches && bOuterHTML)
							{
								if (bExactMatch)
								{
									if (sTempOuterHTML != sOuterHTML)
										bMatches = false;
								}
								else if (bWildcardMatch)
								{
									if (bCaseSensitive)
									{
										if (!StringHelper::WildcardCompareNoCase (sOuterHTML, sTempOuterHTML))
											bMatches = false;
									}
									else
									{
										if (!StringHelper::WildcardCompare (sOuterHTML, sTempOuterHTML))
											bMatches = false;
									}
								}
								// Just locate the text inside!
								else
								{
									if (sTempOuterHTML.Find (sOuterHTML) == -1)
										bMatches = false;
								}
							}
							if (bMatches && bInnerHTML)
							{
								if (bExactMatch)
								{
									if (sTempInnerHTML != sInnerHTML)
										bMatches = false;
								}
								else if (bWildcardMatch)
								{
									if (bCaseSensitive)
									{
										if (!StringHelper::WildcardCompareNoCase (sInnerHTML, sTempInnerHTML))
											bMatches = false;
									}
									else
									{
										if (!StringHelper::WildcardCompare (sInnerHTML, sTempInnerHTML))
											bMatches = false;
									}
								}
								// Just locate the text inside!
								else
								{
									if (sTempInnerHTML.Find (sInnerHTML) == -1)
										bMatches = false;
								}
							}
							if (bMatches && bTooltip)
							{
								if (bExactMatch)
								{
									if (sTempTooltip != sTooltip)
										bMatches = false;
								}
								else if (bWildcardMatch)
								{
									if (bCaseSensitive)
									{
										if (!StringHelper::WildcardCompareNoCase (sTooltip, sTempTooltip))
											bMatches = false;
									}
									else
									{
										if (!StringHelper::WildcardCompare (sTooltip, sTempTooltip))
											bMatches = false;
									}
								}
								// Just locate the text inside!
								else
								{
									if (sTempTooltip.Find (sTooltip) == -1)
										bMatches = false;
								}
							}
							// Done with the comparisons!
							if (bMatches)
							{
								//if (IDCANCEL == AfxMessageBox (_T ("Tag: ") + sTempTagName + _T ("\r\nOuter: ") + sTempOuterHTML + _T ("\r\nInner: ") + sTempInnerHTML + _T ("\r\nTooltip: ") + sTempTooltip, MB_OKCANCEL))
								//	bSearch = false;

								// No need to search more!
								bSearch = false;
								if (bClick)
									pElem->click ();
							}
							pElem->Release ();
						}
						pElemDispatch->Release ();
					}		
				}
				pColl->Release ();
			}
			pHtmlDoc->Release();
		}
		pHtmlDocDispatch->Release ();
	}

	sRetTagName.Empty ();
	sRetOuterHTML.Empty ();
	sRetInnerHTML.Empty ();
	sRetTooltip.Empty ();
	
	if (bSearch == false)
		return true;
	
	return false;
}