[SOLVED] Get page title & favicon, block ads and more with CEFSharp

Im trying to get TWBP and CEFSharp to work together and I have a few questions.

How do you display the favicon and the webpage title in the twbp tabpage?

Oh, and also, Im trying to get adblock to work with CEFSharp. I got the code in json but how do you make CEFSharp execute it?

And how do you open createdwebview in a newtab or window?

And lastly, how do you remember passwords?

Help is appreciated

Like 0 Dislike

Why isnt anybody replying

Like 0 Dislike

[notify]ShoutOut! Technology[/notify] Oh oops, forgot about this one.

You can refer to the MinimalExample for CEFSharp, just to see how to get some of the basic functionality working (such as getting the page title and such), it is in C#; although it doesn't differ too much from what you'd have to type in VB.NET (other than a few syntactical differences): https://github.com/cefsharp/CefSharp.Mi … serForm.cs

To answer some of your questions:

1. How do I get the webpage title in CEFSharp?
So from looking at the MinimalExample, you can only fetch the webpage title within the "OnBrowserTitleChanged" event, like this:

Form1.Text = e.Title

2. How do I get the webpage favicon in CEFSharp?
The easiest solution to this one would be to just use a HttpWebRequest and get the "favicon.ico" file from the root of the loaded webpage, like this:

