BSTR IEView::getHrefFromAnchor(IHTMLElement *element) { if (element != NULL) { IHTMLAnchorElement * pAnchor; if (SUCCEEDED(element->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor)) && (pAnchor!=NULL)) { BSTR url; BSTR url2; pAnchor->get_href( &url ); if (url!=NULL) { url2 = wcsdup(url); SysFreeString(url); url = url2; } pAnchor->Release(); return url; } else { IHTMLElement * parent; if (SUCCEEDED(element->get_parentElement(&parent)) && (parent!=NULL)) { BSTR url = getHrefFromAnchor(parent); parent->Release(); return url; } } } return NULL; }
void CBrowserDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL) { // TODO: Add your message handler code here if(logout){ CString u=ie.get_LocationURL(); if(u.Find(L"http://vkontakte.ru/profile.php")==0){ CRect rect; ie.GetWindowRect(rect); IHTMLDocument2 *doc=(IHTMLDocument2*)ie.get_Document(); IHTMLElementCollection* links; doc->get_links(&links); long len=1; links->get_length(&len); for(long i=0;i<len;i++){ IDispatch* _item; _variant_t index = i; HRESULT r=links->item(index, index, &_item); IHTMLAnchorElement* item; _item->QueryInterface(__uuidof(IHTMLAnchorElement), (void**)&item); IHTMLElement* __item; _item->QueryInterface(__uuidof(IHTMLElement), (void**)&__item); wchar_t* href; item->get_href(&href); std::wstring whref=href; if(whref.substr(0, 19).compare(L"http://login.vk.com")==0){ __item->click(); logoutClicked=true; TRACE("Clicked!\n"); return; } //delete href; } } } }
bool CMyInternetExplorer::FindAnchor (bool bClick, bool bFocus, bool bName, bool bOuterText, bool bTooltip, bool bURL, LPCTSTR sName, LPCTSTR sOuterText, LPCTSTR sTooltip, LPCTSTR sURL) { 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)) { // 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; CString sTempTagName; if (!FAILED (pElem->get_tagName (&bstrTagName))) { sTempTagName = bstrTagName; SysFreeString (bstrTagName); } if (sTempTagName == _T ("a") || sTempTagName == _T ("A")) { IHTMLAnchorElement * pAnchor = NULL; hr = pElemDispatch->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor); if (SUCCEEDED (hr) && (pAnchor != NULL)) { BSTR bstrName, bstrOuterText, bstrURL, bstrTooltip; CString sTempName, sTempOuter, sTempURL, sTempTooltip; if (!FAILED (pElem->get_outerText (&bstrOuterText))) { sTempOuter = bstrOuterText; SysFreeString (bstrOuterText); } if (!FAILED (pElem->get_title (&bstrTooltip))) { sTempTooltip = bstrTooltip; SysFreeString (bstrTooltip); } if (!FAILED (pAnchor->get_name (&bstrName))) { sTempName = bstrName; SysFreeString (bstrName); } if (!FAILED (pAnchor->get_href (&bstrURL))) { sTempURL = bstrURL; SysFreeString (bstrURL); } // Do the comparison here! bool bMatches = true; if (bMatches && bName) { if (!StringHelper::WildcardCompareNoCase (sName, sTempName)) bMatches = false; } if (bMatches && bOuterText) { if (!StringHelper::WildcardCompareNoCase (sOuterText, sTempOuter)) bMatches = false; } if (bMatches && bURL) { if (!StringHelper::WildcardCompareNoCase (sURL, sTempURL)) bMatches = false; } if (bMatches && bTooltip) { if (!StringHelper::WildcardCompareNoCase (sTooltip, sTempTooltip)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; if (bFocus) pAnchor->focus (); if (bClick) pElem->click (); } pAnchor->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }