Italian [ risolto] help su JavaScript injection

Xfood

Expert
Licensed User
Buongiorno a tutti,
vorrei tramite webview sia da b4j che da b4a , dopo aver caricato una padina dove ce utente , password, login
poter comilare automaticamente utente password, e premere in automatico il tasto login,
come fare?

allego il debug della pagina

B4X:
  <div class="row">
                <form method="post" action='/PDA/it/Login/PostLogin' style="background-color:#ffffff; color:#7e7e7e;">
                    <div><label for="username">Username</label></div>
                    <div><input type="text" name="username" id="username" /></div>

                    <div><label for="password">Password</label></div>
                    <div><input type="password" name="password" id="password" /></div>

                    <div class="" style="margin-top:1em;">
                        <input type="submit" class="" style="width:16em;" value="LOGIN" />
                    </div>
                </form>
            </div>
 

Xfood

Expert
Licensed User
sono a meta strada,
riesco a compilare correttamente i cambi, ma non riesco a simulare la pressione del tasto login
<div class="" style="margin-top:1em;">
<input type="submit" class="" style="width:16em;" value="LOGIN" />
</div>


allego il codice scritto finora
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://cloud.xxxx/")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub JS(s As String)
    Dim joWV As JavaObject = WebView1
    joWV.RunMethodJO("getEngine", Null).RunMethod("executeScript", Array As String(s))
End Sub

Private Sub Button1_Click
    'JS($"document.getElementById("username").value="123";"$)
    'JS($"document.getElementById("password").value="123";"$)
    
    RunJavaScript($"document.getElementById("username").value="123";"$)
    RunJavaScript($"document.getElementById("password").value="123";"$)
    
    
End Sub

Private Sub Button2_Click
    'JS($"document.getElementById("submit").click();"$)
    RunJavaScript($"document.getElementById("submit").click();"$)
End Sub


Public Sub RunJavaScript (JSX As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(JSX))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub
 

Xfood

Expert
Licensed User
Risolto, allego il codice completo
aiutato dall'intelligenza artificiale

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    WebView1.LoadURL("https://cloud.xxxx/")
End Sub



' questo compila il form con utente e password
Private Sub Button1_Click
      
    RunJavaScript($"document.getElementById("username").value="123";"$)
    RunJavaScript($"document.getElementById("password").value="123";"$)
    
    
End Sub


' questo simula la pressione del tasto submit
Private Sub Button2_Click
   Dim JSx As String = "javascript:(function() {" & _
                           "    var form = document.querySelector('form');" & _
                           "    if (form) {" & _
                           "        form.submit();" & _
                           "    } else {" & _
                           "        alert('Form non trovato');" & _
                           "    }" & _
                           "})();"
    RunJavaScript(JSx)
  
End Sub


Public Sub RunJavaScript (JSX As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(JSX))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub
 
Top