我正在尝试在 javaFx webview Control 中调用 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);
}
}
当我在 google chrome 浏览器中调用这个 js 文件时,它工作正常,但是当我们像上面的代码一样从 javafx webview 浏览器调用时它不起作用。它抛出
“读取 js 文件 javax.script.scriptException 时出现异常:sun.org.mozilla.javascript internal.EcmaError:ReferenceError :“document”未定义”
提前致谢