1

I'm working on a project in Xamarin C# and I'm having trouble running some Java Script. Is there currently a way to call a specific JS function and receive its return value?

I know that JS can be run in a WebView; however, how can I get the output?

At the moment, the JS is coming from a link on my site. Any help would be very much appreciated!


EDIT

Here's the code I've tried:

MyView.axml

 <android.webkit.WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            local:layout_constraintTop_toBottomOf="@+id/pause"
            local:layout_constraintBottom_toBottomOf="parent"/>

MyView.cs

called in protected override void OnCreate

webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new MyWebViewClient());
webView.LoadUrl("http://www.example.com/scheduleV5.js");

MyWebViewClient (class)

public class MyWebViewClient : WebViewClient
{
    public override void OnPageFinished(WebView view, String url)
    {
        view.EvaluateJavascript("javascript: getSchedule();", new EvaluateBack());
    }
}

EvaluateBack (class)

public class EvaluateBack : Java.Lang.Object, IValueCallback
{

    public void OnReceiveValue(Java.Lang.Object value)
    {
        Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        var test = value.ToString();
    }
}
Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32
Scout 43
  • 49
  • 1
  • 7
  • Please see [How to pass data to WebView Javascript and back](https://forums.xamarin.com/discussion/100808/how-pass-data-to-webview-javascript-and-back) or [Xamarin Forms WebView Executing JavaScript](https://xamarinhelp.com/xamarin-forms-webview-executing-javascript/) – Morrison Chang Jan 07 '19 at 01:04
  • could it work ? – Leo Zhu Jan 10 '19 at 05:32

1 Answers1

3

you could do like this

Js calls methods in C#

Configure the OnCreate method in the Activity :

var webview = FindViewById<WebView>(Resource.Id.webView1);
WebSettings settings = webview.Settings;
settings.JavaScriptEnabled = true;
// load the javascript interface method to call the foreground method
webView.AddJavascriptInterface(new MyJSInterface(this), "CSharp");
webview.SetWebViewClient(new WebViewClient());

Create a C# class :

class MyJSInterface : Java.Lang.Object
  {
    Context context;

  public MyJSInterface (Context context)
    {
      this.context = context;
    }

  [JavascriptInterface]
  [Export]
  public void ShowToast (string msg)
    {
      Toast.MakeText(context, msg, ToastLength.Short).Show();
    }
  }

And then it's called in JS :

<button type="button" onClick="CSharp.ShowToast ('Call C#')">Call C#</button>

You can refer to this document:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_from_javascript

C# calls a method in JS and gets the callback value

webView.EvaluateJavascript("javascript: callJS();", new EvaluateBack());

class EvaluateBack : Java.Lang.Object,IValueCallback
    {

        public void OnReceiveValue(Object value)
        {

            Toast.MakeText(Android.App.Application.Context, value.ToString(), ToastLength.Short).Show();// you will get the value "100"
        }
    }

in JS :

<script>
      function callJS(){
        return 100;
      }
</script>
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • could it help you ? – Leo Zhu Jan 07 '19 at 07:16
  • I might be a little confused, do you want to call the JS method in C# and get the callback value? If so, please refer to the second method I updated – Leo Zhu Jan 07 '19 at 10:00
  • Perfect! the second method is what I'm looking for. I'll try it out and let you know. – Scout 43 Jan 07 '19 at 12:53
  • I keep getting a null return value. Just to clarify I call: `webView.LoadUrl("http://mywebsite.com/file.js"); ` then evaluatescript? – Scout 43 Jan 10 '19 at 17:36
  • You could customize a "WebViewClient" and then call "webview.evaluatescript" in "OnPageFinished" – Leo Zhu Jan 11 '19 at 01:38
  • Still getting null... will update post with some code I’ve tried tomorrow. – Scout 43 Jan 14 '19 at 23:39
  • Fixed it! Had to use a link to the html page where the java script was being utilized, instead of a direct link to the file. Thanks for all your help! – Scout 43 Jan 15 '19 at 04:54