VB.Net how to get the Page Source

2,406 views
Skip to first unread message

Philip Holan

unread,
Oct 17, 2015, 9:03:20 AM10/17/15
to CefSharp
I have gone through the documentation and I can't find a way to get the page source using VB

Here is what I have:

Public Function OnBeforeResourceLoad(browserControl As IWebBrowser, objIBrowser As IBrowser, frame As IFrame, request As IRequest, callback As IRequestCallback) As CefReturnValue Implements IRequestHandler.OnBeforeResourceLoad
    Dim sb As New System.Text.StringBuilder
    sb.AppendLine("document.getElementByTagName('html')[0].innerHTML;")
    Dim objTask As Task = browser.EvaluateScriptAsync(sb.ToString())
    Dim myFunc = Function(t)
                   If Not t.IsFaulted Then
                     Dim response = t.Result
                     If response.Success = True Then
                       MessageBox.Show(response.Result.ToString())
                     End If
                   End If
                 End Function
    Try
      objTask.ContinueWith(myFunc, TaskScheduler.FromCurrentSynchronizationContext())
    Catch ex As Exception
      Stop
    End Try


    callback.Continue(True)
    Return CefReturnValue.ContinueAsync
  End Function


 
Thanks for the help

Alex Maitland

unread,
Oct 17, 2015, 5:26:32 PM10/17/15
to CefSharp
Please include the version your using when asking a question.

Have you tried `GetSourceAsync`?

https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.WinForms.Example/BrowserTabUserControl.cs#L219

Philip Holan

unread,
Oct 17, 2015, 9:13:25 PM10/17/15
to CefSharp
I'm using the latest version.
GetSourceAsync does not work using VB.Net because it required adding the prefix 'Await' in VB for the call and the function declaration as well. Once added to the function declaration (OnBeforeResourceLoad) then the compiler complains...

Alex Maitland

unread,
Oct 17, 2015, 11:28:58 PM10/17/15
to CefSharp
Please specify the exact version, `latest version` is relative.

What exactly are you trying to do?

There is only a limited set of functions that you can call in `OnBeforeResourceLoad`, as the page hasn't actually loaded yet (also `OnBeforeResourceLoad` is called for every resource that's loaded, css/js/images). As the name suggests the resource has yet to be loaded.

Typically you'd hook the `LoadingStateChanged` event, check the ages for `IsLoading = false` and then call your methods like `GetSourceAsync`

https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp/IWebBrowser.cs#L54
Message has been deleted

Philip Holan

unread,
Oct 18, 2015, 5:39:25 AM10/18/15
to CefSharp
To be more specific, when a user clicks on the 'Submit' button on a form, I need to be able to look at the HTML source as well as the page title. The POST form content is not really necessary.
Chromium: 43.0.2357.130, CEF: r3.2357.1287.g861c26e, CefSharp: 43.0.0.0, Environment: x86

Alex Maitland

unread,
Oct 18, 2015, 6:22:12 AM10/18/15
to CefSharp
So the whole idea of having a callback is so you can perform an async task. I'm not sure if you can get the source or execute javascript in OnBeforeResourceLoad, I guess you can give it a try.

Spawn a task to perform your async operations, return CefReturnValue.ContinueAsync immediately.

When you've async operations have completed, then execute the callback. (best to Dispose of a callback as it wraps an unmanaged resource).

See the link before for a similar example (spawns a task to perform some processing and returns immediately).
https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.Example/CefSharpSchemeHandler.cs#L58

You can also access the POST form fields through the IRequest object. You'll also likely need to check IRequest.Method == "POST"

Philip Holan

unread,
Oct 18, 2015, 8:37:03 AM10/18/15
to CefSharp
Alex,

Is there a donation page for the CefSharp project?

Alex Maitland

unread,
Oct 20, 2015, 5:32:10 AM10/20/15
to CefSharp
Phil,

There is no donation page.

Philip Holan

unread,
Oct 20, 2015, 8:37:30 AM10/20/15
to CefSharp
Alex,

1.- How can I contribute back to CefSharp for your support? The most obvious way that comes to mind is financially.
2.- When using request.PostData.Elements, is there a way to change the value of an element or else, I can I change the form Post Data value when a form is submitted?

Philip

Alex Maitland

unread,
Oct 20, 2015, 4:41:12 PM10/20/15
to CefSharp
Appreciate the offer!

I've personally never tried to append the post data, so I can't give you a definitive answer. I think it should be possible.


`CEF` just provides an array of bytes for the different elements. So it should be possible to take the current data, convert it to a string, append the values then convert it back.

The `GetBody` extension method is there for convenience to convert the data to a string. More extension methods would probably be useful if you come up with something.

https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp/PostDataExtensions.cs#L78

Philip Holan

unread,
Mar 13, 2016, 11:37:03 PM3/13/16
to cefs...@googlegroups.com

Hi,

 

I would like to request your help with a problem that I can’t figure out. I’m using the class from the example below, which is a separate class from the one where the Cef browser is created.

 

When MyMethod get’s called, how can I then use function:

Await CEFBrowser.EvaluateScriptAsync (in VB)?

 

Thanks,

3. How do you expose a .NET class to Javascript?

Like this:

public class BoundObject

{

    public string MyProperty { get; set; }

    public void MyMethod()

    {

        // Do something really cool here.

    }

}

 

// ...

 

// After your ChromiumWebBrowser has been created

browser.RegisterJsObject("bound", new BoundObject());

In the actual JS code, you would use the object like this:

bound.myProperty; // use this syntax to access the property

bound.myMethod(); // use this to call the method.

--
You received this message because you are subscribed to a topic in the Google Groups "CefSharp" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cefsharp/wpRQXyOlnxE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cefsharp+u...@googlegroups.com.
To post to this group, send email to cefs...@googlegroups.com.
Visit this group at http://groups.google.com/group/cefsharp.
For more options, visit https://groups.google.com/d/optout.

Alex Maitland

unread,
Apr 4, 2016, 5:36:45 PM4/4/16
to CefSharp
This Group is now basically defunct and as a result is not being actively monitored.

If you still require assistance please ask on https://gitter.im/cefsharp/CefSharp#
Reply all
Reply to author
Forward
0 new messages