admin管理员组

文章数量:1317131

I am writing a basic web browser that can only go to a certain website (developed and maintained by another pany) for my work, however in order for the log in and the time spent on the site to be counted (two VERY important things for my pany) you need to log out with a certain button on the site. I looked at the page source and all that button does is call a javascript function (named something like doLogoff() or something similar) which on a normal browser simply closes the window that is created after you log in. In my application everything is done in ONE window, there are no tabs (there are no need for them) and I'm not entirely sure what the call to close the window does to my application, but the site on the WebView simply stays on that page and only goes back to the login page if you click on a link.

Is there anyway to detect when a certain JavaScript function is called in a WebView? If I can bind that function and make sure the log out is actually being performed, then I can just make the webview go to the login page myself.

I am writing a basic web browser that can only go to a certain website (developed and maintained by another pany) for my work, however in order for the log in and the time spent on the site to be counted (two VERY important things for my pany) you need to log out with a certain button on the site. I looked at the page source and all that button does is call a javascript function (named something like doLogoff() or something similar) which on a normal browser simply closes the window that is created after you log in. In my application everything is done in ONE window, there are no tabs (there are no need for them) and I'm not entirely sure what the call to close the window does to my application, but the site on the WebView simply stays on that page and only goes back to the login page if you click on a link.

Is there anyway to detect when a certain JavaScript function is called in a WebView? If I can bind that function and make sure the log out is actually being performed, then I can just make the webview go to the login page myself.

Share Improve this question asked Aug 17, 2012 at 17:47 NexionNexion 4326 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can do that with a JavascriptInterface. The following example es from the documentation. It works the other way round. You can create a function in javascript that will trigger java code in your activity.

You declare your interface in your java code.

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

You add the interface to your WebView

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

And in your web page you can call the java method from a script

<script type="text/javascript">
    function showAndroidToast(toast) {
    Android.showToast(toast);
    }
</script>

First, create the JavascriptInterface, prescribed by NathanZ.

Then override the function that you want to hook into, like this:

webview.loadUrl("javascript:" +
"var functionNameOriginal = functionName;" +
"functionName = function(args) {" +
    "Android.showToast();" +
    "functionNameOriginal(args);" +
"}");

本文标签: androidCatch certain Javascript function calls in a WebViewStack Overflow