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

总结:如何让下载的第三方网页执行本地脚本(利用CHtmlView系列控件)

2009-07-19 13:17 856 查看
如下给网页增加脚本,然后调用就可以了

其中GetElementByHPath函数可能没有,目的是找到body元素。

HRESULT AppendJavaScript(IDispatch* pDocument, LPCTSTR lpszText, bool bIsSrc)

{

CComQIPtr<IHTMLDocument2> pDocument2 = pDocument;

if (pDocument2 == NULL || lpszText == NULL || *lpszText == 0)

return E_FAIL;

CComQIPtr<IHTMLElement> pElement;

CComQIPtr<IHTMLScriptElement> pScriptElement;

pDocument2->createElement(CComBSTR("script"), &pElement);

pScriptElement = pElement;

if (pScriptElement == NULL)

return E_FAIL;

pScriptElement->put_type(L"text/ecmascript");

pScriptElement->put_defer(VARIANT_TRUE);

if (bIsSrc)

pScriptElement->put_src(_bstr_t(lpszText));

else

pScriptElement->put_text(_bstr_t(lpszText));

CComQIPtr<IHTMLElement> pBody = GetElementByHPath(pDocument2, L"TagName=body");

CComQIPtr<IHTMLDOMNode> pBodyNode = pBody;

CComQIPtr<IHTMLDOMNode> pNewNode;

HRESULT hr = pBodyNode->appendChild(CComQIPtr<IHTMLDOMNode>(pElement), &pNewNode);

return hr;

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