Thursday, January 11, 2018

Chrome in C#

Recently I got a new NAS and wanted to monitor the heath on a regular basis. So I wrote an app to log into the web admin portal and display the system status dashboard. I used the CefSharp toolkit found here: https://github.com/cefsharp/CefSharp

It was super easy to use. I just created a form then added the following code:

       
public ChromiumWebBrowser chromeBrowser;

CefSettings cefSettings = new CefSettings();
Cef.Initialize(cefSettings);

chromeBrowser = new ChromiumWebBrowser("INSERT_WEB_URL_HERE");

this.Controls.Add(chromeBrowser);

// fill the window minus the menu
chromeBrowser.Dock = DockStyle.None;
chromeBrowser.Top = menuStrip1.Height;
chromeBrowser.Height = this.ClientSize.Height - menuStrip1.Height;
chromeBrowser.Width = this.ClientSize.Width;
chromeBrowser.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
      
 

Then you can do things like this:

       
chromeBrowser.ExecuteScriptAsync("document.getElementById(\"ext-comp-1091\").click();"); // stats web menu item
      
 

Fun times.
Copyright Realm Laboratories, LLC