I have successfully implemented CefSharp in a .NET 4.0 application. Is it possible to get jQuery calls working on a DOM?

I am trying to find out if this is possible. I went through the GitHub example https://github.com/chillitom/CefSharp , which gave me the source code for the classes (although I could not build CefSharp myself from this GITHUB.

Then I tried to download the binaries from this link https://github.com/downloads/ataranto/CefSharp/CefSharp-1.19.0.7z , and then I built my C # win32 application, referencing these examples, it went quite smoothly , and after 8 hours or so I got a working built-in browser, yipeee. However, I am now at the point where I want to manipulate the DOM - I read that you can only do this with webView.EvaluateScript ("some script"); and webView.ExecuteScript ("some script"); since direct DOM access is not available via cefsharp

So I'm trying to figure it out. Can I call jQuery methods? If the loaded page is already loaded with jQuery, can I do the following in C #?

webView.ExecuteScript("$(\"input#some_id\").val(\" user@example.com \")")); 

This is currently throwing an exception. I am trying to find out; should I even try to use jQuery from the cefsharp dll library, or should i stick to the standard old school javascript, which will take me 5 times longer ...?

I hope the stacker has an answer. I tried wiki and forums for cefsharp, but they do not offer many ways to lead; and the only examples i found are old school javascript.

+7
source share
2 answers

Yes, you can use jQuery, however you can only use it after the DOM has fully loaded. To do this, you need to use the PropertyChanged event for the WebView to check when the IsLoading property has been changed to false and the IsBrowserInitialized property is set to true.

Below is a snippet of how I do this in one of my projects. As you can see, as soon as the IsLoading property changes, I then call some methods that will customize the content in the WebView, and this is done by calling jQuery through ExecuteScript, as you do.

 /// <summary> /// Initialise the WebView control /// </summary> private void InitialiseWebView() { // Disable caching. BrowserSettings settings = new BrowserSettings(); settings.ApplicationCacheDisabled = true; settings.PageCacheDisabled = true; // Initialise the WebView. this.webView = new WebView(string.Empty, settings); this.WebView.Name = string.Format("{0}WebBrowser", this.Name); this.WebView.Dock = DockStyle.Fill; // Setup and regsiter the marshal for the WebView. this.chromiumMarshal = new ChromiumMarshal(new Action(() => { this.FlushQueuedMessages(); this.initialising = false; })); this.WebView.RegisterJsObject("marshal", this.chromiumMarshal); // Setup the event handlers for the WebView. this.WebView.PropertyChanged += this.WebView_PropertyChanged; this.WebView.PreviewKeyDown += new PreviewKeyDownEventHandler(this.WebView_PreviewKeyDown); this.Controls.Add(this.WebView); } /// <summary> /// Handles the PropertyChanged event of CefSharp.WinForms.WebView. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The event arguments.</param> private void WebView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { // Once the browser is initialised, load the HTML for the tab. if (!this.webViewIsReady) { if (e.PropertyName.Equals("IsBrowserInitialized", StringComparison.OrdinalIgnoreCase)) { this.webViewIsReady = this.WebView.IsBrowserInitialized; if (this.webViewIsReady) { string resourceName = "Yaircc.UI.default.htm"; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { using (StreamReader reader = new StreamReader(stream)) { this.WebView.LoadHtml(reader.ReadToEnd()); } } } } } // Once the HTML has finished loading, begin loading the initial content. if (e.PropertyName.Equals("IsLoading", StringComparison.OrdinalIgnoreCase)) { if (!this.WebView.IsLoading) { this.SetSplashText(); if (this.type == IRCTabType.Console) { this.SetupConsoleContent(); } GlobalSettings settings = GlobalSettings.Instance; this.LoadTheme(settings.ThemeFileName); if (this.webViewInitialised != null) { this.webViewInitialised.Invoke(); } } } } 
+6
source

In the comments it was more clear that you want to load jQuery on a page that does not already have it. The int0x90 suggestion to run ExecuteScript in a local copy of jQuery source might work for you. I want to warn you, although many pages will not be compatible with all additional JS that their authors never put. Two great examples are Google and Facebook. Both of them define a $ operator, which is not jQuery $ , so stomping on it will almost certainly break them.

The CEF core library provides many methods for directly managing DOM elements from C ++ that CefSharp did not find, because there is not much demand for them so far. Looks like this is what you would like to use here. It may not be too much work to expose them if you looked at the source of CefSharp, but I have not tried it myself. If you want to try, you can also ask questions https://groups.google.com/forum/#!forum/cefsharp

+1
source

All Articles