Rhino Crash With WebView ExecuteScript

I’m testing out using webview in ETO in Rhino8 and I keep crashing Rhino when using the ExecuteScript function inside of the DocumentLoading Event. I can successfully inject the JavaScript once and then Rhino crashes. Any help would be appreciated. Thanks.

using Eto.Drawing;
using Eto.Forms;
using System;

using Rhino;


namespace TestWebUI
{
    public class EtoForm : Form
    {
        public WebView Wv { get; private set; }
        public bool IndexLoaded = false;

        string index;
        public EtoForm()
        {
            this.ClientSize = new Size(600, 600);
            Wv = new WebView();

            Wv.DocumentLoading += E_DocumentLoading;
            Wv.DocumentLoaded += E_DocumentLoaded;

            Content = Wv;

        }

        private void E_DocumentLoaded(object sender, WebViewLoadedEventArgs e)
        {
            if (e.Uri.AbsolutePath == index) IndexLoaded = true;
        }

        public void SetWVUrl(string url)
        {
            index = url.Replace("\\", "/");
            Wv.Url = new Uri(index);
        }

        private void E_DocumentLoading(object sender, WebViewLoadingEventArgs e)
        {

            if (e.Uri.Scheme == "myapp")
            {
                e.Cancel = true; // Cancel Any Other Navigation

                var path = e.Uri.PathAndQuery;
                RhinoApp.WriteLine(path);

                // change the button text to the path with injected javascript
                string js = $"document.getElementById('greetingsbutton').innerText = '{path}';";
                Wv.ExecuteScript(js);
            }
        }
    }
}
1 Like

Hi @David_Moreau ,

using Eto.Drawing;
using Eto.Forms;
using Rhino;
using System;
namespace TestWebUI
{
    public class EtoForm2 : Form
    {
        public WebView Wv { get; private set; }
        public bool IndexLoaded = false;

        string index;
        private string path;

        public EtoForm2()
        {
            this.ClientSize = new Size(600, 600);
            Wv = new WebView();

            Wv.DocumentLoading += E_DocumentLoading;
            Wv.DocumentLoaded += E_DocumentLoaded;
            SetWVUrl("https://www.google.com");
            Content = Wv;
        }

        private void E_DocumentLoaded(object sender, WebViewLoadedEventArgs e)
        {
            if (e.Uri.AbsolutePath == index && !string.IsNullOrEmpty(path))
            {
                IndexLoaded = true;
                // Execute JavaScript here, after the document is fully loaded
                string js = $"document.getElementById('greetingsbutton').innerText = '{path}';";
                Wv.ExecuteScript(js);
            }
        }

        public void SetWVUrl(string url)
        {
            index = url.Replace("\\", "/");
            Wv.Url = new Uri(index);
        }

        private void E_DocumentLoading(object sender, WebViewLoadingEventArgs e)
        {
            if (e.Uri.Scheme == "myapp")
            {
                e.Cancel = true; // Cancel Any Other Navigation
                path = e.Uri.PathAndQuery;
                RhinoApp.WriteLine(path);
            }
        }
    }
}

You were attempting to execute JavaScript in the DocumentLoading event. This event is triggered before the document has finished loading.
Hope this helps,

Farouk

4 Likes

Brilliant. Thank you!