无法在 Android 4.4 中从 javascript 调用 Sanitize 方法,因为在 Android 4.1 中同样有效
Not able to call Sanitize method from javascript in Android 4.4 as same is working in Android 4.1
无法在 Android 4.4 中从 javascript 调用 Sanitize 方法,而在 Android 4.1 中同样有效。我们可以从 android 4.1 调用 Sanitize 方法,例如 Foo.sanitize('test message'),但在 4.4 中同样不起作用。有人可以尝试解决如何调用 sanitize 方法。
如果我们再次添加 [Export] & [JavascriptInterface] 它的工作,但理想情况下不需要这些注释来调用 sanitize。
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Java.Interop;
using Android.Net;
namespace WebViewJavaScriptInterface
{
[Activity (Label = "Mono WebView ScriptInterface", MainLauncher = true)]
public class JavaScriptInterfaceActivity : Activity
{
const string html = @"<html><body><p>This is a paragraph.</p><button type=""button"" onClick=""Foo.sanitize('test message')"">Click Me!</button></body></html>";
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
WebView view = FindViewById<WebView> (Resource.Id.web);
view.Settings.JavaScriptEnabled = true;
view.SetWebChromeClient (new WebChromeClient ());
view.AddJavascriptInterface ((Java.Lang.Object)new Foo (this), "Foo");
view.LoadData (html, "text/html", null);
}
}
class Foo : Java.Lang.Object, UrlQuerySanitizer.IValueSanitizer
{
public Foo (Context context)
{
this.context = context;
}
Context context;
[Export]
[JavascriptInterface]
public void Bar (string message)
{
Console.WriteLine ("Foo.Bar invoked!");
Toast.MakeText (context, "This is a Toast from C#! " + message.ToString(), ToastLength.Short).Show ();
}
public string Sanitize(string value)
{
Console.WriteLine("Santizie.Bar invoked!");
return string.Empty;
}
}
}
Uncaught TypeError: Object [object Object] has no method 'bar'"
方法应该是Bar
,而不是bar
。
请将您的 html
更改为:
const string html = @"
<html><body><p>This is a paragraph.</p><button type=""button"" onClick=""Foo.Bar('test message')"">Click Me!</button></body></html>
";
无法在 Android 4.4 中从 javascript 调用 Sanitize 方法,而在 Android 4.1 中同样有效。我们可以从 android 4.1 调用 Sanitize 方法,例如 Foo.sanitize('test message'),但在 4.4 中同样不起作用。有人可以尝试解决如何调用 sanitize 方法。 如果我们再次添加 [Export] & [JavascriptInterface] 它的工作,但理想情况下不需要这些注释来调用 sanitize。
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;
using Java.Interop;
using Android.Net;
namespace WebViewJavaScriptInterface
{
[Activity (Label = "Mono WebView ScriptInterface", MainLauncher = true)]
public class JavaScriptInterfaceActivity : Activity
{
const string html = @"<html><body><p>This is a paragraph.</p><button type=""button"" onClick=""Foo.sanitize('test message')"">Click Me!</button></body></html>";
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
WebView view = FindViewById<WebView> (Resource.Id.web);
view.Settings.JavaScriptEnabled = true;
view.SetWebChromeClient (new WebChromeClient ());
view.AddJavascriptInterface ((Java.Lang.Object)new Foo (this), "Foo");
view.LoadData (html, "text/html", null);
}
}
class Foo : Java.Lang.Object, UrlQuerySanitizer.IValueSanitizer
{
public Foo (Context context)
{
this.context = context;
}
Context context;
[Export]
[JavascriptInterface]
public void Bar (string message)
{
Console.WriteLine ("Foo.Bar invoked!");
Toast.MakeText (context, "This is a Toast from C#! " + message.ToString(), ToastLength.Short).Show ();
}
public string Sanitize(string value)
{
Console.WriteLine("Santizie.Bar invoked!");
return string.Empty;
}
}
}
Uncaught TypeError: Object [object Object] has no method 'bar'"
方法应该是Bar
,而不是bar
。
请将您的 html
更改为:
const string html = @"
<html><body><p>This is a paragraph.</p><button type=""button"" onClick=""Foo.Bar('test message')"">Click Me!</button></body></html>
";