I have a WebView element, which render a html code. I want to create getSelectedIndices and setSelectedIndices method. Getter works, Setter throws IndexSizeException.
How can I set the selection area of WebView?
package hu.krisz;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewExample extends Application {
private String content = "<h1>Title</h1> <h2>SubTitle</h2> <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</p>";
@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
webView.getEngine().loadContent(content);
// Create button.
Button button = new Button("Select");
// Button EventHandler.
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// Work correctly.
int[] indeices = getSelectedIndices(webView);
System.out.println(indeices[0] + ", " + indeices[1]);
// netscape.javascript.JSException: IndexSizeError: The index is not in the allowed range.
// indeices[0] = 1;
// indeices[1] = 4;
setSelectedIndices(webView, indeices);
}
});
// Create VBox and add the button.
VBox vbox = new VBox(button);
// Create BorderPane and add VBox.
BorderPane root = new BorderPane();
root.setTop(vbox);
root.setCenter(webView);
// Create Scene
Scene scene = new Scene(root, 800, 600);
// Setting Stage
primaryStage.setTitle("WebView PĂ©lda");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public int[] getSelectedIndices(WebView webView) {
String script = "var selection = window.getSelection();" +
"if (selection.rangeCount > 0) {" +
" var range = selection.getRangeAt(0);" +
" var preSelectionRange = range.cloneRange();" +
" preSelectionRange.selectNodeContents(document.body);" +
" preSelectionRange.setEnd(range.startContainer, range.startOffset);" +
" var start = preSelectionRange.toString().length;" +
" var end = start + range.toString().length;" +
" start + ',' + end;" +
"} else {" +
" '0,0';" +
"}";
String result = (String) webView.getEngine().executeScript(script);
int[] indices = new int[2];
if (result != null && !result.isEmpty()) {
String[] splitResult = result.split(",");
indices[0] = Integer.parseInt(splitResult[0]);
indices[1] = Integer.parseInt(splitResult[1]);
} else {
indices[0] = 0;
indices[1] = 0;
}
return indices;
}
public void setSelectedIndices(WebView webView, int[] indices) {
String script = "function selectText(start, end) {" +
" var range = document.createRange();" +
" var selection = window.getSelection();" +
" range.setStart(document.body.firstChild, start);" +
" range.setEnd(document.body.firstChild, end);" +
" selection.removeAllRanges();" +
" selection.addRange(range);" +
"}" +
"selectText(" + indices[0] + ", " + indices[1] + ");";
webView.getEngine().executeScript(script);
System.out.println("SELECTED FINSIHED.");
}
}
I tried to changed the Js script.
String script = "function selectText(start, end) {" +
" var node = document.body;" +
" var range = document.createRange();" +
" var selection = window.getSelection();" +
" range.setStart(node, start);" +
" range.setEnd(node, end);" +
" selection.removeAllRanges();" +
" selection.addRange(range);" +
"}" +
"selectText(" + indices[0] + ", " + indices[1] + ");";
This way, the method doesn’t always throw errors, but either doesn’t select anything or doesn’t select the specified range