I have a strong need to calculate/get the rendered html size in WebView.
Is there a way to achieve this?
In Objective-C i could pass a HTML string to the Web
I have had to change the injection script to this:
string injectionStyleAndScript = @"<style> html,body {margin: 0;padding: 0;height: 100%;} </style><script type='text/javascript'>function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify(''+height);}</script>";
Ok, i have came up with a solution:
I injected this string into my html :
string injectionStyleAndScript = @"html,body {margin: 0;padding: 0;height: 100%;}function getHeight() { var height = document.getElementById('wrapper').offsetHeight; window.external.notify(''+height);}";
That means insert it before
htmlFragment – this is HTMl that i had to place on my WebView.
htmlFragment = htmlFragment.Insert(htmlFragment.IndexOf("</HEAD>"),injectionStyleAndScript); // Inser the Injection
Next thing, to calculate the body insides, i wrap it
Find the indexes from and to:
int from = htmlFragment.IndexOf("<BODY>")+6;
int to = htmlFragment.IndexOf("</BODY>");
Next everything inside the wrap into .
string bodyWrapper = string.Format("<div id='wrapper'>{0}</div>", htmlFragment.Substring(from, (htmlFragment.Length - 1 - from)-(htmlFragment.Length - 1 - to)));
Next, replace the old content with a new one ( that we created inside the wrapper ):
//Cut out the old one
htmlFragment = htmlFragment.Remove(from, (htmlFragment.Length - 1 - from) - (htmlFragment.Length - 1 - to));
//Insert our wrapper
htmlFragment = htmlFragment.Insert(from, bodyWrapper);
//Navigate to the HTML
MyWebView.NavigateToString(htmlFragment);
Next we need to attach 2 events to the WebView - DOMContentLoaded и ScriptNotify:
MyWebView.ScriptNotify += MyWebView;
MyWebView.DOMContentLoaded += MyWebView;
MyWebView.NavigateToString(htmlFragment);
And here are the two event handlers:
void MyWebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
MyWebView.InvokeScriptAsync("getHeight", null);
}
void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
string result = e.Value.ToString();
MyWebView.Height = double.Parse(result);
}