Error in 'AddMessageEventListener' on GeckoFX

元气小坏坏 提交于 2020-02-03 01:53:22

问题


I want that my browser (GeckoFX) executes a .NET function when clicking on a button in my webpage. I have the following code:

public Form1()
{
   InitializeComponent();

   AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));

   browser.LoadHtml
   (
       @"<!DOCTYPE html>
             <html><head>
             <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
             <script type=""text/javascript"">
                function fireEvent(name, data)
                {
                   event = document.createEvent('MessageEvent');
                   event.initMessageEvent(name, false, false, data, null, null, null, null);
                   document.dispatchEvent(event);
                }
             </script>
             </head>
             <body>fdsfsda
                <input type=""button"" onclick=""fireEvent('myFunction', 'some data');"" value=""SHOW DATA"" />
             </body></html>"
   );
}

public void AddMessageEventListener(string eventName, Action<string> action)
{
   browser.AddMessageEventListener(eventName, action);
}

private void showMessage(string s)
{
   MessageBox.Show(s);
}

But when running that code, the line

browser.AddMessageEventListener(eventName, action);

gives me an 'Object reference not set to an instance of an object' error. I don't understand that because I have checked that all object in that line are initialized. Can somebody help and tell why I'm getting this error?

Thank you very much!


回答1:


Try to put AddMessageEventListener after LoadHtml like this:

browser.LoadHtml(@"<!DOCTYPE html>.....</html>");

AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));

And change it this lines inside your LoadHtml:

event = document.createEvent('MessageEvent');
event.initMessageEvent(name, false, false, data, null, null, null, null);

By this:

var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': false, 'data': data});

Your new code look like:

public Form1()
{
   InitializeComponent();

   browser.LoadHtml
   (
       @"<!DOCTYPE html>
             <html><head>
             <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"">
             <script type=""text/javascript"">
                function fireEvent(name, data)
                {
                   var event = new MessageEvent(name,{'view': window, 'bubbles': false, 'cancelable': false, 'data': data});
                   document.dispatchEvent(event);
                }
             </script>
             </head>
             <body>fdsfsda
                <input type=""button"" onclick=""fireEvent('myFunction', 'some data');"" value=""SHOW DATA"" />
             </body></html>"
   );

   AddMessageEventListener("myFunction", ((string s) => this.showMessage(s)));
}

private void showMessage(string s)
{
   MessageBox.Show(s);
}



回答2:


You can try a different approach. Subscribe to browser.DomClick event - it will be triggered whenever something is clicked on. From there you can filter out uninteresting clicks and only invoke further code when your desired element is clicked.

Browser.DomClick += browser_DomClick;

And then

private void browser_DomClick(object sender, DomMouseEventArgs e)
        {
            var browser = sender as GeckoWebBrowser;

            if (sender != null && e != null && e.Target != null)
            {
                if (e.Button == GeckoMouseButton.Left)
                {
                    HandleLeftClick(e);
                }
                if (e.Button == GeckoMouseButton.Middle)
                {
                    HandleMiddleClick(e);
                }
                if (e.Button == GeckoMouseButton.Right)
                {
                    HandleRightClick(e);
                }
            }
        }


来源:https://stackoverflow.com/questions/40859864/error-in-addmessageeventlistener-on-geckofx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!