none
BHO Event Call

    Dotaz

  • I have developed a BHO using this tutorial. Although my BHO doesn't display message. It loads some image button which further displays menu on click. This is the function i've used which loads image button on OnDocumentComplete. Now there is a small problem. While presing Refresh button OnDocumentComplete event is not called. As a result that image button doesn't get loaded. Can someone help me with a solution.

    Thanks in Advance

    // Parse the given string for any emails using the Regex Class
            // and return a string containing only email addresses
            private static string ParseEmails(string strCompleteText)
            {
                string CurrentMail = string.Empty;
                string PrevMail = string.Empty;
                string[] SplitMail = strCompleteText.Split('<');
                const string emailPattern = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                MatchCollection emails = Regex.Matches(strCompleteText, emailPattern, RegexOptions.IgnoreCase); 
    
                foreach (Match currentEmail in emails)
                {
                    CurrentMail = currentEmail.Value;
                    if (CurrentMail != PrevMail)
                    {
                        for (int j = 0; j < SplitMail.Length - 1; j++)
                        {
                            bool stTextArea = false;
                            bool stInput = false;
                            stInput = SplitMail[j].Contains("INPUT");
                            stTextArea = SplitMail[j].Contains("TEXTAREA");
                            {
                                if (stInput == true || stTextArea == true)
                                {
                                    bool stText = SplitMail[j].Contains("TYPE='Text'");
                                    if (stText == true)
                                    {
    
                                        //if (SplitMail[j].ToString() == CurrentMail)
                                        //{
                                        //    int loc = strCompleteText.IndexOf(currentEmail.Value);
                                        //    strCompleteText = strCompleteText.Remove(loc, currentEmail.Length).Insert(loc, "abc@gmail.com");
                                        //}
                                    }
                                }
    
                                else
                                {
                                    bool st3 = SplitMail[j].Contains(">" + currentEmail.Value);
                                    bool st4 = SplitMail[j].Contains("&nbsp;" + currentEmail.Value);
                                    if (st3 == true)
                                    {
                                        string t = SplitMail[j].Trim().ToString();
                                        int p = t.IndexOf('>');
                                        int l = t.Length;
                                        string name = t.Substring(p, l - p);
                                        if (name == ">" + CurrentMail)
                                        {
    
                                            //int loc = strCompleteText.IndexOf(name);
                                            //if (loc != -1)
                                            //{
                                            //    strCompleteText = strCompleteText.Remove(loc, name.Length).Insert(loc, "><table ><tr><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + currentEmail.Value + "</td><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + "<div style=\"position:relative; z-index:999;background-image:url('http://www.u-2-me.com/images/u-2-me-mini.png');float:right;height:20px;width:20px;display:block;border:solid 0px red; top:0;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\"><div style='background:#088ece; width:172px; height:55px;display:none; border: solid 1px #01608e'><img src='http://www.u-2-me.com/images/addpi.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href='http://www.u-2-me.com/network/contacts/invitecontacts?mailid='" + currentEmail.Value + "  target='_blank'>Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpi.png'  style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 13px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:" + currentEmail.Value + ">Send U-2-Me Link</a></div></div> </td></tr></table>");
                                            //}
                                            int loc = strCompleteText.IndexOf(name);
                                            if (loc != -1)
                                            {
                                                while (loc != -1)
                                                {
                                                    //strCompleteText = strCompleteText.Remove(loc, name.Length).Insert(loc, "><table ><tr><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + currentEmail.Value + "</td><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + "<div style=\"position:relative; z-index:999;background-image:url('http://www.u-2-me.com/images/u-2-me-mini.png');float:right;height:20px;width:20px;display:block;border:solid 0px red; top:0;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\"><div style='background:#088ece; width:172px; height:55px;display:none; border: solid 1px #01608e'><img src='http://www.u-2-me.com/images/addpi.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href='http://www.u-2-me.com/network/contacts/invitecontacts?mailid='" + currentEmail.Value + "  target='_blank'>Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpi.png'  style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 13px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:" + currentEmail.Value + ">Send U-2-Me Link</a></div></div> </td></tr></table>");
                                                    strCompleteText = strCompleteText.Remove(loc, name.Length).Insert(loc,
                                                                                            ">" + "<table style='border:none!important;z-index:999 !important;position: absolute!important;padding-top:0px !important; margin-top:-6px!important;'><tr style='background-color:#D6D6D6;'><td><div style='margin:0;padding:0px 4px 0px 4px;background:#d4d4d4 !important;color:#000000; font-size:12px !important;display: inline-block; z-index:9'><div style='background:#d4d4d4 !important; float:left;z-index:9; position:relative'>"
                                                            + CurrentMail + "</div><div style=\"position:relative; z-index:999 !important;background-color:#D6D6D6;width:20px;height:16px;background-image:url('http://www.u-2-me.com/images/u-2-me-mini-email-plugin.png');border:none !important;display: block;float: left;padding: 2px 0 0 2px !important;height:15px;width:20px;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\">"
                                                            + "<div style='z-index:999 !important;float:left; position:absolute;background: #d4d4d4; width:172px; height:55px;display:none; border: solid 1px #2e2e2e'><img src='http://www.u-2-me.com/images/addpinew.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#2e2e2e; font-family:Verdana, Geneva, sans-serif; display:block' href='http://dev.u-2-me.com/network/contacts/invitecontacts?mailid='"
                                                            + CurrentMail + "target='_blank'>Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpinew.png'  style='float:left; margin:0px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:0px 0px 0px 0px;margin:0px; color:#2e2e2e; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:"
                                                            + CurrentMail + ">Send U-2-Me Link</a></div></div></div></td></tr></table>");
                                                    //loc = strCompleteText.IndexOf(name, loc + name.Length + 1);
                                                    loc = -1;
                                                }
                                            }
                                        }
                                        st3 = false;
                                    }
    
                                    if (st4 == true)
                                    {
                                        string t = SplitMail[j].Trim().ToString();
                                        int p = t.IndexOf("&nbsp;");
                                        int l = t.Length;
                                        string name = t.Substring(p, l - p);
                                        if (name == "&nbsp;" + CurrentMail)
                                        {
                                            //int loc = strCompleteText.IndexOf(name);
                                            //if (loc != -1)
                                            //{
                                            //   // strCompleteText = strCompleteText.Remove(loc, name.Length).Insert(loc, "<table ><tr><td style='border:none!important; background-color:Transparent !important; padding:0px !important' > &nbsp;" + currentEmail.Value + "</td><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + "<div style=\"position:relative;z-index:999;background-image:url('http://www.u-2-me.com/images/u-2-me-mini.png');float:right;height:20px;width:20px;display:block;border:solid 0px red; top:0;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\"><div style='background:#088ece; width:172px; height:55px;display:none; border: solid 1px #01608e'><img src='http://www.u-2-me.com/images/addpi.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href='http://www.u-2-me.com/login  '>Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpi.png'  style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 13px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:" + currentEmail.Value + ">Send U-2-Me Link</a></div></div> </td></tr></table>");
                                            //    strCompleteText = strCompleteText.Remove(loc, name.Length).Insert
                                            //        (loc, "<table ><tr><td style='border:none!important; background-color:Transparent !important; padding:0px !important' > &nbsp;" + 
                                            //        currentEmail.Value +
                                            //        "</td><td style='border:none!important; background-color:Transparent !important; padding:0px !important'>" + "<div style=\"position:relative;z-index:999;background-image:url('http://www.u-2-me.com/images/u-2-me-mini.png');float:right;height:20px;width:20px;display:block;border:solid 0px red; top:0;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\"><div style='background:#088ece; width:172px; height:55px;display:none; border: solid 1px #01608e'><img src='http://www.u-2-me.com/images/addpi.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href='http://www.u-2-me.com/network/contacts/invitecontacts?mailid='" + currentEmail.Value + " target='_blank' >Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpi.png'  style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 13px;margin:0px; color:#ffffff; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:" + 
                                            //        currentEmail.Value + ">Send U-2-Me Link</a></div></div> </td></tr></table>");
    
                                            //}
                                            int loc = strCompleteText.IndexOf(name);
                                            if (loc != -1)
                                            {
                                                while (loc != -1)
                                                {
                                                    strCompleteText = strCompleteText.Remove(loc, name.Length).Insert(loc,
                                                                                           "<table style='border:none!important;z-index:999 !important;position: absolute!important;padding-top:0px !important; margin-top:-6px!important;'><tr style='background-color:#D6D6D6;'><td><div style='margin:0;padding:0px 4px 0px 4px;background:#d4d4d4 !important;color:#000000; font-size:12px !important;display: inline-block;'>"
                                                            + CurrentMail + "<div style='position:absolute; z-index:999 !important;background-color:#D6D6D6;width:20px;height:16px;'><div style=\"position:absolute; z-index:999 !important;background-color:#D6D6D6;background-image:url('http://www.u-2-me.com/images/u-2-me-mini-email-plugin.png');border:none !important;display: block;float: left;padding: 2px 0 0 2px !important;height:15px;width:20px;\" onmouseover=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'block';\"  onmouseout=\"arrtags=this.getElementsByTagName('div');arrtags[0].style.display = 'none';\">"
                                                            + "<div style='z-index:1 !important;background: #d4d4d4; width:172px; height:55px;display:none; border: solid 1px #2e2e2e'><img src='http://www.u-2-me.com/images/addpinew.png' style='float:left; margin:6px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:7px 15px 5px 29px;margin:0px; color:#2e2e2e; font-family:Verdana, Geneva, sans-serif; display:block' href='http://dev.u-2-me.com/network/contacts/invitecontacts?mailid='"
                                                            + CurrentMail + "target='_blank'>Add a U-2-Me Contact</a><img src='http://www.u-2-me.com/images/sendpinew.png'  style='float:left; margin:0px 0 0 4px'><a style='text-decoration:none; font-size:10px; font-weight:bold; padding:0px 0px 0px 0px;margin:0px; color:#2e2e2e; font-family:Verdana, Geneva, sans-serif; display:block' href=mailto:"
                                                            + CurrentMail + ">Send U-2-Me Link</a></div></div></div> </div></td></tr></table>");
                                                    //loc = strCompleteText.IndexOf(name, loc + name.Length + 1);
                                                    loc = -1;
                                                }
                                            }
                                        }
                                        st4 = false;
                                    }
                                }
                            }
                        }
                         PrevMail = CurrentMail;
                    }
                }
                return strCompleteText;
            }
    public void OnDocumentComplete(object pDisp, ref object URL)
            {
                string returnString;
                document = (HTMLDocument)webBrowser.Document;
    
                if (strURL.Equals(document.url.ToLower()))
                {
                    return;
                }
    
                if (webBrowser.Equals(pDisp))
                {
                    strURL = document.url.ToLower().Trim();
                    if (strURL.ToLower().Contains("CurrentMail"))
                    {
                        return;
                    }
                    returnString = ParseEmails(document.body.innerHTML);
                    document.body.innerHTML = returnString;
                }
                ParseEmails(document.body.innerHTML);
            }

    public int SetSite(object site)
            {
                if (site != null)
                {
                    webBrowser = (WebBrowser)site;
                    webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                    webBrowser.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
                }
                else
                {
                    webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
                    webBrowser.BeforeNavigate2 -= new DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);
                    webBrowser = null;
                }
                return 0;
            }



    Adeel

    22. června 2012 6:47

