I am working on a ads/popup blocker BHO and I am trying to access the html of a website from the event "downloadcomplete", so I can filter all the ads and malicious uris.
My code looks something like this:
case DISPID_DOWNLOADCOMPLETE:
{
if(iBrowser) //IWebBrowser2*
{
HRESULT hr;
IUnknown *pUnkBrowser = NULL;
hr = iBrowser->QueryInterface(IID_IUnknown, (void**)&pUnkBrowser);
if( SUCCEEDED(hr) && pUnkBrowser!=NULL)
{
if( SUCCEEDED(hr) )
{
IDispatch* pHtmlDocDispatch = NULL;
IHTMLDocument2 * pHtmlDoc = NULL;
hr = iBrowser->get_Document (&pHtmlDocDispatch);
if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL))
{
hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc);
if (SUCCEEDED (hr) && (pHtmlDoc != NULL))
{
IHTMLElement *pBody = 0;
pHtmlDoc->get_body( &pBody );
// I want to get the html here and filter out the ads but pBody is always null
if(pHtmlDoc) pHtmlDoc->Release();
}
if(pHtmlDocDispatch) pHtmlDocDispatch->Release();
}
}
if(pUnkBrowser) pUnkBrowser->Release();
}
}
return S_OK;
}
break;
How could I access and modify the html from this event?
Wrong event, you can "play" with the dom on the DocumentComplete, not the DownloadComplete.
Also I would advise you to use CComPtr, that way you don't need to call the release() on every interface.