yueying0083 / javachromiumembedded

Automatically exported from code.google.com/p/javachromiumembedded
0 stars 0 forks source link

Is it possible to provide java callbacks from javascript? #134

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The JavaFX Web View offers the possibility to register Java objects as 
JavaScript objects, so the web page can make callbacks to the Java application. 
It would be nice to have this in JCEF as well.

Here is the example from the JavaFX API documentation:

// A simple class...
public class JavaApplication {
    public void exit() {
        System.exit(0);
    }
}

// Code for registering the Java object as JavaScript object
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("app", new JavaApplication());

<!-- callback in JavaScript --> 
<a href="" onclick="app.exit()">Click here to exit application</a>

Original issue reported on code.google.com by th...@literarisch-wertvoll.de on 7 Nov 2014 at 9:32

GoogleCodeExporter commented 8 years ago
Hi Thies,

It's already implemented and called CefMessageRouter. Start detailed MainFrame 
example application, goto "Bookmarks" and select "Binding Test" and/or "Binding 
Test 2" for a more complex example.

See tests.detailed.MainFrame.java (around line 136) for the simple integration:

    //    Beside the normal handler instances, we're registering a MessageRouter
    //    as well. That gives us the opportunity to reply to JavaScript method
    //    calls (JavaScript binding). We're using the default configuration, so
    //    that the JavaScript binding methods "cefQuery" and "cefQueryCancel"
    //    are used.
    CefMessageRouter msgRouter = CefMessageRouter.create();
    msgRouter.addHandler(new MessageRouterHandler(), true);
    msgRouter.addHandler(new MessageRouterHandlerEx(client_), false);
    client_.addMessageRouter(msgRouter);

The handlers are implemented in tests.detailed.handler.MessageRouterHandler 
(for "Binding Test") and MessageRouterHandlerEx (for "Binding Test 2"). The 
corresponding HTML is in tests.detailed.handler.binding_test.html and  
tests.detailed.handler.binding2_test.html

Regards,
Kai

Original comment by k...@censhare.de on 10 Nov 2014 at 7:21

GoogleCodeExporter commented 8 years ago

Original comment by magreenb...@gmail.com on 12 Nov 2014 at 8:29