如何在C ++ BHO插件中创建元素 [英] How to create an element in C++ BHO plugin

查看:97
本文介绍了如何在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.

推荐答案

OnDocumentCompleteInjectScript的外观是由2个不同的人编写的. 因为OnDocumentComplete具有错误检查功能,但InjectScript没有错误检查.
这就是为什么您不明白错误是什么的原因. 因此,添加一些错误检查并找出是否发生错误.
然后,您可以使用浏览器提供的开发工具来调查有关该错误的更多信息.
或者,您可以在使用get_outerHTML注入脚本后获得html并将其转储到文件中以进行进一步调查.
Looks like OnDocumentComplete and InjectScript are written by 2 different people.
Because OnDocumentComplete has error checking, but InjectScript does not.
Which is exactly why you don''t understand what the error is.
So add some error checking and find out if an error occurs.
Then you can use the development tools available with the browser you''re using to investigate more on the error.
Or you can get the html after injecting the script using get_outerHTML and dump it to a file for further investigation.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