cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.89k stars 2.92k forks source link

How to know whether the browser had initialized? #1422

Closed ahgan84 closed 9 years ago

ahgan84 commented 9 years ago

Hi guys, How do we know whether the browser had been initialized? I want to put some data on the webpage immediately after it load out.

But there's an error saying that the browser is not initialized and I need to use the IsBrowserInitializedChanged event and checkthe IsBrowserInitialized property to determine when the browser has been intialized.

      public Form1()
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(0, 0);
        BrowserSettings browserSettings = new BrowserSettings();
        browser = new CefSharp.WinForms.ChromiumWebBrowser(AppDomain.CurrentDomain.BaseDirectory + "html\\index.html")
        {
            Dock = DockStyle.Fill,
        };
        this.Controls.Add(browser);
        browser.RegisterJsObject("printer", new PrintTicket());
        browser.BrowserSettings.WebSecurity = CefState.Disabled;
        browser.BrowserSettings.FileAccessFromFileUrls = CefState.Enabled;
        browser.BrowserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
       IPXMLread();   //Error on this line.
    }

    private void IPXMLread()
    {
        string ioIP = "";
        string ioPort = "";
        string conIP = "";
        string conPort = "";

        using (XmlReader reader = XmlReader.Create("Config.xml"))
        {
            while (reader.Read())
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        case "io_ip":
                            if (reader.Read())
                                ioIP = reader.Value;
                            Console.WriteLine(ioIP);
                            break;
                        case "io_port":
                            if (reader.Read())
                                ioPort = reader.Value;
                            Console.WriteLine(ioPort);
                            break;
                        case "con_ip":
                            if (reader.Read())
                                conIP = reader.Value;
                            Console.WriteLine(conIP);
                            break;
                        case "con_port":
                            if (reader.Read())
                                conPort = reader.Value;
                            Console.WriteLine(conPort);
                            break;
                    }
                }
            }
        }

        var script = string.Format("getIP('{0}', '{1}', '{2}', '{3}');", ioIP, ioPort, conIP, conPort);
        browser.ExecuteScriptAsync(script);
    }

Is there any example that I can get regarding checking browser initializing? I search high and low but coudn't get it.

ahgan84 commented 9 years ago

I have successfully solve the error by adding the below line to the "public Form1": browser.IsBrowserInitializedChanged += new EventHandler(IPXMLread);

Seems like like this, it will only run IPXMLread() after the browser is initialized. But my browser still does not receive the "getIP()" function which I call using ExecuteScriptAsync(). Is it that the browser still does not initialized long time enough?

For the getIP() function in the webpage javascript, it is just like below:

<script>
    getIP = function (IP1, Port1, IP2, Port2) {
        alert("got it!!");
        io_server_ip = IP1;
        io_server_port = Port1;
        iocon_server_ip = IP2;
        iocon_server_port = Port2;
        return false;
    };
</script>

It can only received this function after sometime say, after the web page already load out for 5 seconds.

amaitland commented 9 years ago

There are a limited number of functions you can perform once it's initialized, others you must wait for it to load. You need to use the LoadingStateChanged handler to receive notifications of overall page load.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.OffScreen.Example/Program.cs#L105 https://github.com/cefsharp/CefSharp/blob/master/CefSharp.OffScreen.Example/Program.cs#L71

ahgan84 commented 9 years ago

Can I use this one? browser.FrameLoadEnd += new EventHandler(IPXMLread);

amaitland commented 9 years ago

browser.FrameLoadEnd += new EventHandler(IPXMLread);

In most cases you can. Remembering it will be called one for every frame, so if you have multiple frames.

For notification of overall browser load status use OnLoadingStateChange instead.

As per the documentation for OnLoadingStateChange (Which translates to LoadingStateChanged in CefSharp). So as a general rule I reccomend using LoadingStateChange.

http://magpcss.org/ceforum/apidocs3/projects/%28default%29/CefLoadHandler.html#OnLoadStart%28CefRefPtr%3CCefBrowser%3E,CefRefPtr%3CCefFrame%3E%29

ahgan84 commented 9 years ago

Is it like this? browser.LoadingStateChanged += new EventHandler(IPXMLread); The what variable I need to add in IPXMLread function to know that the web page had been loaded?

amaitland commented 9 years ago

See the example I linked to before to get a rough idea of how it works.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.OffScreen.Example/Program.cs#L94

amaitland commented 9 years ago

@ahgan84 Got it working? Anything else before closing?