有一个网站我想运行JS脚本,如:
document.getElementById("statuses").rows[2].cells[1].firstChild.nodeValue
得到一些结果。我已添加WebView
并尝试实施:
MainActivity.OnCreate
WebView wv = FindViewById<WebView>(Resource.Id.webView);
wv.Settings.JavaScriptEnabled = true;
wv.SetWebViewClient(new Client());
wv.LoadUrl("http://inout.test.com/");
然后 Cleint
public class Client : WebViewClient {
public override void OnPageFinished(WebView view, string url) {
base.OnPageFinished(view, url);
var script = $"javascript: document.getElementById(\"statuses\").rows[2].cells[1].firstChild.nodeValue";
//view.LoadUrl($"javascript: {script}");
view.EvaluateJavascript(script , new JsResult());
}
}
JsResult
public class JsResult : Java.Lang.Object, IValueCallback {
public string Result;
public void OnReceiveValue(Java.Lang.Object value) {
Result = ((Java.Lang.String)value).ToString();
}
}
但Result
总是空的!我做错了什么?是否有可能从JS获得价值?请帮忙。