Tôi cần lấy văn bản đã chọn từ webView trong javaFX. Tôi đã không tìm thấy bất kỳ đầu mối làm thế nào để làm điều đó trên internet.Cách getSelectedText từ webView trong javaFX
5
A
Trả lời
5
Điều này có thể được thực hiện với JavaScript:
String selection = (String) webView.getEngine()
.executeScript("window.getSelection().toString()");
System.out.println(selection);
Ngoài ra bạn có thể tìm thấy điều này hữu ích:
Communicating between JavaScript and JavaFX with WebEngine
executeScript
0
giải pháp Kết hợp được cung cấp bởi jewelsea: https://gist.github.com/jewelsea/7819195 và bởi Get Selected HTML in browser via Javascript:
Người ta có thể trích xuất cả văn bản và HTML đã chọn mã ource từ WebView của JAVAFX:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SelectInWebview;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class SelectionHTMLExtractor extends Application {
public static final String HTML
= "<p><em>\"Do not judge me by my successes, judge me by how many times I fell down and got back up again.\"</em></p>"
+ "    - Nelson Rolihlahla Mandela";
public static final String SELECT_TEXT
= "(function getSelectionText() {\n"
+ " var text = \"\";\n"
+ " if (window.getSelection) {\n"
+ " text = window.getSelection().toString();\n"
+ " } else if (document.selection && document.selection.type != \"Control\") {\n"
+ " text = document.selection.createRange().text;\n"
+ " }\n"
// + " if (window.getSelection) {\n"
// + " if (window.getSelection().empty) { // Chrome\n"
// + " window.getSelection().empty();\n"
// + " } else if (window.getSelection().removeAllRanges) { // Firefox\n"
// + " window.getSelection().removeAllRanges();\n"
// + " }\n"
// + " } else if (document.selection) { // IE?\n"
// + " document.selection.empty();\n"
// + " }"
+ " return text;\n"
+ "})()";
public static final String SELECT_HTML
= "(getSelectionHTML = function() {\n"
+ " var userSelection;\n"
+ " if (window.getSelection) {\n"
+ " // W3C Ranges\n"
+ " userSelection = window.getSelection();\n"
+ " // Get the range:\n"
+ " if (userSelection.getRangeAt)\n"
+ " var range = userSelection.getRangeAt (0);\n"
+ " else {\n"
+ " var range = document.createRange();\n"
+ " range.setStart (userSelection.anchorNode, userSelection.anchorOffset);\n"
+ " range.setEnd (userSelection.focusNode, userSelection.focusOffset);\n"
+ " }\n"
+ " // And the HTML:\n"
+ " var clonedSelection = range.cloneContents();\n"
+ " var div = document.createElement ('div');\n"
+ " div.appendChild (clonedSelection);\n"
+ " return div.innerHTML;\n"
+ " } else if (document.selection) {\n"
+ " // Explorer selection, return the HTML\n"
+ " userSelection = document.selection.createRange();\n"
+ " return userSelection.htmlText;\n"
+ " } else {\n"
+ " return '';\n"
+ " }\n"
+ " })()";
@Override
public void start(Stage stage) throws Exception {
HTMLEditor wisdom = new HTMLEditor();
wisdom.setHtmlText(HTML);
Label selectedText = new Label();
Label selectedHTMLSource = new Label();
Button selectText = new Button("Get TEXT Selection");
selectText.setOnAction(event -> {
WebView webView = (WebView) wisdom.lookup("WebView");
if (webView != null) {
WebEngine engine = webView.getEngine();
Object selection = engine.executeScript(SELECT_TEXT);
if (selection instanceof String) {
selectedHTMLSource.setText((String) selection);
}
}
});
Button selectHTML = new Button("Get HTML Selection");
selectHTML.setOnAction(event -> {
WebView webView = (WebView) wisdom.lookup("WebView");
if (webView != null) {
WebEngine engine = webView.getEngine();
Object selection = engine.executeScript(SELECT_HTML);
if (selection instanceof String) {
selectedText.setText((String) selection);
}
}
});
VBox layout = new VBox(
10,
new ToolBar(selectHTML, selectText),
selectedText, selectedHTMLSource,
wisdom
);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(10));
stage.setTitle("SelectionHTMLExtractor");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(SelectionHTMLExtractor.class);
}
}
Các vấn đề liên quan
- 1. Caching trong JavaFX WebView
- 2. Xóa thanh cuộn trong WebView Javafx
- 3. Cách chuyển dữ liệu từ WebView sang bộ điều khiển và ngược lại trong JavaFX
- 4. Proxy tự động phát hiện - JavaFX - webview
- 5. Thực thi chức năng Javascript cho một WebView từ một chương trình JavaFX
- 6. Android: cách chọn văn bản từ webview
- 7. Làm thế nào để sử dụng JavaFX WebView trong DesktopApp với các tệp cục bộ?
- 8. tải nội dung từ chế độ xem web bằng cách sử dụng javafx
- 9. JavaFX WebView vô hiệu hóa Chính sách gốc tương tự (cho phép yêu cầu miền chéo)
- 10. JavaFX 2.0+ WebView/WebEngine hiển thị trang web thành hình ảnh
- 11. Cách tạo tệp jar từ dự án javaFX
- 12. Lấy mục đã chọn từ JavaFX TableView
- 13. JAVAFX/WebView/WebEngine FireBugLite hoặc Một số trình gỡ lỗi khác?
- 14. Cách xử lý tải xuống tệp bằng cách sử dụng JavaFX 2.0 WebEngine
- 15. android - Cách thực hiện cuộc gọi điện thoại từ webview
- 16. Thêm điều khiển WebView trên Swing JFrame
- 17. Ngăn chặn một accordion trong JavaFX từ sụp đổ
- 18. Đọc lựa chọn từ TableView trong JavaFX 2.0
- 19. Cách tạo hoạt ảnh GIF hoạt động từ Android WebView
- 20. tạo MouseEvent trong JavaFX
- 21. cocoa : Cách tạo nền trong suốt webView?
- 22. JavaFX trong tự nhiên?
- 23. Có thể lấy mã HTML từ WebView
- 24. Tùy chọn chỉnh sửa HTML trong JavaFX
- 25. Chơi các file mp3 trong JavaFX từ input stream
- 26. Cách tạo hộp thoại JavaFX?
- 27. Lấy HTML nguồn từ một WebView trong Cocoa
- 28. JavaFX - phong cách CSS listview
- 29. Cách đặt mặc định JavaFX
- 30. Android: Mở một ContextMenu từ onLongPress trong WebView tùy chỉnh