在事件 DownloadComplete 上访问 HTML 源代码?
我正在开发广告/弹出窗口拦截器 BHO,并且我正在尝试从事件“downloadcomplete”访问网站的 html,因此我可以过滤所有广告和恶意 uri。
我的代码如下所示:
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;
如何从该事件访问和修改 html?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误的事件,您可以在 DocumentComplete 上“玩”dom,而不是在 DownloadComplete 上。
另外我建议您使用 CComPtr,这样您就不需要在每个接口上调用release()。
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.