2

i have mjpg streamer running on my raspberry pi, and i made a android app to watch the stream, i just put a webview there and made it navigate to my streamers URL, but now i enabled the password feature on mjpg streamer. when i enter the page there is a popup asking for username and password:

Login prompt

Now i need my app to somehow enter the username and password automaticly every time i oppen the app.

I have done some research and the only thing i could find was webbrowser controls for c# windows forms:

webBrowser1.Document.GetElementById("username").SetAttribute("value", "supersecretusername");
webBrowser1.Document.GetElementById("Log_in").InvokeMember("click");

But don't think these would work on android's webview and on javascript's alerts.

Is there a way to enter the username and password to these fields and click the button automaticly, or is there a way to enter these once and somehow save them??

I should also mention that i am new to programming and i am not a native english speaker (as you can probably tell), thanks.

Ingmar05
  • 501
  • 2
  • 8
  • 25
  • Possible duplicate of [using javascript in android webview](http://stackoverflow.com/questions/10472839/using-javascript-in-android-webview) – codeCompiler77 Mar 19 '16 at 20:24

3 Answers3

4

You can use LoadUrl method to inject JavaScript into your WebView. Here is a quick sample how it works in Android:

1) Once you have the WebView reference you will need to set JavaScript to enabled

        var webBrowser = this.FindViewById<WebView>(Resource.Id.webView1);
        webBrowser.Settings.JavaScriptEnabled = true;

2) To handle load events you will need to assign a WebViewClient

        webBrowser.SetWebViewClient(new Client());

3) For demo purpose I created a sample HTML page and load it from the assets:

        webBrowser.LoadUrl("file:///android_asset/main.html");

4) The client itself

public class Client : WebViewClient
{
    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);

        if (!url.Contains("main.html")) return;

        var username = "supersecretusername";
        var password = "supersecretpassword";

        // to ensure Log_in click isn't called before username & password are set lets
        // create the script as single item instead of calling them separately
        var script = $"document.getElementById(\"username\").value = \"{username}\";" +
                     $"document.getElementById(\"password\").value = \"{password}\";" +
                     "document.getElementById(\"Log_in\").click()";

        view.LoadUrl($"javascript: {script}");

        // separate calls
        //view.LoadUrl("javascript: document.getElementById(\"username\").value = \"sami\";document.getElementById(\"password\").value = \"password\";");
        //view.LoadUrl("javascript: document.getElementById(\"password\").value = \"password\"");
        //view.LoadUrl("javascript: document.getElementById(\"Log_in\").click()");
    }
}
SKall
  • 5,234
  • 1
  • 16
  • 25
0

This is what worked for me (VS2017):

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp"
             x:Class="MyApp.MainPage"
             NavigationPage.HasNavigationBar="False"
             Title="My App">
    <WebView x:Name="browser" Navigating="OnNavigating" Navigated="OnNavigated"></WebView>
</ContentPage>

MainPage.xaml.cs

using System;
using Xamarin.Forms;

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            browser.Source = "..."; // or in xaml
        }

        // Event when webview has finished change
        void OnNavigated(object sender, WebNavigatedEventArgs args)
        {
            string script = "...;";
            browser.EvaluateJavaScriptAsync(script);
        }

        // ...
    }
}

https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.webview.evaluatejavascriptasync?view=xamarin-forms#Xamarin_Forms_WebView_EvaluateJavaScriptAsync_System_String_

Wtower
  • 18,848
  • 11
  • 103
  • 80
-1

You're posting in the wrong section. You question also isn't very clear. Is this your native program? Do you have full access to the source?

From my understanding, what you're looking for is mobile automation.

Look up robot framework and appium. Used for mobile automation and testing. Appium is the mobile equivalent of selenium and robot framework is just a python wrapper so you're not actually forced to learn python if you don't already know it.

codeCompiler77
  • 508
  • 7
  • 22
  • Yes, i have full access to the source and i need to edit it to enter the login credentials automaticly. – Ingmar05 Mar 19 '16 at 09:07
  • You should have your webview setup somewhere as a context `Context wvContext`, you also have to give it a `JSInterface`. I'm going to flag as duplicate because this has already been answered. – codeCompiler77 Mar 19 '16 at 20:23