IE扩展在网页中注入javascript [英] IE extension to inject javascript in the webpage

查看:1197
本文介绍了IE扩展在网页中注入javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C ++实现了IE扩展。它的功能是在点击扩展图标时在网页的head标签中注入javascript。我已经使用 execScript 方法进行脚本注入。

它运行正常但是当我刷新网页时,或者当我点击网页上的任何链接时,或者当我输入另一个URL时,注入的脚本消失了。

我不希望脚本消失,我希望它在Web浏览器中持久化。

怎么能我实现了吗?我是IE扩展开发的新手,任何帮助都将受到高度赞赏。

谢谢。

  STDMETHODIMP CBlogUrlSnaggerAddIn :: Exec(
const GUID * pguidCmdGroup,DWORD nCmdID,
DWORD nCmdExecOpt,VARIANTARG * pvaIn,VARIANTARG * pvaOut){
HRESULT hr = S_OK;
CComPtr< IDispatch> spDispDoc;
hr = m_spWebBrowser-> get_Document(& spDispDoc);
if(SUCCEEDED(hr)){
CComPtr< IDispatch> spDispDoc;
hr = m_spWebBrowser-> get_Document(& spDispDoc);
if(SUCCEEDED(hr)&& spDispDoc){
CComPtr< IHTMLDocument2> spHTMLDoc;
hr = spDispDoc.QueryInterface< IHTMLDocument2>(& spHTMLDoc);
if(SUCCEEDED(hr)&& spHTMLDoc){
VARIANT vrt = {0};
CComQIPtr< IHTMLWindow2>赢得;
hr = spHTMLDoc-> get_parentWindow(& win);
CComBSTR bstrScript = Lfunction fn(){alert('helloooo');} var head = document.getElementsByTagName('head')[0],script = document.createElement('script'); script [ script.innerText?'innerText':'textContent'] ='('+ fn +')()'; head.appendChild(script); head.parentNode.replaceChild(script,'script');;
CComBSTR bstrLanguage = Ljavascript;
HRESULT hrexec = win-> execScript(bstrScript,bstrLanguage,& vrt);
}
}}


解决方案

不要在Exec事件中编写execScript代码,而是尝试在OnDocumentComplete方法下添加一段代码。使用用于设置事件处理的接收器映射。下面提供了一个样本。

  BEGIN_SINK_MAP(CMyClass)
SINK_ENTRY_EX(1,DIID_DWebBrowserEvents2,DISPID_DOCUMENTCOMPLETE,OnDocumentComplete)

END_SINK_MAP()

在类文件中实现DocumentComplete。

  void STDMETHODCALLTYPE CMyClass :: OnDocumentComplete(IDispatch * pDisp,VARIANT * pvarURL)
{
//在此处注入脚本
}

更新:



<我没有试过这个,但我想DownloadBegin事件会满足你的需要。它类似于Document complete事件映射,只有不同的东西是DISPID_DOWNLOADBEGIN。将相应的处理程序方法映射到DISPID并尝试一下。

  BEGIN_SINK_MAP(CMyClass)
SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2,DISPID_DOWNLOADBEGIN,OnDocumentLoad)
END_SINK_MAP()

类似于DocumentComplete Handler方法

  void STDMETHODCALLTYPE CMyClass :: OnDocumentLoad(IDispatch * pDisp,VARIANT * pvarURL)
{
//在此处注入脚本
}




http://msdn.microsoft.com/en-us/library/cc136547(v = vs .85).aspx



I have implemented an IE extension using C++. Its function is to inject javascript in the webpage's head tag, whenever the extension icon is clicked. I have used execScript method for script injection.
It works fine but when I refresh the webpage, or when I click on any link on the webpage, or when I enter another URL the injected script vanishes away.
I don't want the script to vanish away, I want it to be persistent inside the web browser.
How can I achieve that? I am new to IE extension development, any help would be highly appreciated.
Thanks.

STDMETHODIMP CBlogUrlSnaggerAddIn::Exec(
const GUID *pguidCmdGroup, DWORD nCmdID,
DWORD nCmdExecOpt, VARIANTARG *pvaIn, VARIANTARG *pvaOut){
    HRESULT hr = S_OK;
    CComPtr<IDispatch> spDispDoc;
    hr = m_spWebBrowser->get_Document(&spDispDoc);
    if (SUCCEEDED(hr)){ 
        CComPtr<IDispatch> spDispDoc;
        hr = m_spWebBrowser->get_Document(&spDispDoc);
           if (SUCCEEDED(hr) && spDispDoc){
              CComPtr<IHTMLDocument2> spHTMLDoc;
              hr = spDispDoc.QueryInterface<IHTMLDocument2>( &spHTMLDoc );
                  if (SUCCEEDED(hr) && spHTMLDoc){
                       VARIANT vrt = {0};
                       CComQIPtr<IHTMLWindow2> win;
                       hr = spHTMLDoc->get_parentWindow(&win);
                       CComBSTR bstrScript = L"function fn() {alert('helloooo');}var  head = document.getElementsByTagName('head')[0],script = document.createElement('script');script[script.innerText ? 'innerText' : 'textContent'] = '(' + fn + ')()';head.appendChild(script);head.parentNode.replaceChild(script,'script');";
                       CComBSTR bstrLanguage = L"javascript";
                       HRESULT hrexec = win->execScript(bstrScript,bstrLanguage, &vrt);
    }
}}

解决方案

Instead of writing the execScript code in the Exec event, try adding the piece of code under OnDocumentComplete method. Use the Sink map which is used to set up event handling. A sample is provided below.

BEGIN_SINK_MAP(CMyClass)
    SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2,DISPID_DOCUMENTCOMPLETE , OnDocumentComplete)

END_SINK_MAP()

Implement the DocumentComplete in your class file.

void STDMETHODCALLTYPE CMyClass::OnDocumentComplete(IDispatch *pDisp,VARIANT *pvarURL)
{
   //Inject the scripts here
}

Updated :

I haven't tried this, but I guess DownloadBegin event would serve your purpose. It is similar to the the Document complete event mapped, only thing which would differ would be the DISPID_DOWNLOADBEGIN. Map a corresponding handler method to the DISPID and give it a try.

BEGIN_SINK_MAP(CMyClass)
    SINK_ENTRY_EX(1,DIID_DWebBrowserEvents2,DISPID_DOWNLOADBEGIN, OnDocumentLoad)
END_SINK_MAP()

Similar to DocumentComplete Handler method

void STDMETHODCALLTYPE CMyClass::OnDocumentLoad(IDispatch *pDisp,VARIANT *pvarURL)
{
   //Inject scripts here
}

http://msdn.microsoft.com/en-us/library/cc136547(v=vs.85).aspx

这篇关于IE扩展在网页中注入javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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