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

Remove CefSharp.WebBrowserExtensions.RegisterJsObject/RegisterAsyncJsObject #2990

Closed
amaitland opened this issue Dec 10, 2019 · 25 comments
Closed

Comments

@amaitland
Copy link
Member

amaitland commented Dec 10, 2019

Firstly this is only a minor change to allow for some upcoming changes, the changes to your code are fairly simple.

The methods have been removed to page the way for Legacy Binding being available on a per object basis rather than a per browser basis. See #2977 for details on that change (hopefully be available in the next major release).

For those upgrading here are updated examples.

RegisterJsObject

//Old method
browser.RegisterJsObject("bound", new BoundObject(), options: BindingOptions.DefaultBinder);

//Replaced with
CefSharpSettings.WcfEnabled = true;
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), isAsync:false, options: BindingOptions.DefaultBinder);

RegisterAsyncJsObject

//Old Method
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject(), options: BindingOptions.DefaultBinder);

//Replaced with
browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
browser.JavascriptObjectRepository.Register("boundAsync", new AsyncBoundObject(), isAsync: true, options: BindingOptions.DefaultBinder);
@amaitland amaitland added this to the 79.0.0 milestone Dec 11, 2019
@amaitland
Copy link
Member Author

amaitland commented Dec 11, 2019

Slight change of plans, I'll revert the removal

  • Mark the methods Obsolete
  • Throw exception to provide better communication for users on how to migrate their code (method won't do anything effectively)

@amaitland
Copy link
Member Author

The methods now throw an exception that links to this issue, there are code examples above for code migration.

@miskryt

This comment has been minimized.

@JohnDola

This comment has been minimized.

@khiemnd777

This comment has been minimized.

@amaitland
Copy link
Member Author

You can review the code that was removed at ffaff5d#diff-7ac45247db5707eecab6fe1290834329L56

@zornflute

This comment has been minimized.

@zornflute

This comment has been minimized.

@amaitland
Copy link
Member Author

No functionality changes were made. If you have an example that uses one of the two removed methods then you will need to post code. Off-topic questions won't be answered. Statements saying it doesn't work cannot be answered unless accompanied by code that you are having trouble with.

@mridah

This comment has been minimized.

@amaitland

This comment has been minimized.

@miskryt

This comment has been minimized.

@fandrei

This comment has been minimized.

@amaitland

This comment has been minimized.

@fandrei

This comment has been minimized.

@amaitland

This comment has been minimized.

@amaitland
Copy link
Member Author

For those migrating
from CefSharp.WebBrowserExtensions.RegisterJsObject/RegisterAsyncJsObject please include the following when posting a comment here:

  • Version you are upgrading from
  • A code example that shows your current RegisterJsObject/RegisterAsyncJsObject usage

Please keep comments on topic (migrating from an older version where you used one of the two methods listed above)


Method names are transformed to CamelCase (the first letter is transformed to lowercase, MyFunction becomes myFunction) by default. This is configurable by setting BindingOptions.CamelCaseJavascriptNames when you register your object, example below.

For those of you seeing an error similar to Uncaught TypeError MyFunction is not a function. Please read cefsharp/Questions-and-Support#40 (comment) and post a comment there if you require assistance.

@AndersBillLinden
Copy link

Problems migrating to the new function is reported at

https://stackoverflow.com/questions/65291697/switching-from-chromebrowser-registerasyncjsobject-to-chromebrowser-javascriptob

(Function calls from js to the cef container application where never delivered).

@DominikPalo
Copy link
Contributor

DominikPalo commented Apr 8, 2021

It would be nice to update also the WcfEnabled documentation - it still refers to the old way (browser.RegisterJsObject and browser.AsyncRegisterJsObject) instead of the new way which is recommended now browser.JavascriptObjectRepository.Register:

WCF is used by RegisterJsObject feature for Javascript Binding It's reccomended that anyone developing a new application use the RegisterAsyncJsObject version which communicates using native Chromium IPC.

I think a better formulation would be:

WCF is used by JavascriptObjectRepository.Register(isAsync: false) feature for Javascript Binding. It's recommended that anyone developing a new application use the JavascriptObjectRepository.Register(isAsync: true) version which communicates using native Chromium IPC.

@SchreinerK
Copy link

I am using 94.4.5 and somethings changed again

//Replaced with
CefSharpSettings.WcfEnabled = true; //< WcfEnabled not exists.
Browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true; 
// ^- System.Exception: 'JavascriptBindingSettings.LegacyBindingEnabled can no longer be modified, settings must be changed before the underlying browser has been created.'

but i still linked to this page in the browser.RegisterJsObject method.
what is the replacement for the replacement?

@amaitland
Copy link
Member Author

WcfEnabled not exists.

This property only exists in the .Net 4.5.2 projects as .Net Core/.Net 5 don't support WCF.

https://github.com/cefsharp/CefSharp/blob/cefsharp/94/CefSharp/CefSharpSettings.cs#L33

^- System.Exception: 'JavascriptBindingSettings.LegacyBindingEnabled can no longer be modified, settings must be changed before the underlying browser has been created.'

As the exception states you've attempted to change the property after the browser has been created. It's too late. You need to set it earlier.

Further questions please use https://github.com/cefsharp/CefSharp/discussions

@rajnithin
Copy link

how to call object and its method in javascript after registering?

@Robot37
Copy link

Robot37 commented Nov 11, 2023

how to call object and its method in javascript after registering?

First you must create a class in Visual Studio with a method that you will call from JavaScript. Thus

public class BoundObject{

     public void sm(string str){
     MessageBox.Show(str); //display a message
     }
}

The creator of this thread probably forgot to write this because he thought it was too obvious. But in fact, not everyone understands this.
And inside the JavaScript you need to write like this
bound.sm("hello world");

@Robot37
Copy link

Robot37 commented Nov 11, 2023

It seems I have discovered a flaw. The object is added to the MainFrame only. In ChildFrames the object is missing and does not work.

@amaitland
Copy link
Member Author

Further questions please use https://github.com/cefsharp/CefSharp/discussions

As per above, please use discussions.

@cefsharp cefsharp locked as resolved and limited conversation to collaborators Nov 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests