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

如下给网页增加脚本,然后调用就可以了

其中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;
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值