简体   繁体   English

如何在用户需要时通过bho获取html文本,而不是在OnDocumentComplete处(ie8以上)

[英]how to get html text through bho at when user want, not at OnDocumentComplete (above ie8)

these steps are what i did. 这些步骤是我所做的。

1) i just got IWebBrowser2 interface pointer from pUnkSite in SetSite as most bhos do in usual. 1)我只是像大多数bho一样从SetSite中的pUnkSite获取了IWebBrowser2接口指针。

2) in OnDocumentComplete, 2-1) got IHTMLDocument interface pointer successfully from IWebBrowser2. 2)在OnDocumentComplete中,2-1)从IWebBrowser2成功获取了IHTMLDocument接口指针。 2-2) got html text from IHTMLDocument 2-2)从IHTMLDocument获取html文本

i confirmed that these steps above worked correctly. 我确认上述这些步骤正常工作。

but what i really want to do is that the bho shows messagebox containing the html of current page whenever user want to get html text (for example, user clicks on "get html" button). 但是我真正想做的是,每当用户想要获取html文本时,bho就会显示包含当前页面html的消息框(例如,用户单击“获取html”按钮)。

so, when user clicks on "get html" button, i wrote a function to do that like below. 因此,当用户单击“获取html”按钮时,我编写了一个函数,如下所示。

void CBHO::ClickedOnGetHtml()
{
    CComPtr<IDispatch> spDispDoc; 
    HRESULT hr = m_spWebBrowser->get_Document(&spDispDoc);  // m_spWebBrowser from SetSite
    if (SUCCEEDED(hr))
    {
        CComQIPtr<IHTMLDocument2> spHtmlDoc;
        spHtmlDoc = spDispDoc;
        CComPtr<IDispatch> spDisp;   
        spHtmlDoc->get_Script(&spDisp);        <- exception occured here in ie8. (worked correctly in ie6, but not in ie8.)
    }
}

this is the call stacks at the exception occured. 这是发生异常时的调用堆栈。

mshtml.dll!GetCurrentServiceProvider()  + 0xc bytes
mshtml.dll!GetCallerCommandTarget()  + 0xa6 bytes
mshtml.dll!COmWindowProxy::SecureObject()  - 0x600c5 bytes
mshtml.dll!CDocument::get_Script()  + 0x9c bytes
BHO.dll!CBHO::ClickedOnGetHtml()  line 37 + 0x2d bytes C++

more interesting thing is that it worked correctly in ie6, but not worked in ie8. 更有趣的是,它在ie6中正常工作,但在ie8中无效。 (is there any changes on ie8 compared to ie6 ?) (与ie6相比,ie8上是否有任何变化?)

please leave any advises or comments on this problem, 请就此问题留下任何建议或意见,

thanks in advance. 提前致谢。

Try to use outerHTML property to obtain page HTML: 尝试使用outerHTML属性获取页面HTML:

CString GetOuterHTML(IWebBrowser2* pWebBrowser)
{
    CComDispatchDriver pDocDisp; 
    if(SUCCEEDED(pWebBrowser->get_Document(&pDocDisp)) && pDocDisp != NULL)
    {
        CComQIPtr<IHTMLDocument3> pDoc3 = pDocDisp;
        if(pDoc3 != NULL)
        {
            CComPtr<IHTMLElement> pRootElem;
            if(SUCCEEDED(pDoc3->get_documentElement(&pRootElem)) && pRootElem != NULL)
            {
                CComBSTR bstrText;
                if(SUCCEEDED(pRootElem->get_outerHTML(&bstrText)))
                {
                    return bstrText;
                }
            }
        }
    }
    return L"";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM