Search code examples
javascriptc#wpfcefsharp

CefSharp Running JavaScript


I am having difficulty running JS in CefSharp due to the command running straight after each other. Basically, I need a wait command to await the page to load after executing the last script.

Hence it would be:

await Browser.ExecuteScriptAsync(script1);
Browser.WaitForLoad();
Task<JavascriptResponse> test = await Browser.EvaluateScriptAsync(script2);

I have found a few resources but I'm only new to programming so I'm finding it a bit difficult to work out how I can do what I want.

using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp;
using CefSharp.Wpf;

namespace WpfApp2
{
    public partial class MainWindow : Window
    {
        private string MessagesInbox;
        public MainWindow()
        {
            InitializeComponent();
        }


        private void Browser_Loaded(object sender, RoutedEventArgs e)
        {
            Browser.LoadingStateChanged += async (Sender, args) => {if (args.IsLoading == false)
                {
                    await Browser.EvaluateScriptAsync(@"document.getElementById('User').value = 'test'; document.getElementById('Password').value = 'password'; document.getElementById('LoginButton').click();");
                }};
            Task<JavascriptResponse> test = Browser.EvaluateScriptAsPromiseAsync(@"return new Promise(function(resolve, reject) { setTimeout(resolve.bind(null, { var test = ''; document.querySelectorAll('.MasterAction').forEach(el => test += el.children[3].children[0].href + ', ')}), 2000);");
            MessagesInbox = test.Result.ToString();
            MessageBox.Show(MessagesInbox);
        }
    }
}

Solution

  • You should also check the IsLoading for your second method, you can use a bool to track if you are already logged in, and use the second method inside that function aswell like this:

    public bool loggedIn = false;
    private void Browser_Loaded(object sender, EventArgs e)
        {
            Browser.LoadingStateChanged += async (Sender, args) =>
            {
                if (args.IsLoading == false)
                {
                    if (!loggedIn)
                    {
                        await Browser.EvaluateScriptAsync(@"document.getElementById('User').value = 'test'; document.getElementById('Password').value = 'password'; document.getElementById('LoginButton').click();")
                            .ContinueWith(x =>
                            {
                                var response = x.Result;
                                System.Diagnostics.Debug.WriteLine(response.Result);
                                if (response.Result == null)
                                {
                                    System.Diagnostics.Debug.WriteLine("should be changed to true");
                                    loggedIn = true;
                                }
                            });
                    }
                    else
                    {
                        JavascriptResponse test = await Browser.EvaluateScriptAsync(@"var test = ''; document.querySelectorAll('.MasterAction').forEach(el => test += el.children[3].children[0].href + ', ');test.substr(0,test.length-2)");
                        MessagesInbox = test.Result.ToString();
                        System.Diagnostics.Debug.WriteLine(MessagesInbox);
                    }
                }
            };
        }