Xamarin: How to Get HTML from Page in Webview

Xamarin: How to get HTML from page in WebView?

You can just use javascript webView.EvaluateJavascript ("document.body.innerHTML")

Get HTML document from Xamarin Forms WebView

From a user named "jgold6" on the GitHub bug report for this issue, he suggested the following:

// fetch the document element
var page = await controller.EvaluateJavaScriptAsync("document.documentElement.outerHTML");

// Unescape that damn Unicode Java bull.
page = Regex.Replace(page, @"\\[Uu]([0-9A-Fa-f]{4})", m => char.ToString((char)ushort.Parse(m.Groups[1].Value, NumberStyles.AllowHexSpecifier)));
page = Regex.Unescape(page);

This effectively solved the problem for me. This is also what Lucas suggested above. Thanks all for the help on this.

How to get data from the page in WebView? Android Xamarin

you could use Custom WebViewClient and AddJavascriptInterface to achive it:

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_other);
webView = FindViewById<WebView>(Resource.Id.webView1);
webView.SetWebViewClient(new WebViewClientClass());
WebSettings websettings = webView.Settings;
websettings.JavaScriptEnabled = true;
websettings.DomStorageEnabled = true;
webView.AddJavascriptInterface(new Foo(this), "Foo");
webView.LoadUrl("file:///android_asset/demo.html");
}

class WebViewClientClass : WebViewClient
{
public override void OnReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, string host, string realm)
{

}
public override void OnPageFinished(WebView view, string url)
{
view.LoadUrl("javascript:window.Foo.showSource("
+ "document.getElementsByTagName('html')[0].innerHTML);");
base.OnPageFinished(view, url);
}

}

class Foo : Java.Lang.Object
{
Context context;

public Foo(Context context)
{
this.context = context;
}
[JavascriptInterface]
[Export]
public void showSource(string html)
{
Log.Error("content", html);//here html is the HTML code
}
}

Xamarin : html page embeded in WebView doesn't execute js

It works if I don't use jquery in the js file.

 function displayName(str) {
var result = document.querySelector('#result');
result.innerHTML = str;
}

Get html content from webview in Android (using Xamarin with C#)

Your constructor is not public and you have to inherit from Java.Lang.Object. You have to add the Export attribute, too.

class MyJavaScriptInterface : Java.Lang.Object
{
private Context ctx;

public MyJavaScriptInterface(Context ctx)
{
this.ctx = ctx;
}

public MyJavaScriptInterface(IntPtr handle, JniHandleOwnership transfer)
: base (handle, transfer)
{
}

[Export("showHTML")]
public void showHTML(string html)
{
Console.WriteLine(html);
}
}

And in your javascript code is an error, too. You are missing a opening ( after showHTML.

view.LoadUrl("javascript:HtmlViewer.showHTML(" + ...


Related Topics



Leave a reply



Submit