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

381 Views Asked by At

these steps are what i did.

1) i just got IWebBrowser2 interface pointer from pUnkSite in SetSite as most bhos do in usual.

2) in OnDocumentComplete, 2-1) got IHTMLDocument interface pointer successfully from IWebBrowser2. 2-2) got html text from IHTMLDocument

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).

so, when user clicks on "get html" button, i wrote a function to do that like below.

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. (is there any changes on ie8 compared to ie6 ?)

please leave any advises or comments on this problem,

thanks in advance.

1

There are 1 best solutions below

0
On

Try to use outerHTML property to obtain page 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"";
}