从JavaFX WebView Control调用JavaScript方法

时间:2013-05-24 07:43:52

标签: java javascript webview javafx-2

我试图在javaFx webview控件中调用javascript函数,我已经尝试了所有可能的组合,但我没有解决它

我的js文件是

function getHTMLOfSelection () {
   var range;
   if (document.selection && document.selection.createRange) {
   range = document.selection.createRange();
   alert("executed 1");
   return range.htmlText;
   }
   else if (window.getSelection) {
     var selection = window.getSelection();
     if (selection.rangeCount > 0) {
     range = selection.getRangeAt(0);
     var clonedSelection = range.cloneContents();
     var div = document.createElement('div');
     div.appendChild(clonedSelection);
     alert(div.innerHTML)    ;
     return div.innerHTML;  
   }
    else {
     alert("executed 3");
     return '';
    }
  }
  else {
    return '';
  }
}

我的java文件是

  package test;

  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;


  public class JavaFX_GoogleMaps extends Application {
      public Scene scene;
      MyBrowser myBrowser;
      public JavaFX_GoogleMaps () {  }
      public static void initJavaFX_GoogleMaps () {
         launch("hello");
      }
      @Override
      public void start(Stage primaryStage) {
      primaryStage.setTitle("My Application");
      myBrowser = new MyBrowser();
      scene = new Scene(myBrowser, 800, 600);
      primaryStage.setScene(scene);
      primaryStage.show();
  }

  public static void main(String[] args) {
     //launch(args); 
     JavaFX_GoogleMaps.initJavaFX_GoogleMaps();
  }


  class MyBrowser extends Region {
     HBox toolbar;
     static final WebView webView = new WebView();
     static final WebEngine webEngine = webView.getEngine();
     final Button button = new Button("Add Marker");
     String str;

     public MyBrowser () {

       try {
          webEngine.load(new  File("F:/ce/ceacts/ceacts44_2.htm").toURI().toURL().toString());
      } catch(Exception e) {
          System.out.println("Input error "+e);
      }

     toolbar = new HBox();
     toolbar.getChildren().addAll(button);
     getChildren().add(webView);
     button.setOnAction(new EventHandler<ActionEvent>() {
     @Override public void handle(ActionEvent t) {

         Platform.runLater(new Runnable() {
            @Override
             public void run() {
             try {

                     str = webEngine.executeScript(
                            "function getSelectedText() {"
                     + " var range;"
                     + "if (document.selection && document.selection.createRange) {"
                     + "range = document.selection.createRange();"
                     + "alert(range.htmlText);"
                     + "return range.htmlText;"
                     + "}"
                     + " else if (window.getSelection) {"
                     + " var selection = window.getSelection();"
                     + "if (selection.rangeCount > 0) {"
                     + " range = selection.getRangeAt(0);"
                     + "var clonedSelection = range.cloneContents();"
                     + " var div = document.createElement('div');"
                     + "div.appendChild(clonedSelection);"
                     + " return div.innerHTML;"
                     + "}"
                     + " else {"
                     + "alert('else 1');"  
                     + "return '';"
                     + "}"
                     + " }"
                     + "else {"
                     + "alert('else 2');"
                             + "document.write('In Else2');"
                             + "document.write('else 2');"
                     + "return '';"
                     + "}"
                     + "}").toString();


             } catch(Exception e ) {
                    System.out.println("Exception in executing script " +e);
                }

                System.out.println(" Value of str  " +str);
               }
        });

     }});
  getChildren().add(toolbar); 

  } 

 }

当我在谷歌浏览器浏览器中调用此js文件时,它工作正常,但是当我们从javafx webview浏览器调用上面的代码时,它无效。抛出投掷   “读取js文件中的异常javax.script.scriptException:sun.org.mozilla.javascript internal.EcmaError:ReferenceError:”document“未定义”

先谢谢

0 个答案:

没有答案