VC 通过IHTMLINTEFACE 接口实现网页执行自定义js代码

  1 VC 通过IHTMLINTEFACE 接口实现网页执行自定义js代码

  2 IHTMLDocument2 *pDoc2

  3 

  4 ......

  5 

  6 IHTMLWindow2 *pWindow;

  7 HRESULT hr=pDoc2->get_parentWindow(&pWindow);

  8 VARIANT ret;

  9 ret.vt=VT_EMPTY;

 10 CComBSTR func="alert(document.cookie)";

 11 CComBSTR lang="JAVAScript";

 12 hr=pWindow->execScript(func,lang,&ret);

 13 

 14  

 15 

 16 //首先只要得到 ihtmldocument2 接口即可

 17 BOOL CIEop::putValue(CString IDorName,CString put_value,DWORD ChoosePtr){

 18  IHTMLElementCollection *pAllElement=NULL;

 19  if (m_pHTMLDocument2==NULL)

 20  {

 21   OutputDebugString(_T("LTDLL putValue Failed m_pHTMLDocument2==null"));

 22   // initcom();

 23   return FALSE;

 24  }

 25  m_pHTMLDocument2->get_all(&pAllElement);

 26  if (pAllElement==NULL)

 27  {

 28   MessageBox(0,_T("putValue pAllElement is NULL "),_T("错误提示"),MB_OK);

 29   return FALSE;

 30  }

 31  CComPtr<IDispatch> pDispIE;

 32  HRESULT hr;

 33  switch (ChoosePtr)

 34  {

 35  case  Choose_IHTMLInputTextElment:{

 36   hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);

 37   if (FAILED(hr))

 38   {

 39    MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);

 40    return FALSE;

 41   }

 42   CComQIPtr<IHTMLInputTextElement,&IID_IHTMLInputTextElement> pElement;

 43   pElement=pDispIE;

 44   put_value.ReleaseBuffer();

 45   pElement->put_value(put_value.AllocSysString());

 46            }

 47            break;

 48  case Choose_JavaScript:{

 49   hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);

 50   if (FAILED(hr))

 51   {

 52    MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);

 53    return FALSE;

 54   }

 55   IHTMLWindow2* pIHTMLWindow = NULL;

 56   m_pHTMLDocument2->get_parentWindow(&pIHTMLWindow);

 57   put_value.ReleaseBuffer();

 58   VARIANT        pvarRet;

 59   pIHTMLWindow->execScript(CComBSTR(put_value.AllocSysString()), CComBSTR("JavaScript"), &pvarRet);

 60   pIHTMLWindow->Release(); 

 61          }

 62          break;

 63  case Choose_Click:{

 64   hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);

 65   if (FAILED(hr))

 66   {

 67    //MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);

 68    return FALSE;

 69   }

 70   CComQIPtr<IHTMLElement,&IID_IHTMLElement> pElement;

 71   if (pDispIE==NULL)

 72   {

 73    return FALSE;

 74   }

 75   pElement=pDispIE;

 76   pElement->click();

 77 //   VARIANT vOnclick;

 78 // 

 79 //     pElement->put_onclick(&vOnclick);

 80        }

 81        break;

 82  case Choose_IHTMLFormElement :

 83   {

 84    hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);

 85    if (FAILED(hr))

 86    {

 87     MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);

 88     return FALSE;

 89    }

 90    CComQIPtr<IHTMLFormElement,&IID_IHTMLFormElement> pElement1;

 91    pElement1=pDispIE;

 92   // pElement1->put_target(CComBSTR("_blank"));

 93    pElement1->put_target(CComBSTR(_T("_self")));

 94    pElement1->put_action(CComBSTR(_T("action.php")));

 95    pElement1->submit();

 96   }

 97   break;

 98  case CHoose_GetText:

 99   {

100    hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);

101    if (FAILED(hr))

102    {

103     MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);

104     return FALSE;

105    }

106 //    CComQIPtr<IHTMLElement,&IID_IHTMLElement> pElement1;

107 //    pElement1=pDispIE;

108 //    pElement1->get_innerText(&m_strVerify);

109 

110   }

111   break;

112  }

113  return TRUE;

114 }

 

你可能感兴趣的:(html)