I am trying to call javascript function in javaFx webview Control ,I have tried every possible combination but i didn't solve it
my js file is
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 '';
}
}
And my java file is
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);
}
}
When i call this js file in google chrome browser, it work fine but it's not working when we called from javafx webview browser like above code. It throw throws
"Exception in reading js file javax.script.scriptException: sun.org.mozilla.javascript internal.EcmaError:ReferenceError :"document" is not defined"
Thanks in Advance