Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterJavascriptObject works only on first page loaded #185

Open
sakya opened this issue Nov 6, 2024 · 4 comments · May be fixed by #186
Open

RegisterJavascriptObject works only on first page loaded #185

sakya opened this issue Nov 6, 2024 · 4 comments · May be fixed by #186

Comments

@sakya
Copy link

sakya commented Nov 6, 2024

I tried to use RegisterJavascriptObject in the browser LoadEnd event and I'm seeing a strange behaviour

        private void OnBrowserLoadEnd(object sender, LoadEndEventArgs e)
        {
            Console.WriteLine("OnBrowserLoadEnd");
            if (!browser.RegisterJavascriptObject(new BindingTestClass(), "bindingTest", AsyncCallNativeMethod))
            {
                Console.WriteLine("Failed to Register Javascript Object");
            }
        }

I modified RegisterJavascriptObject to return the boolean value from _objectRegistry.Register.
The strange thing is:

  • When the browser loads google the object is registerer and in the console I can access the object
  • If I navigate to another page (by clicking a link or writing the address) I get the error "Failed to Register Javascript Object" because the object is registered but in the console I get "Uncaught ReferenceError: bindingTest is not defined" when I try to access the object

The code that return false is this

            if (_registeredObjects.ContainsKey(name))
            {
                return false;
            }

This should mean that the object is registered. Why I cannot access it?

@sakya
Copy link
Author

sakya commented Nov 6, 2024

The same happens with clean code using the menu item "Bind Javascript Object".
It works on the first loaded page but if you navigate and retry you get the error in the console

Uncaught ReferenceError: dotNetObject is not defined", source: about:blank (1)

@sakya sakya linked a pull request Nov 7, 2024 that will close this issue
@sakya sakya changed the title RegisterJavascriptObject strange behaviour RegisterJavascriptObject works only on first page loaded Nov 22, 2024
@AndreBooysen
Copy link

I was struggling with the same issue today, The _objectRegistry.SetBrowser(browser); method that sends the registration messages for all the registered objects is only called in the OnBrowserCreated event inside the CommonBrowserAdapter class. I think it needs to be called every time a new page is loading

@AndreBooysen
Copy link

Should have opened your PR first :). I did almost the exact same fix on my side, however in the HandleLoadStart method instead of HandleLoadEnd.

@sakya
Copy link
Author

sakya commented Dec 27, 2024

@AndreBooysen you're right! It's better to call it in the load start event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants