对于我的Xamarin应用程序,我想获取<中的自定义属性值。 a href>每次点击都会将标签提供给UIWebView。
示例:
<html>
<head>
<style>
.democlass {
color: red;
}
</style>
</head>
<body>
<p id="demo"></p>
<button>Button1</button>
<button>Button2</button>
<button>Button3</button>
<a href="https://www.google.com">
<div class="tile trending black" data-type="goog">
<div class="title">Title1</div>
<div class="subtitle">subtitle1</div>
<div class="subtitle">subtitle1</div>
</div>
</a>
<a href="https://www.yahoo.com">
<div class="tile trending black" data-type="yah">
<div class="title">Tit1e1</div>
<div class="subtitle">subtitle1a</div>
<div class="subtitle">subtitle1b</div>
</div>
</a>
</body>
</html>
当选择www.google.com时,我想跟踪数据类型并获取值goog
,当选择www.yahoo.com时,我想跟踪数据类型并获取值{{1 }}
现在我得到完整的HTML并解析它。
yah
是否有更好的方法可以跟踪每次点击事件的WebView.LoadFinished += (object sender, EventArgs e) =>
{
var htmlstring = frmWebView.EvaluateJavascript("document.documentElement.outerHTML");
}
值。
答案 0 :(得分:0)
<强>的iOS 强>
public override void ViewDidLoad()
{
base.ViewDidLoad();
var webView = new UIWebView();
webView.LoadRequest(new NSUrlRequest(new NSUrl(" */above html page or url/*")));
web.Delegate = new myDelegate();
}
public class myDelegate : UIWebViewDelegate
{
public override void LoadingFinished(UIWebView webView)
{
var jsScript = string.Empty;
if (request != null)
{
jsScript = string.Format("var aTag = document.getElementsByTagName('a');for(var i=0;i<aTag.length;i++) {{ var div = aTag[i]; if(!div) break;var link = div.getElementsByTagName('a'); if(aTag[i]=='{0}'){{aTag[i].childNodes[1].getAttribute('data-type')}};}}", request.Url.AbsoluteString);
var tracker = ((UIWebView)webView).EvaluateJavascript(jsScript);
}
}
}
<强>的Android 强>
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
webView = FindViewById<WebView> (Resource.Id.webview);
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new HelloWebViewClient());
webView.LoadUrl (/* url or html */);
}
public override void OnPageStarted(WebView view, string url, Bitmap favicon)
{
webView = view;
var jsScript = string.Empty;
if (url != null && url != "about:blank")
{
jsScript = string.Format("(function(){{var aTag = document.getElementsByTagName('a');for(var i=0;i<aTag.length;i++){{var div = aTag[i]; if(!div) break;var link = div.getElementsByTagName('a');if(aTag[i]=='{0}'){{return aTag[i].childNodes[1].getAttribute('data-type');}}}}}})();", url);
view.EvaluateJavascript(jsScript, this);
}
}
public void OnReceiveValue(Java.Lang.Object result)
{
string json = ((Java.Lang.String)result).ToString();
string theResult = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(json);
}