如何在javaFX中从webView获取getSelectedText

时间:2012-10-08 16:51:58

标签: java webview javafx

我需要从javaFX中的webView获取所选文本。我还没有找到任何线索如何在互联网上进行。

2 个答案:

答案 0 :(得分:6)

这可以通过JavaScript完成:

String selection = (String) webView.getEngine()
                     .executeScript("window.getSelection().toString()");
System.out.println(selection);

你也可能会觉得这很有帮助:
Communicating between JavaScript and JavaFX with WebEngine
executeScript

答案 1 :(得分:1)

结合jewelsea提供的解决方案:https://gist.github.com/jewelsea/7819195 并通过Get Selected HTML in browser via Javascript

可以从JAVAFX Webview中提取选定的Text和HTML源代码:

/*
 * 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>"
            + "&nbsp;&nbsp&nbsp&nbsp;-&nbspNelson 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);
    }

}