How to enter a value in the prompt box?

2

I need to access a webpage through the WebView component.

1- I can load the page.

2- I can intercept the button event.

I can not insert / inject var name = prompt("Introduza o numero:");

Note: The page is only available via intranet - I can not share the link.

HTML button:

<input class="SUBMIT" onclick="return collect_info();" 
       onmouseout="this.className='SUBMIT';" onmouseover="this.className='SUBMITOVER';" 
       alt="Enviar Pedido" name="3" value="Falta de mat. no YYYY" type="SUBMIT">

JavaScript Function:

function collect_info(){

var name = prompt("Introduza o numero:"); // <---- Problema aqui -  Não consigo inserir este valor
// Expressao regular - apenas decimais
var regexp = /^\d\d?$/;
if (!regexp.test(name))
        {
                 alert("Campo númerico de 2 digitos. Confirme por favor.");
         return false;
        }


if (name != '' && name != null) 
 {
    var elem = document.createElement("input");
    elem.type = "hidden";
    elem.name = "id_c";
    elem.id = "id_c";
    elem.value = name;
    document.forms["alarmes"].appendChild(elem);
  }
else 
    return false;


var name = prompt("Introduza o numero:");
// Expressao regular - apenas decimais
//if (name.length < 10)
//        {
//                 alert("Numero demasiado curto. Confirme por favor.");
//                 return false;
//        }



if (name != '' && name != null) 
 {
    var elem = document.createElement("input");
    elem.type = "hidden";
    elem.name = "id_p";
    elem.id = "id_p";
    elem.value = name;
    document.forms["alarmes"].appendChild(elem);
    return true;
  }
else 
    return false;
}

Java code:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.web.*;
import javafx.stage.*;

public class WebViewWithPromptHandler extends Application {
  public static void main(String[] args) { launch(args); }
  @Override 
    public void start(final Stage primaryStage) {
        WebView webView = new WebView();
        String url = "--->>   Link  <<---";
        webView.getEngine().load(url);
        webView.getEngine().setJavaScriptEnabled(true);
        webView.getEngine().setOnAlert(event -> {
              Alert alert = new Alert(Alert.AlertType.INFORMATION);
              alert.setHeaderText("Message from the web page");
              alert.setContentText(event.getData());
              alert.showAndWait();
        });

    final Scene scene = new Scene(webView);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The maximum I can receive is the return of when it is falso

Any help? Is there another way?

    
asked by anonymous 07.11.2017 / 16:07

1 answer

3

All of these boxes that appear in the browser are handled in WebEngine . To control calls to the prompt() function of Javascript, you need the WebEngine#setPromptHandler() :

webView.getEngine().setPromptHandler(data -> {
   return "1234"; // o retorno é o que será 'digitado' no prompt.
});

For testing, I created a page with a simple script that reads a value and gives an alert soon after:

(function(){
    var name = prompt("Qual é seu username?");
    alert(name);
})();

In the code that accesses and manipulates the page, I defined two handlers. One to write at the prompt and another to display the alert message:

webview.getEngine().load("http://meulink.com");
webview.getEngine().setJavaScriptEnabled(true);

webview.getEngine().setOnAlert(event -> {
  System.out.println("Valor digitado no prompt: " + event.getData());
});

webview.getEngine().setPromptHandler(data -> {
  System.out.println("Mensagem do 'prompt': " + data.getMessage());
  return "Meu username é jsantos1991"; // o retorno é o que será 'digitado' no prompt.
});

output:

Mensagem do 'prompt': Qual é seu username?
Valor digitado no prompt: Meu username é jsantos1991
    
09.11.2017 / 14:02