How to insert a value into the prompt box?

Asked

Viewed 209 times

2

I need to access a web page, through the component WebView.

1- I can load the page.

2- I can intercept the button event.

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

Note: The page is only available via intranet - I cannot 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 most I can get is the return when it is falso

alert

Any help? Is there another way?

1 answer

3


All these boxes that appear in the browser have treatment on WebEngine. To control calls to the function prompt() 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 right away:

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

In the code that accesses and manipulates the page, set two handlers. One to write at the prompt and one 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.