2012-10-08 36 views

Respuesta

0

solución Combinando proporcionada por jewelsea: https://gist.github.com/jewelsea/7819195 y por Get Selected HTML in browser via Javascript:

Se pueden extraer tanto texto seleccionado como HTML s Código de origen de una vista web de 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>" 
      + "&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); 
    } 

} 
Cuestiones relacionadas