Try
  'Get the favicon from the root of the loaded website
  Dim url As Uri
  Form1.InvokeOnUiThreadIfRequired((Sub() url = CefBrowser.Address)
  Dim iconURL = "http://" & url.Host & "/favicon.ico"
  Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
  Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
  Dim stream As System.IO.Stream = response.GetResponseStream()
  Dim favicon = Image.FromStream(stream)
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = favicon
Catch ex As Exception
  'Show a blank page icon if no icon is found
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.pageIcon
End Try

You'll have to import the following:

Imports CefSharp.WinForms.Internals

3. How do I block ads in CEFSharp?
You can execute some JavaScript using the "ExecuteScriptAsync" method, to try and change things on the current webpage (such as removing all detected ads). I don't have any JavaScript code on me which would do something of the sort, so you may have to look through some of the code of other 'AdBlock' extensions to see how they do it.

4. How do I handle new window/tab creation events in CEFSharp?
The following question has already been answered elsewhere (it is in C#, although it shouldn't be too hard to convert into working VB.NET code): https://stackoverflow.com/questions/305 … n-cefsharp

5. How do I remember passwords in CEFSharp?
This is a lot easier said than done, I'd suggest steering away from this sort of feature until you get a little more familiar with both the language and CEFSharp. Handling other people's password's should be done with a fairly security-conscious approach, and should never be "half-arsed" so to speak.

Hope this answers some of your questions! 1f609

Like 0 Dislike

I tried to use the title method you mentioned in TWBP but it kept giving me an error saying reference not set to an instance of an object.

admin wrote:

@ShoutOut! Technology Oh oops, forgot about this one.

You can refer to the MinimalExample for CEFSharp, just to see how to get some of the basic functionality working (such as getting the page title and such), it is in C#; although it doesn't differ too much from what you'd have to type in VB.NET (other than a few syntactical differences): https://github.com/cefsharp/CefSharp.Mi … serForm.cs

To answer some of your questions:

1. How do I get the webpage title in CEFSharp?
So from looking at the MinimalExample, you can only fetch the webpage title within the "OnBrowserTitleChanged" event, like this:

Form1.Text = e.Title

2. How do I get the webpage favicon in CEFSharp?
The easiest solution to this one would be to just use a HttpWebRequest and get the "favicon.ico" file from the root of the loaded webpage, like this:

Try
  'Get the favicon from the root of the loaded website
  Dim url = CefBrowser.Address
  Dim iconURL = "http://" & url.Host & "/favicon.ico"
  Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
  Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
  Dim stream As System.IO.Stream = response.GetResponseStream()
  Dim favicon = Image.FromStream(stream)
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = favicon
Catch ex As Exception
  'Show a blank page icon if no icon is found
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.pageIcon
End Try

3. How do I block ads in CEFSharp?
You can execute some JavaScript using the "ExecuteScriptAsync" method, to try and change things on the current webpage (such as removing all detected ads). I don't have any JavaScript code on me which would do something of the sort, so you may have to look through some of the code of other 'AdBlock' extensions to see how they do it.

4. How do I handle new window/tab creation events in CEFSharp?
The following question has already been answered elsewhere (it is in C#, although it shouldn't be too hard to convert into working VB.NET code): https://stackoverflow.com/questions/305 … n-cefsharp

5. How do I remember passwords in CEFSharp?
This is a lot easier said than done, I'd suggest steering away from this sort of feature until you get a little more familiar with both the language and CEFSharp. Handling other people's password's should be done with a fairly security-conscious approach, and should never be "half-arsed" so to speak.

Hope this answers some of your questions! 1f609

Like 0 Dislike

[notify]ShoutOut! Technology[/notify] Did you make sure to only call it within the "OnBrowserTitleChanged" event? Otherwise you may just have to invoke it, like this:

Form1.BeginInvoke(Sub() Form1.Text = e.Title)

Like 0 Dislike

I used your favicon method like this...

        Me.Invoke(New MethodInvoker(Function()

                                        Try
                                            Dim url = browser.Address
                                            Dim iconURL = "http://" & e.Address & "/favicon.ico"
                                            Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
                                            Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
                                            Dim stream As System.IO.Stream = response.GetResponseStream()
                                            Dim favicon = Image.FromStream(stream)
                                            Form1.TabControl.TabContainer.getTabFromForm(Me).TabIcon = favicon
                                        Catch __unusedNullReferenceException1__ As NullReferenceException

                                            Form1.TabControl.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.window
                                        End Try



                                        Return 0
                                        End Function))

but it gave me this error...

System.Net.WebException
  HResult=0x80131509
  Message=The remote name could not be resolved: 'https'
  Source=System
  StackTrace:
   at System.Net.HttpWebRequest.GetResponse()
   at HexWeb.BrowserTab._Closure$__54-0._Lambda$__0() in c:\users\vincent\source\repos\HexWeb\HexWeb\BrowserTab.vb:line 65
   at HexWeb.BrowserTab._Closure$__54-0._Lambda$__R2()
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
admin wrote:

@ShoutOut! Technology Oh oops, forgot about this one.

You can refer to the MinimalExample for CEFSharp, just to see how to get some of the basic functionality working (such as getting the page title and such), it is in C#; although it doesn't differ too much from what you'd have to type in VB.NET (other than a few syntactical differences): https://github.com/cefsharp/CefSharp.Mi … serForm.cs

To answer some of your questions:

1. How do I get the webpage title in CEFSharp?
So from looking at the MinimalExample, you can only fetch the webpage title within the "OnBrowserTitleChanged" event, like this:

Form1.Text = e.Title

2. How do I get the webpage favicon in CEFSharp?
The easiest solution to this one would be to just use a HttpWebRequest and get the "favicon.ico" file from the root of the loaded webpage, like this:

Try
  'Get the favicon from the root of the loaded website
  Dim url = CefBrowser.Address
  Dim iconURL = "http://" & url.Host & "/favicon.ico"
  Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
  Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
  Dim stream As System.IO.Stream = response.GetResponseStream()
  Dim favicon = Image.FromStream(stream)
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = favicon
Catch ex As Exception
  'Show a blank page icon if no icon is found
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.pageIcon
End Try

3. How do I block ads in CEFSharp?
You can execute some JavaScript using the "ExecuteScriptAsync" method, to try and change things on the current webpage (such as removing all detected ads). I don't have any JavaScript code on me which would do something of the sort, so you may have to look through some of the code of other 'AdBlock' extensions to see how they do it.

4. How do I handle new window/tab creation events in CEFSharp?
The following question has already been answered elsewhere (it is in C#, although it shouldn't be too hard to convert into working VB.NET code): https://stackoverflow.com/questions/305 … n-cefsharp

5. How do I remember passwords in CEFSharp?
This is a lot easier said than done, I'd suggest steering away from this sort of feature until you get a little more familiar with both the language and CEFSharp. Handling other people's password's should be done with a fairly security-conscious approach, and should never be "half-arsed" so to speak.

Hope this answers some of your questions! 1f609

Like 0 Dislike

You don't need to invoke the whole "Try" statement, just the part which relates directly to the browser control, like this:

Try
  'Get the favicon from the root of the loaded website
  Dim url As Uri
  Form1.InvokeOnUiThreadIfRequired((Sub() url = CefBrowser.Address)
  Dim iconURL = "http://" & url.Host & "/favicon.ico"
  Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
  Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
  Dim stream As System.IO.Stream = response.GetResponseStream()
  Dim favicon = Image.FromStream(stream)
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = favicon
Catch ex As Exception
  'Show a blank page icon if no icon is found
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.pageIcon
End Try

I'm using the following method to invoke, it's a bit cleaner and does pretty much the exact same thing as the other methods mentioned:

admin wrote:

Nice! You could also try this (as used in the "MinimalExample", here: https://github.com/cefsharp/CefSharp.Mi … orm.cs#L49):

Form1.InvokeOnUiThreadIfRequired((Sub() Form1.TwbpTabs1.TabContainer.getSelectedTab.TabTitle = e.Title)

You'll have to import the following:

Imports CefSharp.WinForms.Internals

Like 0 Dislike

It says url.host isn't a member of chromiumbrowser.address.

Like 0 Dislike

Oh oops, I messed up line 3 (I'm a little too used to C#), this should do it:

admin wrote:

You don't need to invoke the whole "Try" statement, just the part which relates directly to the browser control, like this:

Try
  'Get the favicon from the root of the loaded website
  Dim url As Uri
  Form1.InvokeOnUiThreadIfRequired((Sub() url = CefBrowser.Address)
  Dim iconURL = "http://" & url.Host & "/favicon.ico"
  Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
  Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
  Dim stream As System.IO.Stream = response.GetResponseStream()
  Dim favicon = Image.FromStream(stream)
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = favicon
Catch ex As Exception
  'Show a blank page icon if no icon is found
  Form1.TwbpTabs1.TabContainer.getTabFromForm(Me).TabIcon = My.Resources.pageIcon
End Try

I'm using the following method to invoke, it's a bit cleaner and does pretty much the exact same thing as the other methods mentioned:

admin wrote:

Nice! You could also try this (as used in the "MinimalExample", here: https://github.com/cefsharp/CefSharp.Mi … orm.cs#L49):

Form1.InvokeOnUiThreadIfRequired((Sub() Form1.TwbpTabs1.TabContainer.getSelectedTab.TabTitle = e.Title)

You'll have to import the following:

Imports CefSharp.WinForms.Internals

Like 0 Dislike

I have the code for favicons... Actually, I have the code for everything in CEFSharp although they are all in c#.
To achieve a favicon in the tabs, you need to add a webaddress_changed sub in the form load. For C#use this code...

  this.Invoke(new MethodInvoker(() =>
            {

                autocompeltepnl.Visible = false;
                webinfopanel.Visible = false;
                Uri Urlh = new Uri("https://" + new Uri(web.Address).Host + "/favicon.ico");
                try
                {
                    Image Img = new Bitmap(new System.IO.MemoryStream(new WebClient().DownloadData(Urlh)));
                    Bitmap theBitmap = new Bitmap(Img, new Size(32, 32));
                    IntPtr Hicon = theBitmap.GetHicon();// Get an Hicon for myBitmap.
                    Icon favicon = Icon.FromHandle(Hicon);// Create a new icon from the handle.
                    ParentTabs.SelectedTab.Icon = favicon;
                    faviconurl.Image = Img;
                    favicconwebinfo.Image = Img;

                }
                catch (Exception)
                {
                    Image fav = new Bitmap(Resources.favicons);
                    Bitmap theBitmap = new Bitmap(fav, new Size(32, 32));
                    IntPtr Hicon = theBitmap.GetHicon();// Get an Hicon for myBitmap.
                    Icon favno = Icon.FromHandle(Hicon);// Create a new icon from the handle
                    ParentTabs.SelectedTab.Icon = favno;
                    faviconurl.Image = fav;
                }

A rough translation to vb would be:

Private Sub web_addresschanged(ByVal sender As Object, ByVal e As AddressChangedEventArgs)
    Me.Invoke(New MethodInvoker(Function()
        autocompeltepnl.Visible = False
        webinfopanel.Visible = False
        Dim Urlh As Uri = New Uri("https://" & New Uri(web.Address).Host & "/favicon.ico")
        Try
            Dim Img As Image = New Bitmap(New System.IO.MemoryStream(New WebClient().DownloadData(Urlh)))
            Dim theBitmap As Bitmap = New Bitmap(Img, New Size(32, 32))
            Dim Hicon As IntPtr = theBitmap.GetHicon()
            Dim favicon As Icon = Icon.FromHandle(Hicon)
            ParentTabs.SelectedTab.Icon = favicon
            faviconurl.Image = Img
            favicconwebinfo.Image = Img
        Catch __unusedException1__ As Exception
            Dim fav As Image = New Bitmap(Resources.favicons)
            Dim theBitmap As Bitmap = New Bitmap(fav, New Size(32, 32))
            Dim Hicon As IntPtr = theBitmap.GetHicon()
            Dim favno As Icon = Icon.FromHandle(Hicon)
            ParentTabs.SelectedTab.Icon = favno
            faviconurl.Image = fav
        End Try
    End Function))
End Sub
A post by brwck

Like 0 Dislike