void ExplorerElement::getTagName(std::string & value) { IHTMLElement *e; value.clear (); HRESULT hr = m_pElement->QueryInterface(IID_IHTMLElement, reinterpret_cast<void**>(&e)); if (!FAILED(hr) && e != NULL) { BSTR bstrTag; hr = e->get_tagName(&bstrTag); if (!FAILED(hr) && bstrTag != NULL) { Utils::bstr2str(value, bstrTag); SysFreeString(bstrTag); } e->Release(); } }
HRESULT STDMETHODCALLTYPE CElementEvents::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult, EXCEPINFO* pexcepinfo, UINT* puArgErr) { UNREFERENCED_PARAMETER(puArgErr); UNREFERENCED_PARAMETER(pexcepinfo); UNREFERENCED_PARAMETER(pvarResult); UNREFERENCED_PARAMETER(riid); UNREFERENCED_PARAMETER(lcid); UNREFERENCED_PARAMETER(wFlags); // try to get IHTMLEventObj IDispatch* param1=NULL; if (pdispparams->cArgs==1 && (pdispparams->rgvarg[0].vt==VT_DISPATCH)) param1=pdispparams->rgvarg[0].pdispVal; IHTMLEventObj* pEvtObj=NULL; if (param1) param1->QueryInterface(IID_IHTMLEventObj, (void**)&pEvtObj); // see HTMLElementEvents2 Dispinterface for available values (include mshtmdid.h) // disable context menu by default (but for INPUT) switch (dispidMember) { case DISPID_HTMLELEMENTEVENTS2_ONCONTEXTMENU: if (this->bEnableContextMenu) break; // else { IHTMLElement* pHTMLElement; BSTR String; // get element source of event HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement); if (SUCCEEDED(hr) && pHTMLElement) { // get element name pHTMLElement->get_tagName(&String); if (String==NULL) String=SysAllocString(L""); // apply for all but input if (wcsicmp(String,L"INPUT")!=0) { VARIANT v; v.vt=VT_BOOL; v.boolVal=VARIANT_FALSE; pEvtObj->put_returnValue(v); } // Free memory and release objects SysFreeString(String); pHTMLElement->Release(); } } break; // disable selection but for input case DISPID_HTMLELEMENTEVENTS2_ONSELECTSTART: if (this->bEnableSelection) break; // else { IHTMLElement* pHTMLElement; BSTR String; // get element source of event HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement); if (SUCCEEDED(hr) && pHTMLElement) { // get element name pHTMLElement->get_tagName(&String); if (String==NULL) String=SysAllocString(L""); // apply for all but input if (wcsicmp(String,L"INPUT")!=0) { VARIANT v; v.vt=VT_BOOL; v.boolVal=VARIANT_FALSE; pEvtObj->put_returnValue(v); } // Free memory and release objects SysFreeString(String); pHTMLElement->Release(); } } break; // disable selection but for input case DISPID_HTMLELEMENTEVENTS2_ONMOUSEOVER: if (this->bEnableSelection || (this->pCmdTarget==NULL)) break; // else { VARIANT var; var.vt=VT_BOOL; IHTMLElement* pHTMLElement; BSTR String; // get element source of event HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement); if (SUCCEEDED(hr) && pHTMLElement) { // get element name pHTMLElement->get_tagName(&String); if (String==NULL) String=SysAllocString(L""); // apply for all but input, link and img if ( (wcsicmp(String,L"INPUT")!=0) && (wcsicmp(String,L"A")!=0) && (wcsicmp(String,L"IMG")!=0) ) { // stop cursor automatic mode var.boolVal = VARIANT_TRUE; this->pCmdTarget->Exec(&CGID_MSHTML, IDM_OVERRIDE_CURSOR, OLECMDEXECOPT_DODEFAULT, &var, NULL); // Set cursor to arrow this->hCursorOld=::SetCursor(this->hCursorArrow); } // Free memory and release objects SysFreeString(String); pHTMLElement->Release(); } } break; case DISPID_HTMLELEMENTEVENTS2_ONMOUSEOUT: if (this->bEnableSelection || (this->pCmdTarget==NULL)) break; // else { VARIANT var; var.vt=VT_BOOL; IHTMLElement* pHTMLElement; BSTR String; // get element source of event HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement); if (SUCCEEDED(hr) && pHTMLElement) { // get element name pHTMLElement->get_tagName(&String); if (String==NULL) String=SysAllocString(L""); // apply for all but input, link and img if ( (wcsicmp(String,L"INPUT")!=0) && (wcsicmp(String,L"A")!=0) && (wcsicmp(String,L"IMG")!=0) ) { // restore cursor automatic mode var.boolVal = VARIANT_FALSE; ::SetCursor(this->hCursorOld); this->pCmdTarget->Exec(&CGID_MSHTML, IDM_OVERRIDE_CURSOR, OLECMDEXECOPT_DODEFAULT, &var, NULL); } // Free memory and release objects SysFreeString(String); pHTMLElement->Release(); } } break; case DISPID_HTMLELEMENTEVENTS2_ONDRAGSTART: if (this->bEnableSelection) break; { VARIANT v; v.vt=VT_BOOL; v.boolVal=VARIANT_FALSE; pEvtObj->put_returnValue(v); } break; } // call callback if (this->ElementEventsCallBack) { IHTMLElement* pHTMLElement; BSTR String; // get element source of event HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement); if (SUCCEEDED(hr) && pHTMLElement) { // get element id pHTMLElement->get_id(&String); if (String==NULL) String=SysAllocString(L""); // call callback this->ElementEventsCallBack(dispidMember,String,this->ElementEventsCallBackUserParam); // Free memory and release objects SysFreeString(String); pHTMLElement->Release(); } } if (this->ElementEventsCallBackEx) { this->ElementEventsCallBackEx(dispidMember,pEvtObj,this->ElementEventsCallBackExUserParam); } // release IHTMLEventObj if (pEvtObj) pEvtObj->Release(); return S_OK; }
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; }
bool CMyInternetExplorer::FindOption (bool bClick, bool bValue, bool bText, LPCTSTR sValue, LPCTSTR sText) { 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; sTempTagName.MakeLower (); SysFreeString (bstrTagName); } if (sTempTagName == _T ("option")) { IHTMLOptionElement * pOption = NULL; hr = pElemDispatch->QueryInterface (IID_IHTMLOptionElement, (void**)&pOption); if (SUCCEEDED (hr) && (pOption != NULL)) { BSTR bstrValue, bstrText; CString sTempValue, sTempText; if (!FAILED (pOption->get_value (&bstrValue))) { sTempValue = bstrValue; SysFreeString (bstrValue); } if (!FAILED (pOption->get_text (&bstrText))) { sTempText = bstrText; SysFreeString (bstrText); } //if (IDCANCEL == AfxMessageBox (_T ("Value: ") + sTempValue + _T ("\r\nText: ") + sTempText, MB_OKCANCEL)) // bSearch = false; // Do the comparison here! bool bMatches = true; if (bMatches && bValue) { if (!StringHelper::WildcardCompareNoCase (sValue, sTempValue)) bMatches = false; } if (bMatches && bText) { if (!StringHelper::WildcardCompareNoCase (sText, sTempText)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; pOption->put_selected (VARIANT_TRUE); if (bClick) pElem->click (); } pOption->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }
bool CMyInternetExplorer::GetInput (IDispatch* pHtmlDocDispatch,bool bClick, bool bSelect, bool bChangeValue, bool bSetCheck, bool bType, bool bName, bool bValue, LPCTSTR sTypeToLook, LPCTSTR sNameToLook, LPCTSTR sValueToLook, bool bNewCheckValue, LPCTSTR sNewValue,LPTSTR szValue) { // 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 (true && (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; sTempTagName.MakeLower (); //AfxMessageBox (sTempTagName); SysFreeString (bstrTagName); } if (sTempTagName == _T ("input")) { IHTMLInputElement * pInputElem = NULL; hr = pElemDispatch->QueryInterface (IID_IHTMLInputElement, (void**)&pInputElem); if (SUCCEEDED (hr) && (pInputElem != NULL)) { BSTR bstrType, bstrName, bstrValue; CString sTempType, sTempName, sTempValue; if (!FAILED (pInputElem->get_type (&bstrType))) { sTempType = bstrType; SysFreeString (bstrType); } if (!FAILED (pInputElem->get_name (&bstrName))) { sTempName = bstrName; SysFreeString (bstrName); } if (!FAILED (pInputElem->get_value (&bstrValue))) { sTempValue = bstrValue; SysFreeString (bstrValue); } //AfxMessageBox (_T ("Name: ") + sTempName + _T ("\r\nType: ") + sTempType + _T ("\r\nValue: ") + sTempValue); // Do the comparison here! lstrcpy(szValue,sTempValue); bool bMatches = true; if (bMatches && bType) { if (!StringHelper::WildcardCompareNoCase (sTypeToLook, sTempType)) bMatches = false; } if (bMatches && bName) { if (!StringHelper::WildcardCompareNoCase (sNameToLook, sTempName)) bMatches = false; } if (bMatches && bValue) { if (!StringHelper::WildcardCompareNoCase (sValueToLook, sTempValue)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; if (bSetCheck) { if (bNewCheckValue) pInputElem->put_checked (VARIANT_TRUE); else pInputElem->put_checked (VARIANT_FALSE); } if (bChangeValue) { CString sTemp (sNewValue); BSTR bstrNewValue = sTemp.AllocSysString (); pInputElem->put_value (bstrNewValue); SysFreeString (bstrNewValue); } if (bSelect) pInputElem->select (); if (bClick) pElem->click (); } pInputElem->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }
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; }