如下给网页增加脚本,然后调用就可以了
其中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;
}