Ho bisogno di ottenere il testo selezionato da webView in javaFX. Non ho trovato alcun indizio su come farlo su internet.Come ottenereSelectedText da webView in javaFX
5
A
risposta
5
Questo può essere fatto con JavaScript:
String selection = (String) webView.getEngine()
.executeScript("window.getSelection().toString()");
System.out.println(selection);
Inoltre è possibile trovare questo utile:
Communicating between JavaScript and JavaFX with WebEngine
executeScript
0
Combinando soluzione fornita da jewelsea: https://gist.github.com/jewelsea/7819195 e Get Selected HTML in browser via Javascript:
Uno può estrarre sia il testo selezionato che i messaggi HTML codice ource da una JAVAFX Webview:
/*
* 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);
}
}
Problemi correlati
- 1. Caching in JavaFX WebView
- 2. Autodetect delega - JavaFX - WebView
- 3. Rimuovere ScrollBar nella WebView Javafx
- 4. Debug HTML/Javascript in JavaFX WebView
- 5. Versione di WebKit in JavaFX 8 WebView
- 6. Sfondo trasparente nella WebView in JavaFX
- 7. Rilevamento evento HTML onareaup in JavaFX WebView
- 8. Come utilizzare JavaFX WebView in DesktopApp con i file locali?
- 9. JavaFX WebView carica file Javascript locale
- 10. Applicazione del file CSS a JavaFX WebView
- 11. JavaFX - Come creare SnapShot/Screenshot di (invisibile) WebView
- 12. Come passare i dati da WebView al controller e viceversa in JavaFX
- 13. Gestione risorse JavaFX: caricamento di file HTML in WebView
- 14. Come chiamare una funzione JavaScript da una WebView JavaFX con clic sul pulsante?
- 15. JAVAFX/WebView/WebEngine FireBugLite o qualche altro debugger?
- 16. JavaFx Webview JDK 8 non può caricare il certificato autofirmato
- 17. È possibile modificare WebView di JavaFX su Chrome
- 18. JavaFX da Clojure repl
- 19. Leggi selezione da TableView in JavaFX 2.0
- 20. Impossibile richiamare Scene da MenuItem in JavaFX
- 21. Cattura eccezioni JavaScript da WebView in ios
- 22. SecondaryLoop in JavaFX, come Swing?
- 23. Come chiamo window.openDatabase da una webView ospitata in un'applicazione Android?
- 24. Come aprire url da stringa in WebView per iPhone
- 25. JavaFX Più colori in textarea (JavaFX 8)
- 26. Come utilizzare OpenGL in JavaFX?
- 27. JavaFX TextField EventHandler
- 28. Integrazione di JavaFX 2.0 WebView in un'applicazione Swing Java SE 6
- 29. Come creare una sovrapposizione sulla vista Web JavaFX 2?
- 30. Aggiungi controllo WebView su Swing JFrame