本文介绍了如何在C ++ BHO插件中创建元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是BHO插件的新手.
我将在加载首页时添加一些脚本.
所以我实现了OnDocumentcompleted函数.

I am new to BHO plugin.
I'' m going to add some script when homepage is loaded.
So I implemented OnDocumentcompleted function.

void STDMETHODCALLTYPE CHelloWorldBHO::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
{
	HRESULT hr = S_OK;

	// Query for the IWebBrowser2 interface.
	CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;

	// Is this event associated with the top-level browser?
	if (spTempWebBrowser && m_spWebBrowser &&
		m_spWebBrowser.IsEqualObject(spTempWebBrowser))
	{
		// Get the current document object from browser...
		CComPtr<IDispatch> spDispDoc;
		hr = m_spWebBrowser->get_Document(&spDispDoc);
		if (SUCCEEDED(hr))
		{
			// ...and query for an HTML document.
			CComQIPtr<IHTMLDocument2> spHTMLDoc = spDispDoc;
			if (spHTMLDoc != NULL)
			{
				// Finally, remove the images.
				OutputDebugString(pvarURL->bstrVal);
				WCHAR wszURL[1025];
				wcscpy(wszURL, pvarURL->bstrVal);
				if(wcsnicmp(wszURL, L"http://www.google.com/", wcslen(L"http://www.google.com/")) == 0){
					MessageBox(NULL, L"", L"", MB_OK);
					InjectScript(spHTMLDoc);
				}
			}
		}
	}
}


InjectScript的实现如下.


And InjectScript is implemented as follow.

void CHelloWorldBHO::InjectScript(IHTMLDocument2* pDocument)
{
    CComPtr<IHTMLElement> body;
    pDocument->get_body(&body);

    CComPtr<IHTMLScriptElement> scriptObject;
    pDocument->createElement(L"script", (IHTMLElement**)&scriptObject);

    scriptObject->put_type(L"text/javascript");
    scriptObject->put_text(L"\nfunction hidediv(){document.getElementById(''myOwnUniqueId12345'').style.visibility = ''hidden'';}\n\n");

    CComPtr<IHTMLDOMNode> domnodebody;
    body->QueryInterface(IID_IHTMLDOMNode, (void**)&domnodebody);

    CComPtr<IHTMLDOMNode> domnodescript;
    scriptObject->QueryInterface(IID_IHTMLDOMNode, (void**)&domnodescript);


    CComPtr<IHTMLDOMNode> pRefNode = NULL;
    domnodebody->appendChild(domnodescript, &pRefNode);
}



但是访问www.google.com时出错.
任何建议将不胜感激.



But there''s an error when visit www.google.com.
Any advice will be appreciated.

推荐答案


这篇关于如何在C ++ BHO插件中创建元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:46