您的位置:首页 > Web前端 > JavaScript

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

2012-06-17 10:44 411 查看
VC 通过IHTMLINTEFACE 接口实现网页执行自定义js代码
IHTMLDocument2 *pDoc2

......

IHTMLWindow2 *pWindow;
HRESULT hr=pDoc2->get_parentWindow(&pWindow);
VARIANT ret;
ret.vt=VT_EMPTY;
CComBSTR func="alert(document.cookie)";
CComBSTR lang="JAVAScript";
hr=pWindow->execScript(func,lang,&ret);

//首先只要得到 ihtmldocument2 接口即可
BOOL CIEop::putValue(CString IDorName,CString put_value,DWORD ChoosePtr){
IHTMLElementCollection *pAllElement=NULL;
if (m_pHTMLDocument2==NULL)
{
OutputDebugString(_T("LTDLL putValue Failed m_pHTMLDocument2==null"));
// initcom();
return FALSE;
}
m_pHTMLDocument2->get_all(&pAllElement);
if (pAllElement==NULL)
{
MessageBox(0,_T("putValue pAllElement is NULL "),_T("错误提示"),MB_OK);
return FALSE;
}
CComPtr<IDispatch> pDispIE;
HRESULT hr;
switch (ChoosePtr)
{
case  Choose_IHTMLInputTextElment:{
hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);
if (FAILED(hr))
{
MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);
return FALSE;
}
CComQIPtr<IHTMLInputTextElement,&IID_IHTMLInputTextElement> pElement;
pElement=pDispIE;
put_value.ReleaseBuffer();
pElement->put_value(put_value.AllocSysString());
}
break;
case Choose_JavaScript:{
hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);
if (FAILED(hr))
{
MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);
return FALSE;
}
IHTMLWindow2* pIHTMLWindow = NULL;
m_pHTMLDocument2->get_parentWindow(&pIHTMLWindow);
put_value.ReleaseBuffer();
VARIANT        pvarRet;
pIHTMLWindow->execScript(CComBSTR(put_value.AllocSysString()), CComBSTR("JavaScript"), &pvarRet);
pIHTMLWindow->Release();
}
break;
case Choose_Click:{
hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);
if (FAILED(hr))
{
//MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);
return FALSE;
}
CComQIPtr<IHTMLElement,&IID_IHTMLElement> pElement;
if (pDispIE==NULL)
{
return FALSE;
}
pElement=pDispIE;
pElement->click();
//   VARIANT vOnclick;
//
//     pElement->put_onclick(&vOnclick);
}
break;
case Choose_IHTMLFormElement :
{
hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);
if (FAILED(hr))
{
MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);
return FALSE;
}
CComQIPtr<IHTMLFormElement,&IID_IHTMLFormElement> pElement1;
pElement1=pDispIE;
// pElement1->put_target(CComBSTR("_blank"));
pElement1->put_target(CComBSTR(_T("_self")));
pElement1->put_action(CComBSTR(_T("action.php")));
pElement1->submit();
}
break;
case CHoose_GetText:
{
hr= pAllElement->item(CComVariant(IDorName.AllocSysString()),CComVariant((long)0),&pDispIE);
if (FAILED(hr))
{
MessageBox(0,_T("putValue pDispIE is NULL put_value="+put_value),_T("错误提示"),MB_OK);
return FALSE;
}
//    CComQIPtr<IHTMLElement,&IID_IHTMLElement> pElement1;
//    pElement1=pDispIE;
//    pElement1->get_innerText(&m_strVerify);

}
break;
}
return TRUE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