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
Entity Framework Specification Pattern Implementation
Calling a SQL User-Defined Function in a Linq Query
Linq-To-Sql: Recursively Get Children
How to Hook into Hardware Keyboard Events at an Application Level
Search Xdocument Using Linq Without Knowing the Namespace
Is the Destructor Called If the Constructor Throws an Exception
How Can Synchronizationcontext.Current of the Main Thread Become Null in a Windows Forms Application
Deploying ASP.NET Web Forms Project to Fedora 24
Embedding Flash Player in a C++ or Java Application
How to Consume a Blazor Component as a Web Component Within a Regular Non-Blazor HTML Page
How to Make the .Net Httpclient Use Http 2.0
What Is Ruby (1.8.7) Analog to Sorteddictionary in C#/.Net
Compile to a Stand-Alone Executable (.Exe) in Visual Studio
Copying One Azure Blob to Another Blob in Azure Storage Client 2.0