(MVC) MVC (2018)

CefSharp.Winforms.ChromiumWebBrowser minimal example on VB.NET (with cookies collector and script executor).

This is browser form code.

   1:  Imports CefSharp.WinForms
   2:  Imports CefSharp
   3:   
   4:  Partial Public Class BrowserForm
   5:      Inherits Form
   6:   
   7:      Public ReadOnly WebBrowser As ChromiumWebBrowser
   8:   
   9:      Public Sub New()
  10:          InitializeComponent()
  11:          Text = "CefSharp"
  12:          WindowState = FormWindowState.Maximized
  13:          WebBrowser = New ChromiumWebBrowser("about:blank") With {
  14:              .Dock = DockStyle.Fill
  15:          }
  16:   
  17:          toolStripContainer.ContentPanel.Controls.Add(WebBrowser)
  18:          AddHandler WebBrowser.IsBrowserInitializedChanged, AddressOf OnIsBrowserInitializedChanged
  19:          AddHandler WebBrowser.LoadingStateChanged, AddressOf OnLoadingStateChanged
  20:          AddHandler WebBrowser.ConsoleMessage, AddressOf OnBrowserConsoleMessage
  21:          AddHandler WebBrowser.StatusMessage, AddressOf OnBrowserStatusMessage
  22:          AddHandler WebBrowser.TitleChanged, AddressOf OnBrowserTitleChanged
  23:          AddHandler WebBrowser.AddressChanged, AddressOf OnBrowserAddressChanged
  24:          AddHandler WebBrowser.FrameLoadEnd, AddressOf OnFrameLoadEnd
  25:   
  26:          Dim bitness = If(Environment.Is64BitProcess, "x64", "x86")
  27:          Dim version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", CefSharp.Cef.ChromiumVersion, CefSharp.Cef.CefVersion, CefSharp.Cef.CefSharpVersion, bitness)
  28:          DisplayOutput(version)
  29:      End Sub
  30:   
  31:   
  32:      Private Sub OnFrameLoadEnd(sender As Object, e As FrameLoadEndEventArgs)
  33:          Dim task As Task = New Task(Sub()
  34:                                          If e.Url.Contains("jotform") Then
  35:                                              PrintCookies(e.Url, e.HttpStatusCode, e.Frame, e.Browser)
  36:                                              EvaluateScript(WebBrowser, Script1.Value)
  37:                                          End If
  38:                                      End Sub)
  39:          task.Start()
  40:      End Sub
  41:   
  42:      Private Shared Function EvaluateScript(browser1 As ChromiumWebBrowser, ByVal script As String) As Object
  43:          Dim task = browser1.EvaluateScriptAsync(script)
  44:          task.Wait()
  45:          Dim response As JavascriptResponse = task.Result
  46:          'Return If(response.Success, (If(response.Result, "")), response.Message)
  47:          If response.Success Then
  48:              If response.Result Is Nothing Then
  49:                  Return ""
  50:              Else
  51:                  Return response.Result
  52:              End If
  53:          Else
  54:              Return response.Message
  55:          End If
  56:   
  57:      End Function
  58:   
  59:      Public Visitor As CookieMonster
  60:      Sub PrintCookies(Url As String, HttpStatusCode As Integer, Frame As IFrame, Browser As IBrowser)
  61:          Visitor = New CookieMonster()
  62:          Dim CookieManager = WebBrowser.GetCookieManager
  63:          If CookieManager.VisitUrlCookies(Url, True, Visitor) Then Visitor.WaitForAllCookies()
  64:   
  65:          Dim sb = New Text.StringBuilder()
  66:          For Each nameValue In Visitor.NamesValues
  67:              sb.AppendLine(nameValue.Item1 + " = " + nameValue.Item2)
  68:          Next
  69:   
  70:          MessageBox.Show("Cookies:" & vbCrLf & sb.ToString, Url & "(" & HttpStatusCode & ")")
  71:      End Sub
  72:   
  73:      'Script Example
  74:      Function Script1() As XElement
  75:          Return _
  76:              <script>
  77:  function downloadFile(urlToSend) {
  78:       var req = new XMLHttpRequest();
  79:       req.open("GET", urlToSend, true);
  80:       req.responseType = "blob";
  81:       req.onload = function (event) {
  82:           var blob = req.response;
  83:           var fileName = req.getResponseHeader("fileName") //if you have the fileName header available
  84:           var link=document.createElement('a');
  85:           link.href=window.URL.createObjectURL(blob);
  86:           link.download=fileName;
  87:           link.click();
  88:       };
  89:       req.send();
  90:   }
  91:              </script>
  92:      End Function
  93:   
  94:   
  95:      Private Sub OnIsBrowserInitializedChanged(ByVal sender As Object, ByVal e As IsBrowserInitializedChangedEventArgs)
  96:          If e.IsBrowserInitialized Then
  97:              Dim b = (CType(sender, ChromiumWebBrowser))
  98:              Me.InvokeOnUiThreadIfRequired(Function() b.Focus())
  99:          End If
 100:      End Sub
 101:   
 102:      Private Sub OnBrowserConsoleMessage(ByVal sender As Object, ByVal args As ConsoleMessageEventArgs)
 103:          DisplayOutput(String.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message))
 104:      End Sub
 105:   
 106:      Private Sub OnBrowserStatusMessage(ByVal sender As Object, ByVal args As StatusMessageEventArgs)
 107:          Me.InvokeOnUiThreadIfRequired(Sub() statusLabel.Text = args.Value)
 108:      End Sub
 109:   
 110:      Private Sub OnLoadingStateChanged(ByVal sender As Object, ByVal args As LoadingStateChangedEventArgs)
 111:          SetCanGoBack(args.CanGoBack)
 112:          SetCanGoForward(args.CanGoForward)
 113:          Me.InvokeOnUiThreadIfRequired(Sub() SetIsLoading(Not args.CanReload))
 114:      End Sub
 115:   
 116:      Private Sub OnBrowserTitleChanged(ByVal sender As Object, ByVal args As TitleChangedEventArgs)
 117:          Me.InvokeOnUiThreadIfRequired(Sub() Text = args.Title)
 118:      End Sub
 119:   
 120:      Private Sub OnBrowserAddressChanged(ByVal sender As Object, ByVal args As AddressChangedEventArgs)
 121:          Me.InvokeOnUiThreadIfRequired(Sub() urlTextBox.Text = args.Address)
 122:      End Sub
 123:   
 124:      Private Sub SetCanGoBack(ByVal canGoBack As Boolean)
 125:          Me.InvokeOnUiThreadIfRequired(Sub() backButton.Enabled = canGoBack)
 126:      End Sub
 127:   
 128:      Private Sub SetCanGoForward(ByVal canGoForward As Boolean)
 129:          Me.InvokeOnUiThreadIfRequired(Sub() forwardButton.Enabled = canGoForward)
 130:      End Sub
 131:   
 132:      Private Sub SetIsLoading(ByVal isLoading As Boolean)
 133:          goButton.Text = If(isLoading, "Stop", "Go")
 134:          goButton.Image = If(isLoading, My.Resources.nav_plain_red, My.Resources.nav_plain_green)
 135:          HandleToolStripLayout()
 136:      End Sub
 137:   
 138:      Public Sub DisplayOutput(ByVal output As String)
 139:          Me.InvokeOnUiThreadIfRequired(Function() outputLabel.Text = output)
 140:      End Sub
 141:   
 142:      <System.Diagnostics.DebuggerStepThrough()>
 143:      Private Sub HandleToolStripLayout(ByVal sender As Object, ByVal e As LayoutEventArgs) Handles toolStrip1.Layout
 144:          HandleToolStripLayout()
 145:      End Sub
 146:   
 147:      <System.Diagnostics.DebuggerStepThrough()>
 148:      Private Sub HandleToolStripLayout()
 149:          Dim width = toolStrip1.Width
 150:   
 151:          For Each item As ToolStripItem In toolStrip1.Items
 152:   
 153:              If item IsNot urlTextBox Then
 154:                  width -= item.Width - item.Margin.Horizontal
 155:              End If
 156:          Next
 157:   
 158:          urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18)
 159:      End Sub
 160:   
 161:      Private Sub ExitMenuItemClick(ByVal sender As Object, ByVal e As EventArgs) Handles exitToolStripMenuItem.Click
 162:          WebBrowser.Dispose()
 163:          CefSharp.Cef.Shutdown()
 164:          Close()
 165:      End Sub
 166:   
 167:      Private Sub GoButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles goButton.Click
 168:          LoadUrl(urlTextBox.Text)
 169:      End Sub
 170:   
 171:      Private Sub BackButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles backButton.Click
 172:          WebBrowser.Back()
 173:      End Sub
 174:   
 175:      Private Sub ForwardButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles forwardButton.Click
 176:          WebBrowser.Forward()
 177:      End Sub
 178:   
 179:      Private Sub UrlTextBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles urlTextBox.KeyUp
 180:          If e.KeyCode <> Keys.Enter Then
 181:              Return
 182:          End If
 183:   
 184:          LoadUrl(urlTextBox.Text)
 185:      End Sub
 186:   
 187:      Private Sub LoadUrl(ByVal url As String)
 188:          If Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute) Then
 189:              WebBrowser.Load(url)
 190:          End If
 191:      End Sub
 192:   
 193:      Private Sub ShowDevToolsMenuItemClick(ByVal sender As Object, ByVal e As EventArgs) Handles showDevToolsToolStripMenuItem.Click
 194:          WebBrowser.ShowDevTools()
 195:      End Sub
 196:   
 197:   
 198:  End Class


Comments ( )
Link to this page: //www.vb-net.com/CefSharp_ChromiumWebBrowser/FormCode.htm
< THANKS ME>