TheDeveloper TheDeveloper - 1 year ago 163
Android Question

Xamarin WebView: How to get custom data attribute values from URL content on every click?

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>


When www.google.com is selected I want to track data-type and get the value
goog
and when www.yahoo.com is selected I want to track data-type and get the value
yah


Right now I get the complete html and parse it.

WebView.LoadFinished += (object sender, EventArgs e) =>
{
var htmlstring = frmWebView.EvaluateJavascript("document.documentElement.outerHTML");

}


Is there a better way to track
data-type
value on every click event.

Answer Source

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);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download