Search code examples
cefsharp

CEFSharp bound object lost when new website is loaded


I am using CEFSharp in one of my projects and all works perfectly fine so far. I am still on V63 of CEFSharp (never touch a running system :-) )

I am using it in VB.NET

The bound object is created after CEFSharp initialisation within Public Sub New() as follows:

 CefSharpSettings.LegacyJavascriptBindingEnabled = True
        Dim obj As New BoundObject()
        obj.browser = browser
        browser.RegisterJsObject("bound", obj)
        AddHandler browser.LoadingStateChanged, AddressOf obj.Browser_LoadingStateChanged

I am then after each "LoadingStateChanged" event of the browser execute a JS script with browser.ExecuteScriptAsync.....its basically an event listner that will fire the bound object when the event occurs (e.g. a mousclick).

This also so far works 100% OK....the event fires and calls the bound object and passes a variable to my VB.NET code via the bound object.

Now my problem is that after I load a new page the bound object is lost.

The event listner will try to call the bound object but I just get an error like "Uncaught ReferenceError: bound is not defined"

Looks like be loading a new page inside CEFSharp the bound object is lost.

I triead the above initialisation code also with FrameLoadEnd event instead, but it behaves exactly the same as LoadingStateChanged event.

Any clue anyone how I can handle (or renew) the bound object whenever I load a new website?

Thanks

Update: In the meantime I tried the binding method V2 as recommended. Using the following code for testing purpose:

'########### TEST CODE  TEST CODE   TEST CODE  ##################
            'try to use new binding V2 method by calling CefSharp.BindObjectAsync:
            browser.ExecuteScriptAsync("CefSharp.BindObjectAsync(""myObject"");")
            'now register object:
            browser.JavascriptObjectRepository.Register("myObject", New BoundObject(), True)
            'now do test execution of myObject:
            browser.ExecuteScriptAsync("myObject(""test"");")

But still struggeling to get it working :-(


Solution

  • The last sugestion of amaitland solved the problem. it works now nicely across various sites. In principle I just replaced this two lines in my original working code:

    Dim obj As New BoundObject()
    obj.browser = browser
    'browser.RegisterJsObject("bound", obj)    'replaced by next line (JS binding V2)
    browser.JavascriptObjectRepository.Register("bound", obj, True, Nothing)
    AddHandler browser.LoadingStateChanged, AddressOf obj.Browser_LoadingStateChanged
    

    And then later within the Sub that handles the browser_LoadingStateChangedAsync event I just added this line beofre any other injected JS:

     'use new binding V2 method by calling CefSharp.BindObjectAsync:
     browser.ExecuteScriptAsync("CefSharp.BindObjectAsync(""bound"");")
    

    I hope this helps others using CEFsharpin VB.NET if they come across the same problem.