Všechny reakce

  • Refresh action never triggers OnDocumentComplete event in IE. You have to use some heuristic to catch Refresh action. In my BHO I'm checking OnDownloadComplete event and verifying document state is Interactive. This approach works fine in IE7-IE10.


    Sergey

    22. června 2012 7:14
  • Refresh action never triggers OnDocumentComplete event in IE. You have to use some heuristic to catch Refresh action. In my BHO I'm checking OnDownloadComplete event and verifying document state is Interactive. This approach works fine in IE7-IE10.


    Sergey


    Thanks can you demonstrate it with your sample code?

    Adeel

    22. června 2012 7:17
  • //Declaration
    
    //IE events subscription
    BEGIN_SINK_MAP(CMyBHO)
    	SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
    	SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOWNLOADCOMPLETE, OnDownloadComplete)
    END_SINK_MAP()
    
    //Browser interface
    CComPtr<IWebBrowser2> _web_browser;
    bool _advised; 
    
    
    //Implementation
    
    STDMETHODIMP
    CMyBHO::SetSite(IUnknown* pUnkSite)
    {
    	SCOPE_TRACE;
    
    	if( pUnkSite )
    	{
    		// Cache the pointer to IWebBrowser2.
    		HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&_web_browser);
    		if( SUCCEEDED( hr ) )
    		{
    			// Register to sink events from DWebBrowserEvents2.
    			hr = DispEventAdvise( _web_browser );
    			if( SUCCEEDED( hr ) )
    				_advised = true;
    		}
    	}
    	else
    	{
    		// Unregister event sink.
    		if( _advised )
    		{
    			hr = DispEventUnadvise( _web_browser );
    			_advised = false;
    		}
    
    		// Release cached pointers and other resources here.
    		_web_browser.Release();
    	}
    
    	// Call base class implementation.
    	return IObjectWithSiteImpl<COktaBHO>::SetSite(pUnkSite);
    }
    
    void STDMETHODCALLTYPE
    CMyBHO::OnDocumentComplete( IDispatch* pdisp,	VARIANT* pvtURL )
    {
    
      // Is this event associated with the top-level browser?
      CComQIPtr<IWebBrowser2> browser( pdisp );
      if( !browser || !_web_browser ||
        !_web_browser.IsEqualObject( browser ) )
        return;
    
      //Do you work here
    }
    
    
    void STDMETHODCALLTYPE
    CMyBHO::OnDownloadComplete()
    {
      CComPtr<IDispatch> disp_doc;
      HRESULT hr = _web_browser->get_Document( &disp_doc );
      if( FAILED( hr ) )
        return;
    
      CComBSTR ready_state;
      hr = document->get_readyState( &ready_state );
      if( FAILED( hr ) )
        return;
    
      if( ready_state != L"interactive" )
        return;
    
      //Do you work here
    }
    


    Best regards, Sergey

    22. června 2012 7:43
  • //Declaration
    
    //IE events subscription
    BEGIN_SINK_MAP(CMyBHO)
    	SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
    	SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOWNLOADCOMPLETE, OnDownloadComplete)
    END_SINK_MAP()
    
    //Browser interface
    CComPtr<IWebBrowser2> _web_browser;
    bool _advised; 
    
    
    //Implementation
    
    STDMETHODIMP
    CMyBHO::SetSite(IUnknown* pUnkSite)
    {
    	SCOPE_TRACE;
    
    	if( pUnkSite )
    	{
    		// Cache the pointer to IWebBrowser2.
    		HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&_web_browser);
    		if( SUCCEEDED( hr ) )
    		{
    			// Register to sink events from DWebBrowserEvents2.
    			hr = DispEventAdvise( _web_browser );
    			if( SUCCEEDED( hr ) )
    				_advised = true;
    		}
    	}
    	else
    	{
    		// Unregister event sink.
    		if( _advised )
    		{
    			hr = DispEventUnadvise( _web_browser );
    			_advised = false;
    		}
    
    		// Release cached pointers and other resources here.
    		_web_browser.Release();
    	}
    
    	// Call base class implementation.
    	return IObjectWithSiteImpl<COktaBHO>::SetSite(pUnkSite);
    }
    
    void STDMETHODCALLTYPE
    CMyBHO::OnDocumentComplete( IDispatch* pdisp,	VARIANT* pvtURL )
    {
    
      // Is this event associated with the top-level browser?
      CComQIPtr<IWebBrowser2> browser( pdisp );
      if( !browser || !_web_browser ||
        !_web_browser.IsEqualObject( browser ) )
        return;
    
      //Do you work here
    }
    
    
    void STDMETHODCALLTYPE
    CMyBHO::OnDownloadComplete()
    {
      CComPtr<IDispatch> disp_doc;
      HRESULT hr = _web_browser->get_Document( &disp_doc );
      if( FAILED( hr ) )
        return;
    
      CComBSTR ready_state;
      hr = document->get_readyState( &ready_state );
      if( FAILED( hr ) )
        return;
    
      if( ready_state != L"interactive" )
        return;
    
      //Do you work here
    }


    Best regards, Sergey

    Is there a way I can also find out if Refresh button or F5 is pressed? Because OnDownloadComplete is called multiple times when the page is loaded or when someone press Refresh so that thing gets loaded multiple times one over other

    Adeel


    22. června 2012 9:56
  • Is there a way I can also find out if Refresh button or F5 is pressed? Because OnDownloadComplete is called multiple times when the page is loaded or when someone press Refresh so that thing gets loaded multiple times one over other


    Adeel

    I don't know the right way to catch Refresh button or F5 key pressed. Even MSFT guys don't know :) . You can google about that but seems like you have to set a hook in IE main frame and listen for some event (WM_LBUTTONUP and WM_KEYUP). For sure, this is hacking technology and might not work on any other IE versions or even on different build. I don't recommend you to go this way.

    Better solution is to add some hidden div into a DOM and use it to mark handled pages.


    Best regards, Sergey

    22. června 2012 12:51
  • Is there a way I can also find out if Refresh button or F5 is pressed? Because OnDownloadComplete is called multiple times when the page is loaded or when someone press Refresh so that thing gets loaded multiple times one over other


    Adeel

    I don't know the right way to catch Refresh button or F5 key pressed. Even MSFT guys don't know :) . You can google about that but seems like you have to set a hook in IE main frame and listen for some event (WM_LBUTTONUP and WM_KEYUP). For sure, this is hacking technology and might not work on any other IE versions or even on different build. I don't recommend you to go this way.

    Better solution is to add some hidden div into a DOM and use it to mark handled pages.


    Best regards, Sergey


    Can you demonstrate it with an example how I can add hidden Div into DAM and use it to mark handled pages?

    Adeel

    25. června 2012 9:16