Nashorn webview issue

Chris Root hammer65 at gmail.com
Tue Dec 1 05:11:28 UTC 2015


I'm trying to get two way communication working for a browser project in
Nashorn and I'm having trouble implementing an HTML to host callback. I
tried this in Java using setMember and it works perfectly, but I can't seem
to duplicate it with Nashorn. Since using setMember didn't work I decided
to use the approach below but still no luck.

I don't want to pick up objects from the window object, I want to plant a
function in window that will call Nashorn code. Again getting this to work
in Java was no problem. I followed the instructions on this page

https://blogs.oracle.com/nashorn/entry/porting_from_the_browser_to

I have the jsObject wrapper shown in the article and it works fine. but the
code below doesn't work. This is the relevant snip from the load handler.

This.engine.loadWorker.stateProperty().addListener(new ChangeListener() {
    changed: function(value, oldState, newState) {
      switch(newState){
        case Worker.State.SUCCEEDED:
          This.document = wrap(This.engine.executeScript("document"));
          This.window = wrap(This.engine.executeScript("window"));
          This.window.hello = function(){
            This.hello();
          }

As you can see I planted a function on the window called hello, which calls
a method of the webview wrapper (same wrapper as in the article) called
hello()

This.hello = function(){
  print("****** hello it worked *******");
}

I then set up the alert handler

This.engine.onAlert = new javafx.event.EventHandler() {
  handle: function(evt) {
    print(evt.data)
  }
};

and then finally loaded the HTML file below into the webview.

<html>
<head>
  <title>Nashorn Browser</title>

  <script type="text/Javascript">
    function sayhello(){
      try{
        hello();
      }catch(e){
        alert(e.message)
      }
    }
  </script>

</head>
<body>
  <a href="#" onclick="sayhello(); return false;">Say Hello</a>
</body>
</html>

What I get back in the console is.

JavaRuntimeObject is not a function (evaluating 'window.hello()')

Is there any way to establish a callback into my Nashorn code the way it
can be done in Java?


More information about the nashorn-dev mailing list