我是开发BHO(浏览器助手对象)的新手。 编写简单的BHO,从Google搜索结果中收集URL,并在每个URL后插入GIF(BITMAP)。 我的BHO代码已经部分完成并且也正在设置URL,但URLS不是我在页面上看到的:
示例:
如果我搜索“驱动程序对象” 然后我希望网址像:
http://msdn.microsoft.com/en-us/library/windows/hardware/ff548034%28v=vs.85%29.aspx
但在DocumentComplete事件之后,我得到的网址如下:
http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCcQFjAA&url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fwindows%2Fhardware%2Fff548034%28v%3Dvs.85%29.aspx&ei=s6TmUvneH8eJrQeJ0YEQ&usg=AFQjCNGPT5rAp6Yw2givF35tAtkFEi7gjw
经过一些研究和分析后,我想出了一个解决方案,可以使用javascript来收集实际的网址而不是Google重定向的网址。 但我不知道如何从VC ++ BHO执行JavaScript源代码(xyz.js)。
提前致谢。
hRes = m_WebBrowser2->get_Document(&pIDisp);
if (FAILED(hRes))
return TRUE;
pHTMLDoc = (IHTMLDocument2 *)pIDisp;
IHTMLElementCollection *pMainCollection = NULL;
IHTMLElementCollection *pCollection = NULL;
hRes = pHTMLDoc->get_all(&pMainCollection);
if (FAILED(hRes))
{
pHTMLDoc->Release();
return TRUE;
}
IDispatch *pEmbdDisp = NULL;
IHTMLElementCollection *pEmbedCollection = NULL;
VARIANT varTag;
varTag.vt = VT_BSTR;
varTag.bstrVal = SysAllocString(L"a");
hRes = pMainCollection->tags(varTag, &pEmbdDisp);
if (FAILED (hRes))
{
goto CleanUp;
}
hRes = pEmbdDisp->QueryInterface(IID_IHTMLElementCollection, (void **)&pEmbedCollection);
if (FAILED (hRes))
{
if (pEmbdDisp)
{
pEmbdDisp->Release();
pEmbdDisp = NULL;
}
goto CleanUp;
}
pCollection = pEmbedCollection;
//
// Gel links count.
//
long lCount = 0;
hRes = pCollection->get_length(&lCount);
if (FAILED(hRes))
{
OutputDebugStringW(L"pCollection->get_length...FAILED.");
pCollection->Release();
pHTMLDoc->Release();
return TRUE;
}
BSTR bstr;
VARIANT varIndex;
varIndex.vt = VT_UINT;
VARIANT var2;
var2.vt = VT_UINT;
IHTMLElement *pElement = NULL;
for (int iIter = 0; iIter < lCount; iIter++)
{
varIndex.lVal = iIter;
var2.lVal = iIter;
pElement = NULL;
hRes = pCollection->item(var2, varIndex, &pDisp);
if (FAILED(hRes))
continue;
hRes = pDisp->QueryInterface(IID_IHTMLElement, (void **)&pElement);
if (FAILED (hRes))
{
OutputDebugStringW(L"Disp->QueryInterface for IID_IHTMLElement...FAILED.");
if (pDisp)
pDisp->Release();
continue;
}
IHTMLAnchorElement *pAnchElement = NULL;
hRes = pElement->QueryInterface(IID_IHTMLAnchorElement, (void **)&pAnchElement);
if (FAILED (hRes))
{
if (pDisp)
{
pDisp->Release();
pDisp = NULL;
}
continue;
}
hRes = pAnchElement->get_href(&bstr);
//
// bstr is the URL
// I got here redirected URL instead i am expecting actual web site URLS.
//
}
以上代码只需在DocumnetComplete事件被触发时调用。