Asked  7 Months ago    Answers:  5   Viewed   33 times

On Android, I have a WebView that is displaying a page.

How do I get the page source without requesting the page again?

It seems WebView should have some kind of getPageSource() method that returns a string, but alas it does not.

If I enable JavaScript, what is the appropriate JavaScript to put in this call to get the contents?

webview.loadUrl("javascript:(function() { " +  
    "document.getElementsByTagName('body')[0].style.color = 'red'; " +  



I know this is a late answer, but I found this question because I had the same problem. I think I found the answer in this post on The code below is basically a cut-and-paste from the site. It seems to do the trick.

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
    public void processHTML(String html)
        // process the html as needed by the app

final WebView browser = (WebView)findViewById(;
/* JavaScript must be enabled if you want it to work, obviously */

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url)
        /* This call inject JavaScript into the page which just finished loading. */

/* load a web page */
Tuesday, June 1, 2021
answered 7 Months ago

After visiting the above links, I come up with this code and hope this helps.

wv.setWebChromeClient(new WebChromeClient() {
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
        WebView.HitTestResult result = view.getHitTestResult();
        String data = result.getExtra();
        Context context = view.getContext();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
        return false;
Wednesday, June 23, 2021
answered 6 Months ago

You can get the raw data by calling ReadAsStringAsAsync on the Request.Content property.

string result = await Request.Content.ReadAsStringAsync();

There are various overloads if you want it in a byte or in a stream. Since these are async-methods you need to make sure your controller is async:

public async Task<IHttpActionResult> GetSomething()
    var rawMessage = await Request.Content.ReadAsStringAsync();
    // ...
    return Ok();

EDIT: if you're receiving an empty string from this method, it means something else has already read it. When it does that, it leaves the pointer at the end. An alternative method of doing this is as follows:

public IHttpActionResult GetSomething()
    var reader = new StreamReader(Request.Body);
    reader.BaseStream.Seek(0, SeekOrigin.Begin); 
    var rawMessage = reader.ReadToEnd();

    return Ok();

In this case, your endpoint doesn't need to be async (unless you have other async-methods)

Monday, July 19, 2021
answered 5 Months ago

In onPageFinished():

            + "var FunctionOne = function () {"
            + "  var r = $.Deferred();"
            + "  try{document.getElementsByClassName('header')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('section_0').style.display='none';}catch(e){}"
            + "  try{document.getElementById('page-actions').style.display='none';}catch(e){}"
            + "  try{document.getElementsByClassName('languageSelector')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('mw-mf-last-modified').style.display='none';}catch(e){}"
            + "  try{document.getElementById('footer').style.display='none';}catch(e){}"
            + "  setTimeout(function () {"
            + "    r.resolve();"
            + "  }, 2500);"
            + "  return r;"
            + "};"
            + "var FunctionTwo = function () {"
            + "  window.CallToAnAndroidFunction.setVisible();"
            + "};"
            + "FunctionOne().done(FunctionTwo);");

In MainActivity.onCreate():

this.webView.addJavascriptInterface(new JsObject(webView, loadingView), "CallToAnAndroidFunction");

In MainActivity():

public class JsObject {
    private View loadingView;
    private View view;
    JsObject(View view, View loadingView){this.view = view;this.loadingView = loadingView;}
    public void setVisible(){
        runOnUiThread(new Runnable() {

           public void run() {

So, it was a combination of making a JavascriptInterface and making a JS function to wait for the JS calls to finish before calling the interface (with the visibility settings).

Wednesday, September 8, 2021
answered 3 Months ago

You can use AOP techniques for this task. Considering PostSharp, you can create custom aspect like this:

public class TraceAttribute : OnMethodBoundaryAspect
    public override void OnEntry(MethodExecutionArgs args)
        Trace.WriteLine(string.Format("Entering {0}", args.Method.Name));

        for (int i = 0; i < args.Arguments.Count; i++)
            Trace.WriteLine(string.Format("    {0}", args.Arguments.GetArgument(i)));

and then apply it to your web-service methods:

[WebMethod, Trace]
public string HelloWorld()
    return "Hello World";
Wednesday, November 24, 2021
answered 6 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :