For my Xamarin app, I want to get the custom attribute value present inside < a href> tag provided to the UIWebView on every click.
Example:
<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>
goog
yah
WebView.LoadFinished += (object sender, EventArgs e) =>
{
var htmlstring = frmWebView.EvaluateJavascript("document.documentElement.outerHTML");
}
data-type
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);
}